Krptodr
Krptodr

Reputation: 139

XPATH help XSLT 1.0

I need help getting the count of preceding parent elements for INVOICES_ROW.

The XML follows this set up:

<ROOT>
   <INVOICES>
      <INVOICES_ROW>
      </INVOICES_ROW>
   </INVOICES>
</ROOT>

The XPATH I am trying is:

<!-- Returns 2 for the first INVOICES_ROW and jumps to 4 for the next but then proceeds with incrementing by 1 anytime after. -->
count(preceding::*/ancestor::*/position())

<!-- Returns 1 for the first INVOICES_ROW and then jumps to 3 for the next one, but then proceeds with incrementing by 1. -->
count(preceding::*/parent::*/position())

<!-- Same thing happens with position() removed from either XPath Expression -->

Upvotes: 0

Views: 52

Answers (1)

Linga Murthy C S
Linga Murthy C S

Reputation: 5432

If you want to count all preceding INVOICES_ROW, use this:

count(preceding::INVOICES_ROW)

E.g., For the following XML:

<ROOT>
<INVOICES>
    <INVOICES_ROW/>
    <INVOICES_ROW/>
</INVOICES>
</ROOT>

This XSLT will produce output as "1":

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output method="text"/>

<xsl:template match="/">
    <xsl:value-of select="count(ROOT/INVOICES/INVOICES_ROW[2]/preceding::INVOICES_ROW)"/>
</xsl:template>
</xsl:stylesheet>

Upvotes: 1

Related Questions