22

Here are three XML-trees

(1)

<?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 id="5">
            <section>
                <bookmark/> <!-- here's the bookmark-->
                <section id="6">
                    <section id="7">
                        <section id="8"/>
                    </section>
               </section>
            </section>
        </section>
    </section>
</content>

(2)

<?xml version="1.0" encoding="UTF-8"?>
<content>
    <section id="1"/>
    <section id="2">
        <section id="9">
            <section id="10">
                <section id="11"/>
            </section>            
        </section>
        <section>
            <section id="4">
                <section id="5"/>
            </section>            
        </section>
        <section/>
        <bookmark/> <!-- here's the bookmark-->
        <section id="6">
            <section id="7">
                <section id="8"/>
            </section>
        </section>
    </section>
</content>

The desired result is in both cases the id 5.

With XSLT 1.0 and XPath 1.0 I can either get the ancestor from (1)

<xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/>

or the preceding node from (2)

<xsl:value-of select="//bookmark/preceding::*[@id][1]/@id"/>

How do I get the nearest ancestor or preceding node with an id from my bookmark?
I need a single xsl:value-of which matches both cases. Thanks.

EDIT:

The solution should also cover this structure. Desired id is still 5.

(3)

<?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/> <!-- here's the bookmark-->
                <section id="6">
                    <section id="7">
                        <section id="8"/>
                    </section>
                </section>
            </section>
        </section>
    </section>
</content>
2
  • 8
    Picture which shows all xpath axes: pic Jul 1, 2011 at 23:49
  • 1
    Good question, +1. See my answer for a one-liner XPath expression that selects exactly the wanted attribute node in all three cases. :) Jul 2, 2011 at 2:59

2 Answers 2

26

Use:

    (//bookmark/ancestor::*[@id][1]/@id 
| 
    //bookmark/preceding::*[@id][1]/@id
     )
     [last()]

Verification: Using XSLT as host of XPath, the following transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:template match="/">
  <xsl:value-of select=
  "(//bookmark/ancestor::*[@id][1]/@id
  |
   //bookmark/preceding::*[@id][1]/@id
   )
    [last()]"/>
 </xsl:template>
</xsl:stylesheet>

when applied on any of the provided three XML documents, produces the wanted, correct result:

5

I strongly recomment using the XPath Visualizer for playing with / learning XPath.

0
3

Try with :

<xsl:value-of 
    select="//bookmark/ancestor::*[1]/descendant-or-self::*[last()-1]/@id"/>

It returns 5 for both XML documents.

EDIT:

In such conditions you could use simple xsl:choose:

<xsl:variable name="lastSibling"
    select="//bookmark/preceding-sibling::*[1]"/>
<xsl:choose>
    <xsl:when test="$lastSibling">
        <xsl:value-of
            select="$lastSibling/descendant-or-self::*[last()]/@id"/>
    </xsl:when>
    <xsl:otherwise>
        <xsl:value-of select="//bookmark/ancestor::*[@id][1]/@id"/>
    </xsl:otherwise>
</xsl:choose>

Another general solution:

<xsl:for-each
    select="//section[following::bookmark or descendant::bookmark][@id]">
    <xsl:if test="position() = last()">
        <xsl:value-of select="./@id"/>
    </xsl:if>
</xsl:for-each>
5
  • Your solution works under special circumstances. But if I add more sections with id after bookmark on both XMLs your solution will give wrong results. Will edit my examples to have some sections after bookmark. Jul 1, 2011 at 23:42
  • @therealmarv: Right. I assumed specific structure. Check my second solution. Jul 2, 2011 at 0:11
  • @therealmarv: corrected last()1 in lastSibling variable. Jul 2, 2011 at 0:24
  • Your solution is a good approach. It gives me some ideas. But it is not a general solution. I edited my examples again. The nearest ancestor is maybe not parent and the id can be somewhere in a child of a preceding node. Jul 2, 2011 at 0:29
  • Wow. Your more general solution works for (1) and (3). Thanks alot! :-) But, it does not work for (2), the solution is empty. Still figuring out what's the reason. Hopefully I can solve it Jul 2, 2011 at 1:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.