Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT - position() and <xsl:sort> together - This one has me stumped :)

Status
Not open for further replies.

Wickersty

Programmer
Nov 13, 2002
51
US
First, example code :)

XML:
<quiz>
<question narrative=&quot;1&quot;>
<text>This is a question.</text>
<ref>104</ref>
</question>
<question narrative=&quot;3&quot;>
<text>This is a question.</text>
<ref>098</ref>
</question>
<question narrative=&quot;8&quot;>
<text>This is a question.</text>
<ref>112</ref>
</question>
</quiz>

XSLT:
<xsl:template name=&quot;quiz&quot;>
<xsl:for-each select=&quot;question&quot;>
<xsl:sort select=&quot;ref&quot;/>
Previous Narrative:
<xsl:value-of select=&quot;/quiz/question[position() - 1]/@narrative&quot; />
</xsl:for-each>
</xsl:template>

Hi everyone. I'm using <xsl:sort> to sort my output, and using position(), and not getting the results I need. Perhaps someone could identify where I've gone wrong here, or if there's another way to accomplish what I'm trying to do. My XML and XSLT were shown above, in simplified form.

What I would THINK would happen is that the XSLT would sort by the number in the REF tag, and output the questions in the following order: the 2nd one, the 3rd one, the 1st one.

The XSLT would be outputting the text &quot;Current Narrative:&quot; followed by the attribute &quot;narrative&quot; value of the PREVIOUS question outputted (hence the position() - 1). However, what winds up happening is the XSLT outputs the questions in the order I expected (2, 3, 1), but still treats position() literally... so when I say position()-1 on the second question outputted (the 3rd question in the XML doc), it says position()=2 because its the second question being outputted, then subtracts 1 from that... winding up with 1. Then, it takes the FIRST &quot;question&quot; node in the XML doc, NOT the question node previous to the one just outputted.

I hope I'm making sense. It's hard to explain. Bottom line is I'm trying to get it so that I can output my XML data in the order I want, but still be able to access an attribute from the <question> node JUST PREVIOUSLY outputted. I would have thought that this would have worked.

Thanks for your help.

Jeff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top