2

我正在尝试根据对图形的引用来确定当前章节中包含的图形编号。

要求:

  • 每个章节的图号都应该重新设置。
  • 图参考,<figure_reference>,可能出现在任何深度。
  • XSLT 1.0

XML:

<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:

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

</xsl:template>

当前计数结果:8

期望的计数结果:6

4

2 回答 2

4

试试这个模板:

  <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>
于 2012-05-02T18:25:34.040 回答
2

另一种方法不需要计算出复杂的 XPath 表达式——通过使用<xsl:number>

<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>

当此转换应用于提供的 XML 文档时:

<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
于 2012-05-03T01:41:16.110 回答