From reference element, get count of preceding element occurrences within ancestor

I am trying to determine the figure number contained to the current chapter from the reference to the figure.

Requirements:

  • The Figure number should reset for each chapter.
  • Figure References, <figure_reference>, may occur at any depth.
  • 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>

Current count Results: 8

Desired count result: 6

Answers


Try this template:

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

Need Your Help

Force git to recognize that a file has moved when merging

git

I'm developing a piece of software which is built on top of an open source project but contains some proprietary changes. Recently, there has been a big update to the open source project which has ...

Get $post object of next post

wordpress object post

Is there a way to get $post object of the Next or Previous post in Wordpress?