XSL 1.0 <xsl:for-each>和<call-template> </call-template> </xsl:for-each>的问题

时间:2009-06-11 07:52:25

标签: xslt

请查看我的代码并就此提出建议:

XML文件:content.xml

<content>
<page id="page-1">
<!-- ... -->
<block-center>
  <block-center-row id="block-center-row-1">
    <cd>
      <title>Empire Burlesque</title>
      <artist>Bob Dylan</artist>
      <country>USA</country>
      <company>Columbia</company>
      <price>10.90</price>
      <year>1985</year>
    </cd>
    <cd>
      <title>Hide your heart</title>
      <artist>Bonnie Tyler</artist>
      <country>UK</country>
      <company>CBS Records</company>
      <price>9.90</price>
      <year>1988</year>
    </cd>
  </block-center-row>
  <block-center-row id="block-center-row-2">
    <block-center-colunm id="block-center-2-1">
      <book>
        <title>Book Title1</title>
        <author>Book Author1</author>
      </book>
      <book>
        <title>Book Title2</title>
        <author>Book Author2</author>
      </book>
      <book>
        <title>Book Title3</title>
        <author>Book Author3</author>
      </book>
    </block-center-colunm>
    <block-center-colunm id="block-center-2-2">
      <seminar>
        <author>Seminar author1</author>
        <durable>3</durable>
      </seminar>
      <seminar>
        <author>Seminar author2</author>
        <durable>1.5</durable>
      </seminar>
      <seminar>
        <author>Seminar author3</author>
        <durable>2</durable>
      </seminar>
      <seminar>
        <author>Seminar author4</author>
        <durable>3</durable>
      </seminar>
    </block-center-colunm>
  </block-center-row>
</block-center>
</page>
<!-- ... -->
</content>

XSL文件:block-center-1-1.xsl

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

  <xsl:template name="block-center-row-1">
    <div class="block-center-row">
      <h2>My CD Collection</h2>
      <table border="1" width="100%">
        <tr bgcolor="#9acd32">
          <th>Title</th>
          <th>Artist</th>
        </tr>
        <xsl:for-each select="
          content/page[@id=$pageId]/block-center/
          block-center-row[@id='block-center-row-1']/cd
        ">
          <tr>
            <td>
              <xsl:value-of select="title" />
            </td>
            <xsl:choose>
              <xsl:when test="price &gt; 10">
                <td bgcolor="#ff00ff">
                  <xsl:value-of select="artist" />
                </td>
              </xsl:when>
              <xsl:when test="price &gt; 9">
                <td bgcolor="#cccccc">
                  <xsl:value-of select="artist" />
                </td>
              </xsl:when>
              <xsl:otherwise>
                <td>
                  <xsl:value-of select="artist" />
                </td>
              </xsl:otherwise>
            </xsl:choose>
          </tr>
        </xsl:for-each>
      </table>
    </div>
  </xsl:template>
</xsl:stylesheet>

XSL文件block-center.xsl

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:import href="block-center-1-1.xsl" />
  <xsl:template name="block-center">
    <!-- if put here. It work properly -->
    <div class="block-center"> 
      <xsl:for-each select="content/page[@id=$pageId]/block-center/block-center-row">
        <xsl:choose>
          <!-- // I does not work here --> 
          <xsl:when test="@id='block-center-row-1'">
            <xsl:call-template name="block-center-row-1" />
          </xsl:when>
        </xsl:choose>
      </xsl:for-each>
    </div>
  </xsl:template>
</xsl:stylesheet>

如果我在<xsl:call-template>循环内调用<xsl:for-each>,我不知道为什么它不起作用(不输出数据)。否则,没关系。

3 个答案:

答案 0 :(得分:2)

我有几点要说。首先,在XML中制作格式含义是一个坏主意。拥有被称为<block-center><block-center-colunm>的元素不仅不必要地冗长,一旦它们的内容不再显示在中心的区块中,它也会使你的头部受伤。

其次,放开<xsl:call-template><xsl:for-each>。如果你有程序编程背景,它们似乎很方便,但它们是错误的选择。使用<xsl:apply-templates>代替,它会使代码更清晰,更易于理解。

现在到你的XSL。你的第二个XSL(block-center.xsl) - 它做了什么?为什么你有两个单独的XSL文件?此外,它缺少变量或参数声明。我用过:

<xsl:variable name="pageId" select="'page-1'" />

我的测试。它也只有一个永远不会被调用的模板(<xsl:template name="block-center">),所以它对我没有任何作用。我添加了

<xsl:template match="/">
  <xsl:call-template name="block-center" />
</xsl:template>

所以它至少会做任何事情。

您的其他XSL文件(block-center-1-1.xsl)有一个for-each循环,没有查看正确的上下文。当您输入模板"block-center-row-1“:

时,您已处于以下上下文中
content/page[@id=$pageId]/block-center/block-center-row

所以你需要在for-each循环中做的就是:

<xsl:for-each select="cd">

它将开始输出所有<cd>个节点。

答案 1 :(得分:0)

也许改变:

<xsl:for-each select="content/page[@id=$PageId]/block-center/block-center-row">

<xsl:for-each select="/content/page[@id=$PageId]/block-center/block-center-row">

我猜它找不到内容节点,因为它从另一个节点内调用模板。

答案 2 :(得分:0)

非常感谢您的真挚敬意! 如果我更改block-center-1-1.xsl文件,它现在可以正常工作 至

OR