Subject: Re: [xsl] Determine position in node sequence based on criteria From: Dimitre Novatchev <dnovatchev@xxxxxxxxx> Date: Tue, 29 Jun 2010 18:37:26 -0700 |
> 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.. for $item in $seq[someCondition] return index-of($seq, $item) where $sequence is the desired sequence and someCondition is the desired condition. Of course, nothing guarantees that only one positive integer will be returned -- more than one items in the sequence (with the same or different values) may satisfy the condition. Using FXSL one can write this in an even more compact form: f:map(f:index-of($seq), $vInts[f:someCondition(.)]) When there are implementations of XPath 2.1, it would be possible to write an equivalent expression to the one above using standard HOF functions -- I guess that f:map() will have an equivalent in the standard F & O. -- Cheers, Dimitre Novatchev --------------------------------------- Truly great madness cannot be achieved without significant intelligence. --------------------------------------- To invent, you need a good imagination and a pile of junk ------------------------------------- Never fight an inanimate object ------------------------------------- You've achieved success in your field when you don't know whether what you're doing is work or play On Tue, Jun 29, 2010 at 4:13 PM, Houghton,Andrew <houghtoa@xxxxxxxx> wrote: > 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: > > <r> > B <f> > B <s>...</s> > B <s>...</s> > B <s>...</s> > B </f> > </r> > > 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']"> > B <xsl:variable name="re" as="xsd:string" select="'(\([^)]+\))\s*$'" /> > B <xsl:variable name="pos" as="xsd:integer*"> > B B <xsl:for-each select="../s"> > B B B <xsl:if test="matches(@c, '^[ab]$') and matches(., $re)"> > B B B B <xsl:sequence select="position()" /> > B B B </xsl:if> > B B </xsl:for-each> > B </xsl:variable> > </xsl:template> > > 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: > > <r> > B <f> > B B <s c="a">123</s> > B B <s c="b">456 (abc)</s> > B B <s c="c" /> > B </f> > </r> > > 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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Determine position in node se, Houghton,Andrew | Thread | Re: [xsl] Determine position in nod, Dimitre Novatchev |
[xsl] Determine position in node se, Houghton,Andrew | Date | Re: [xsl] Determine position in nod, Dimitre Novatchev |
Month |