First, example code
XML:
<quiz>
<question narrative="1">
<text>This is a question.</text>
<ref>104</ref>
</question>
<question narrative="3">
<text>This is a question.</text>
<ref>098</ref>
</question>
<question narrative="8">
<text>This is a question.</text>
<ref>112</ref>
</question>
</quiz>
XSLT:
<xsl:template name="quiz">
<xsl:for-each select="question">
<xsl:sort select="ref"/>
Previous Narrative:
<xsl:value-of select="/quiz/question[position() - 1]/@narrative" />
</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 "Current Narrative:" followed by the attribute "narrative" 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 "question" 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
XML:
<quiz>
<question narrative="1">
<text>This is a question.</text>
<ref>104</ref>
</question>
<question narrative="3">
<text>This is a question.</text>
<ref>098</ref>
</question>
<question narrative="8">
<text>This is a question.</text>
<ref>112</ref>
</question>
</quiz>
XSLT:
<xsl:template name="quiz">
<xsl:for-each select="question">
<xsl:sort select="ref"/>
Previous Narrative:
<xsl:value-of select="/quiz/question[position() - 1]/@narrative" />
</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 "Current Narrative:" followed by the attribute "narrative" 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 "question" 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