tags:

views:

289

answers:

1

Given the schema below - with a "query" node as the context, is it possible with xpath/xslt to determine the index of that "query" node relative to all "query" nodes in the document (not position() relative to its immediate siblings).

TIA,

Geoff

<section>
    <name>About</name>
    <link>about</link>
    <questions>
        <question>
            <query>Question 1</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
    </questions>
</section>

<section>
    <name>Contact</name>
    <link>contact</link>
    <questions>
        <question>
            <query>Question 1</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
        <question>
            <query>Question 2</query>
            <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
        </question>
    </questions>
</section>

+4  A: 

Use:

count(preceding::question)+1

This transformation performs an exhaustive test:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"&gt;
 <xsl:output method="text"/>

 <xsl:variable name="vQs" select="//question"/>

 <xsl:template match="/">
  <xsl:for-each select="$vQs">
    <xsl:value-of select=
    "count(preceding::question)+1"/>
    <xsl:text> </xsl:text>
  </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

When the above transformation is applied on the following XML document:

<t>
    <section>
        <name>About</name>
        <link>about</link>
        <questions>
            <question>
                <query>Question 1</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 2</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
        </questions>
    </section>
    <section>
        <name>Contact</name>
        <link>contact</link>
        <questions>
            <question>
                <query>Question 3</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 4</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
            <question>
                <query>Question 5</query>
                <answer>Lorem ipsum dolor sit amet, consectetur adipiscing elit. In cursus, elit id lacinia semper, ligula nunc rhoncus ante, in euismod tortor nibh dictum tellus.</answer>
            </question>
        </questions>
    </section>
</t>

The wanted results are produced:

1 2 3 4 5 
Dimitre Novatchev
Nice! I didn't know about that. Certainly makes it easier than what I was trying to do.
jwismar
@jwismar, Glad you like the answer. How about expressing this with an upvote? :)
Dimitre Novatchev
Perfect - thank you!
Geoff