Xml 从引用元素中,获取祖先中前面元素出现的次数

Xml 从引用元素中,获取祖先中前面元素出现的次数,xml,xslt,xpath,xslt-1.0,Xml,Xslt,Xpath,Xslt 1.0,我正试图从对图形的引用中确定当前章节中包含的图形编号 要求: 每个章节的图形编号应重置 地物参考,,可能出现在任何深度 XSLT1.0 XML: 图形 图形 图形 图形 图形 图形 图形 图形 XSL: 当前计数结果: 八, 所需计数结果: 6尝试此模板: <xsl:template match="figure_reference"> <xsl:value-of select="count(ancestor::chapter//figure[@id=cu

我正试图从对图形的引用中确定当前章节中包含的图形编号

要求:

  • 每个章节的图形编号应重置
  • 地物参考,
    ,可能出现在任何深度
  • XSLT1.0
XML:


图形
图形
图形
图形
图形
图形
图形
图形
XSL:


当前计数结果: 八,

所需计数结果: 6

尝试此模板:

  <xsl:template match="figure_reference">
    <xsl:value-of select="count(ancestor::chapter//figure[@id=current()/@id]/preceding::figure[ancestor::chapter = current()/ancestor::chapter])+1"/>      
  </xsl:template>

另一种方法不需要计算出复杂的XPath表达式——使用

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kFigById" match="figure" use="@id"/>

 <xsl:template match="figure_reference">

  <xsl:for-each select="key('kFigById', @id)">
      <xsl:number level="any" count="chapter//figure"
                from="chapter"/>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>
<top>
    <chapter>
        <dmodule>
            <paragraph>
                <figure>figure</figure>
            </paragraph>
            <figure>figure</figure>
        </dmodule>
    </chapter>
    <chapter>
        <dmodule>
            <figure>figure</figure>
            <paragraph>
                <figure>figure</figure>
            </paragraph>
        </dmodule>
        <dmodule>
            <figure>figure</figure>
            <paragraph>
                <figure>figure</figure>
                <paragraph>
                    <figure>figure</figure>
                </paragraph>
            </paragraph>
            <figure_reference id="c"/>
            <figure id="c">figure</figure>
        </dmodule>
    </chapter>
</top>
<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:key name="kFigById" match="figure" use="@id"/>

 <xsl:template match="figure_reference">

  <xsl:for-each select="key('kFigById', @id)">
      <xsl:number level="any" count="chapter//figure"
                from="chapter"/>
  </xsl:for-each>
 </xsl:template>

 <xsl:template match="text()"/>
</xsl:stylesheet>
<top>
    <chapter>
        <dmodule>
            <paragraph>
                <figure>figure</figure>
            </paragraph>
            <figure>figure</figure>
        </dmodule>
    </chapter>
    <chapter>
        <dmodule>
            <figure>figure</figure>
            <paragraph>
                <figure>figure</figure>
            </paragraph>
        </dmodule>
        <dmodule>
            <figure>figure</figure>
            <paragraph>
                <figure>figure</figure>
                <paragraph>
                    <figure>figure</figure>
                </paragraph>
            </paragraph>
            <figure_reference id="c"/>
            <figure id="c">figure</figure>
        </dmodule>
    </chapter>
</top>
6