Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XPath help?

Status
Not open for further replies.

Muddmuse

Programmer
Jul 31, 2001
85
US
Here is some sample XML:

<?xml version=&quot;1.0&quot;?>
<Data>
<LineItems>
<LineItem>
<A>1</A>
<B>2</B>
</LineItem>
<LineItem>
<A>3</A>
<B>4</B>
<C>5</C>
<Z>X</Z>
</LineItem>
</LineItems>
</Data>

I'm have a template to match &quot;LineItems&quot; and in it am doing a for-each loop on &quot;LineItem&quot;. My problem is this. If a LineItem node has a child of Z I need to know the count of that LineItem child nodes, excluding Z (In the above example the answer would be 3).

Can anyone assist me in creating this expression? Thanks in advance.
 
sure i'll give it a shot

in xpath you have the ability to search for child nodes using the &quot;child::&quot; expression. for example to test if a node has the child node Z you would do:

<xsl:if test=&quot;child::Z&quot;>
</xsl:if>

...

as a brief and very unsophisticated answer to your problem i have written this:

<?xml version=&quot;1.0&quot;?>
<xsl:stylesheet version=&quot;1.0&quot; xmlns:xsl=&quot;<xsl:template match=&quot;Data&quot;>
<xsl:for-each select=&quot;//LineItem&quot;>
<xsl:if test=&quot;child::Z&quot;>
<!-- do something here -->
<xsl:value-of select=&quot;count(*) - count(child::Z)&quot;/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

... I hope you can see what I am trying to do.

Hope this helps :)

Matt
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top