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

Determine position in node sequence based on criteria

Subject: Determine position in node sequence based on criteria
From: "Houghton,Andrew" <houghtoa@xxxxxxxx>
Date: Tue, 29 Jun 2010 19:13:27 -0400
 Determine position in node sequence based on criteria
I'm have a problem trying to come up with an XPath expression that will
determine the position of a node in a node sequence (terminology?) based on
some criteria. I have some XML that looks like:


I have a template rule which is at one of the <s> nodes, e.g.:

<xsl:template match="s[@c = 'c']" />

inside that template I'm trying to figure out which other <s> ends in a
parenthical expression if and only if an attribute has a certain value. So I
started out with this:

<xsl:template match="s[@c = 'c']">
  <xsl:variable name="re" as="xsd:string" select="'(\([^)]+\))\s*$'" />
  <xsl:variable name="pos" as="xsd:integer*">
    <xsl:for-each select="../s">
      <xsl:if test="matches(@c, '^[ab]$') and matches(., $re)">
        <xsl:sequence select="position()" />

Great this does exactly what I want, but it seems to me that I should be able
to construct an XPath expression and use a select attribute on the $pos
variable. So given the XML:

    <s c="a">123</s>
    <s c="b">456 (abc)</s>
    <s c="c" />

I should be able to construct an equivalent XPath expression that would give
me a position of 2. My first attempt was:

../s[matches(@c, '^[ab]$') and matches(., $re)]/position()

obviously wrong since it will always return 1, given the above XML, e.g., I
need the position() in ../s not the position() of the resulting nodeset from
the match criteria. So right now I'm stumpped out of ideas in how to construct
the XPath to give me the position. Anybody have any suggestions to offer...

Thanks, Andy.

Current Thread


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.
First Name
Last Name
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.