XSLT参数值

时间:2019-04-30 09:44:51

标签: xslt

我想将当前“节”的position()传递给“正文”模板。因此,我将当前position()分配给变量“ sectionPos”。 但是,继续操作时,“节/正文”模板中的参数“ pSectionPos”始终包含数字1。

如果我取消注释模板“ section ...”中的行,则参数“ pSectionPos”正确包含1和2。

xml结构:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <content>
        <section type="__section_table_">
            <body>
                <item>
                </item>
                <item>
                </item>
            </body>
        </section>
        <section type="__section_table_">
            <body>
                <item>
                </item>
                <item>
                </item>
            </body>
        </section>
    </content>
</data>

xslt结构:

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xsl:stylesheet [
<!ENTITY newLine          "&#10;">
<!ENTITY tab              "&#9;">
]>
<xsl:stylesheet version="2.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    >
<xsl:output method='html' standalone="yes" indent="yes" />

<!--root-->
<xsl:template match="/">
  <!DOCTYPE html>
  <html>
      <head>
        <meta charset="UTF-8"/>     
      </head>
      <body>
          <xsl:apply-templates select="//data/content" /> 
          <xsl:text>&newLine;</xsl:text>   
      </body>
  </html>
</xsl:template>

<!--content part-->
<xsl:template match="content">
    <xsl:apply-templates select="section[contains(attribute::type,'__section_table_')]" /> 
</xsl:template>

<!--sections part-->
<xsl:template match="section[contains(attribute::type,'__section_table_')]">    
    <xsl:variable name="sectionPos" as="xs:integer" select="xs:integer(position())" />
    <!--xsl:text>&newLine;</xsl:text>   
    <xsl:text>&tab;&tab;</xsl:text>   
    <xsl:value-of select="$sectionPos" /> 
    <xsl:value-of select="' template-section'"/-->

    <xsl:apply-templates select="body" >
        <xsl:with-param name="pSectionPos" select="$sectionPos" />
    </xsl:apply-templates>
</xsl:template>

<!--section body-->
<xsl:template match="section/body">
    <xsl:param name="pSectionPos" />        
    <xsl:text>&newLine;</xsl:text>   
    <xsl:text>&tab;&tab;</xsl:text>   
    <xsl:value-of select="$pSectionPos"/>
    <xsl:value-of select="' template-body'"/>
</xsl:template>

</xsl:stylesheet>

谢谢。

1 个答案:

答案 0 :(得分:0)

您谈到了当前“部分”的 position()

这似乎暗示了一种误解,因为position()不是节点的属性,而是您当前正在处理的节点列表的属性。

在带有match="section"的模板中,position()的值将是您在由xsl:apply-templates指令选择的节点列表中与之匹配的部分的位置,该节点导致该部分被选中。您尚未向我们展示此说明,这表明您尚未意识到其重要性。

有些人误以为position()返回元素在其同级元素中的位置。如果您要这样做,最好使用xsl:numbercount(preceding-sibling::XXXX)

相关问题