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>

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

Need Your Help

Is there a way to handle catch exception in MSI install wizard?

exception-handling wix windows-installer

I am facing some odd situation that when I select/deselect the features in the feature customization wizard, it sometimes crashes the window. It does not give any clue at all. It also not happens

if an image isnt a background how do i make it fit it's container

html css image css3

Iv found an image slider the url is below. the image has to be a certain height + width as

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.