View Full Version : XPath to get the nearest preceding sibling - howto?
senglory
11-18-2009, 11:30 PM
I need to write a XPath for getting the closest sibling to the current node. It should return exactly 1 element. How to write it?
oesxyl
11-19-2009, 01:03 AM
I need to write a XPath for getting the closest sibling to the current node. It should return exactly 1 element. How to write it?
post your code please
best regards
senglory
11-19-2009, 01:23 AM
.....
<w:p XID="5">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>Demo 3</w:t>
</w:r>
</w:p>
....
<w:p XID="25">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="1"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>Doberman</w:t>
</w:r>
</w:p>
Attributes XID are not in the original XML. I put'em here for clarity.
The proposed XPath should point me at <w:p XID="5"> in this particular case. Anyway, it should point at the neares <w:p> with w:ilvl = w:ilvl for the current node +1 and the same value for w:numId.
oesxyl
11-19-2009, 02:55 AM
.....
<w:p XID="5">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="0"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>Demo 3</w:t>
</w:r>
</w:p>
....
<w:p XID="25">
<w:pPr>
<w:pStyle w:val="ListParagraph"/>
<w:numPr>
<w:ilvl w:val="1"/>
<w:numId w:val="2"/>
</w:numPr>
</w:pPr>
<w:r>
<w:t>Doberman</w:t>
</w:r>
</w:p>
Attributes XID are not in the original XML. I put'em here for clarity.
The proposed XPath should point me at <w:p XID="5"> in this particular case. Anyway, it should point at the neares <w:p> with w:ilvl = w:ilvl for the current node +1 and the same value for w:numId.
I don't understand. Do you want to select first node who's name is w:p from whole file? in this case xpath expr will be: //w.p[1]
this will select w:p which have XID='5' if w:p is first w:p from the begining of the file
if selection is based on a condition //w.p[w:pPr/w:numPr/w:ilvl/@w:val = '0']
will select w:p with XID='5' if you don't have any other w:p which match the condition else will return a node set.
you can do same thing but using w:numId in condition.
this w:ilvl = w:ilvl, wont work, one of them must be a variable
best regards
senglory
11-19-2009, 03:50 AM
No, it's not necessary the first w:p node in the file. I'd like to select the closest node to the current w:p
oesxyl
11-19-2009, 03:58 AM
No, it's not necessary the first w:p node in the file. I'd like to select the closest node to the current w:p
what do you mean by "current w: p"? all are current.
when you apply a template xslt will search each node. When you add a condition to the expression, the node set will be filtered against that condition.
best regards
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.