marionetek
marionetek

Reputation: 13

XSLT count all sibling elements before empty node

This is a skeleton of the type of XML files I am working on.

<table><tr>
<td>x</td>
<td>y</td>
<td>q</td>
<td>z</td>
<td></td>
<td>o</td>
</tr></table>

I want to count all sibling elements before first empty node. In example I want 4 to be returned from the element above.

I tried <xsl:variable name="NumFields" select="count(preceding-sibling::table/tr[1]/td[.=''])" /> but it returns 0.

What am I doing wrong?

Thank you.

Upvotes: 1

Views: 361

Answers (2)

user3681005
user3681005

Reputation: 136

Find you answer code in below

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="2.0">
    <xsl:template match="/">
        <xsl:value-of select="count(//tr[1]//td[. = ''][1]/preceding::td)"/>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Tim C
Tim C

Reputation: 70618

Assuming you are positioned on the document node (or rather, the parent of the table node), the expression you want is this...

<xsl:value-of select="count(table/tr[1]/td[.=''][1]/preceding-sibling::*)" />

If you are positioned on the table node though, just shorten it to this...

<xsl:value-of select="count(tr[1]/td[.=''][1]/preceding-sibling::*)" />

Upvotes: 3

Related Questions