Eric
Eric

Reputation: 23

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:

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

Upvotes: 2

Views: 2457

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243449

Another way to do this doesn't require to fugure out complicated XPath expressions -- by using <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>

When this transformation is applied to the provided XML document:

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

the wanted, correct result is produced:

6

Upvotes: 2

Daniel Haley
Daniel Haley

Reputation: 52858

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>

Upvotes: 5

Related Questions