[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: how to get the position of first special element?

Subject: Re: how to get the position of first special element?
From: "Joe Fawcett" <joefawcett@xxxxxxxxxxx>
Date: Sun, 6 Jun 2004 11:53:01 +0100
xsl position first
----- Original Message ----- From: "Chen Yi" <chen_yi36@xxxxxxxxxxx>
To: <xsl-list@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, June 06, 2004 10:06 AM
Subject: how to get the position of first special element?



ALL,

For example,
<element>
<A/>
<B/>
<A/>
<C/>
<C/>
<D/>
</element>

I want to get the position first element whoes name is not A and B, but C & D. So it's 4 for this tree segment.
How can I write the xslt to figure out this ?


Thanks,
Chen Yi

This will do but I'm sure there's a more elegant solution:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count((element/*[name() = 'C' or name() = 'D'])[1]/preceding-sibling::*) + 1"/>
</xsl:template>
</xsl:stylesheet>


One trap, if there aren't any matches this will give the wrong result, you may wish to test for any matches first:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="CDnode" select="(element/*[name() = 'C' or name() = 'D'])[1]"/>
<xsl:choose>
<xsl:when test="$CDnode">
<xsl:value-of select="count($CDnode/preceding-sibling::*) + 1"/>
</xsl:when>
<xsl:otherwise>
No matches
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>



--


Joe (MVP - xml)

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.