xslt - How to get the nearest ancestor or child of an ancestor with xpath -
look @ <bookmark/>. desired output want id value 5
<xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/> gives me 4
<?xml version="1.0" encoding="utf-8"?> <content> <section id="1"> <section id="2"/> <section id="3"/> <section id="9"/> </section> <section id="4"> <section> <section id="10"/> <section id="5"/> <section> <bookmark/> <section id="6"> <section id="7"> <section id="8"/> </section> </section> </section> </section> </section> </content>
the way understand question , provided xml document nearest id attribute can happen on ancestor (section) element.
in such case expressions using ony preceding:: axis (as specified in other answers question) don't select node.
one correct xpath expression, selects wanted id attribute is:
(//bookmark/ancestor::*[@id][1]/@id | //bookmark/preceding::*[@id][1]/@id ) [last()] if id attribute allowed on bookmark element itself, above xpath expression needs modified accomodate this:
(//bookmark/ancestor-or-self::*[@id][1]/@id | //bookmark/preceding::*[@id][1]/@id ) [last()] do note: ancestor:: , preceding:: axes not intersect (do not overlap).
Comments
Post a Comment