⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 placing a list of items.htm

📁 这是一本关于XML的学习的书
💻 HTM
字号:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<!-- saved from url=(0068)file://G:\application\XSL%20uguid\Authoring%20Well-Formed%20HTML.htm -->
<HTML><HEAD><TITLE>使用context()方法在一个grid表格中安排一个项目链表</TITLE>
<META content="text/html; charset=gb_2312-80" http-equiv=Content-Type>
<STYLE type=text/css>.normal10 {
	FONT-SIZE: 11pt
}
.normal9 {
	FONT-SIZE: 9pt
}
A {
	TEXT-DECORATION: none
}
A:hover {
	COLOR: red; TEXT-DECORATION: underline
}
A {
	COLOR: cornflowerblue; FONT-FAMILY: "新细明体", "宋体"; FONT-SIZE: 11pt; FONT-WEIGHT: bold
}
.normal105 {
	FONT-SIZE: 11pt
}
.p {
	FONT-SIZE: 11pt
}
</STYLE>
<LINK href="level_2_2.files/Global.css" tppabs="http://www.xml.org.cn:8188/application/XSL%20uguid/level_2_2.files/Global.css" rel=stylesheet>
<META content="MSHTML 5.00.2014.210" name=GENERATOR></HEAD>
<BODY>
<P align=center class=normal105>[<A href="file:///G:/default.asp" 
target=_parent>返回首页</A>][<FONT face=宋体 lang=ZH-CN><A 
href="file:///G:/application/application.htm" 
target=_parent>返回实践应用</A></FONT>]</P>
<TABLE align=center border=0 width="85%">
  <TBODY>
  <TR>
    <TD height=63 width="4%">&nbsp;</TD>
    <TD height=63 width="92%"> 
      <H2 align=center><font size="4">使用context()方法在一个grid表格中安排一个项目链表</font></H2>
    </TD>
    <TD height=63 width="4%"><a name=top></a></TD>
  </TR>
  <TR>
    <TD colSpan=3 height=50> 
      <P><SPAN class=normal105><SPAN class=normal105>上一篇: <a href="How the DOM Defines the Context for XSL Pattern Queries.htm" tppabs="http://www.xml.org.cn:8188/application/XSL%20uguid/How%20the%20DOM%20Defines%20the%20Context%20for%20XSL%20Pattern%20Queries.htm">DOM如何定义XSL模式语言查询的样式</a> 
        <BR>
        下一篇: <a href="Creating a Comma-Separated List of Items with the context() Method.htm" tppabs="http://www.xml.org.cn:8188/application/XSL%20uguid/Creating%20a%20Comma-Separated%20List%20of%20Items%20with%20the%20context%28%29%20Method.htm">使用context方法生成一个逗号分割的项目链表</a> 
        <BR>
        返 回 : <a href="XSL Patterns.htm" tppabs="http://www.xml.org.cn:8188/application/XSL%20uguid/XSL%20Patterns.htm">XSL模式语言</a></SPAN></SPAN></P>
      <P class="normal105">一个经常需要用到的转换就是把一串项目放到一个栅格或者表格中.例如给出一串产品名称,你可以两格两格地横向把他们放在一个表格中.</P>
      <table border=1>
        <tbody> 
        <tr> 
          <td class="normal105">product 1</td>
          <td class="normal105">product 2</td>
        </tr>
        <tr> 
          <td class="normal105">product 3</td>
          <td class="normal105">product 4</td>
        </tr>
        </tbody> 
      </table>
      <p class="normal105">项目需要一对一对的在表格中分组配对.Xsl当前还没有一种内建机制来操纵任意的分组操作.带有<b>expr</b>脚本出口<b>的&lt;xsl:if&gt;</b>元素和<b>context</b> 
        方法的结合方式能近似的用于实现这个操作.下面的样板把一串&quot;product&quot;元素放入一个两列的表格.</p>
      <pre class=clsCode><font size="2" face="Arial, Helvetica, sans-serif" class="normal105">&lt;TABLE BORDER="1"&gt;
  &lt;xsl:for-each select="products/product"&gt;
    &lt;xsl:if expr="(childNumber(this) % 2) == 1"&gt;
      &lt;TR&gt;
        &lt;TD&gt;&lt;xsl:value-of /&gt;&lt;/TD&gt;
        &lt;TD&gt;&lt;xsl:value-of select="../product[index() $gt$ context()!index()][0]"/&gt;&lt;/TD&gt;
      &lt;/TR&gt;
    &lt;/xsl:if&gt;
  &lt;/xsl:for-each&gt;
&lt;/TABLE&gt;</font></pre>
      <p class="normal105">他是如何工作的呢?首先,在遍历product元素时使用带脚本"childNumber(this) % 
        2"的 <b>&lt;xsl:if&gt;</b>来<font color="#000000">把那些会从新一行开始的product分离出来.这种product的名称也被放置在这一行的第一个字段.然后你</font>生成另一个字段并且在其中放置被<b>&lt;xsl:if&gt;</b> 
        过滤掉的下一个子元素.选择下个元素的查询导航到父元素并且选择比当前节点的索引值大的product节点来得到下面一系列兄弟节点.通过选择紧接当前节点的下一个元素的&quot;[0]&quot;子脚本([0]意思是取查询结果的第一个值)来整理这个集合.</p>
      <p class="normal105"> 由于context()提供了访问有前一个查询所查询到的节点集的方法<b>,</b>所以<b>context()</b> 
        方法是这个查询的关键. 一个优化参数提供了访问通向当前查询的执行结果的完全路径.正数会从这个转换的根往下遍历路径,这是通过context(0)指定的;负数会沿着当前的查询往上遍历.缺省值是-1,他指定了通过前一个查询而选择到的节点.这个节点当然也提供了当前查询的上下文.</p>
      <p class="normal105">三行式的表格能通过在该行中再加入一个列来得到另一个兄弟元素,以及还必须调整这个异常来允许每三元素生成一个新行.你能通过这个机制以此类推来生成更多的数据列的表.</p>
      <pre class=clsCode><font color="#000000" face="Arial, Helvetica, sans-serif" size="2" class="normal105">&lt;TABLE BORDER="1"&gt;
  &lt;xsl:for-each select="products/product"&gt;
    &lt;xsl:if expr="(childNumber(this) % 3) == 1"&gt;
      &lt;TR&gt;
        &lt;TD&gt;&lt;xsl:value-of /&gt;&lt;/TD&gt;
        &lt;TD&gt;&lt;xsl:value-of select="../product[index() $gt$ context()!index()][0]"/&gt;&lt;/TD&gt;
        &lt;TD&gt;&lt;xsl:value-of select="../product[index() $gt$ context()!index()][1]"/&gt;&lt;/TD&gt;
      &lt;/TR&gt;
    &lt;/xsl:if&gt;
  &lt;/xsl:for-each&gt;
&lt;/TABLE&gt;</font></pre>
      <p class="normal105">注意:由于异常使用了<b>childNumber</b> 方法,他给出了相对于在XMl源文档的其他元素的号码,你不可以同时为这一串项目排序以及使用上面的机制来把他们插入一个表格中.</p>
      <p><b><span class="normal105">尝试!</span></b><span class="normal105"> 在<a 
href="../../../msdn.microsoft.com/xml/samples/product-list/product-list.xml" tppabs="http://msdn.microsoft.com/xml/samples/product-list/product-list.xml">Product 
        List (Table) Sample</a>中使用两行或者三行的方式来浏览这一串产品数据.</span></p>
      <p><a 
href="../../../msdn.microsoft.com/xml/samples/product-list/product-list.zip" tppabs="http://msdn.microsoft.com/xml/samples/product-list/product-list.zip"><span class="normal105"><img 
alt=Download border=0 height=20 
src="Placing a List of Items into a Grid with the context() Method.files/icodownl.gif" tppabs="http://www.xml.org.cn:8188/application/XSL%20uguid/Placing%20a%20List%20of%20Items%20into%20a%20Grid%20with%20the%20context%28%29%20Method.files/icodownl.gif" 
width=16> Download this sample</span></a><span class="normal105">. </span></p>
</TD></TR>
  <TR>
    <TD colSpan=3 height=2><FONT face=宋体 lang=ZH-CN 
      size=3></FONT><REV></REV><FONT face=宋体 lang=ZH-CN size=3></FONT> 
      <HR SIZE=1>

      <P align=center><FONT face=宋体 lang=ZH-CN>[<A 
      href="file:///G:/application/XSL%20uguid/Authoring%20Well-Formed%20HTML.htm#top">返回标题</A>][<A 
      href="file:///G:/application/application.htm" target=_parent>返回实践应用</A>][<A 
      href="file:///G:/default.asp" target=_parent>返回首页</A>][<A 
      href="mailto:xmlteam@egroups.com">欢迎投稿</A>]</FONT></P>
      <P align=center>最后更新:1999年10月28日<BR>
        <FONT color=#ff0033 
      size=-1>本站点所刊文章版权,均归本站点所有。署名作者拥有其著作权。<BR>
        未经</FONT><FONT color=#ff0033><A 
      href="mailto:xmlteam@egroups.com"><FONT color=#6666ff 
      size=-1>允许</FONT></A></FONT><FONT color=#ff0033 
      size=-1>,不得随意转载,违者必究!!</FONT> </P>
    </TD></TR></TBODY></TABLE>
<H2 align=center>&nbsp;</H2><FONT face=宋体 lang=ZH-CN size=3>
<P align=justify>&nbsp;</P>
</FONT>
</BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -