Re: [xsl] select all answers belongs to one question from xml with xpath

Subject: Re: [xsl] select all answers belongs to one question from xml with xpath
From: "Joris Gillis" <roac@xxxxxxxxxx>
Date: Thu, 30 Jun 2005 22:08:29 +0200
Hi,

Tempore 20:36:25, die 06/30/2005 AD, hinc in xsl-list@xxxxxxxxxxxxxxxxxxxxxx scripsit <04083259@xxxxxxxxxxxxx>:

i have a set of questions and for each question a  set of answers (4
answers )stored in xml file as each each answer has an attribute as a
reference to the question that it belogs to as following :

You're probably stuck with an xpath because you're missing the 'current()' function.
But this situation (nodes connected with references) it is common to use keys.


here's a stylesheet demonstrating two methods:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>


<xsl:output method="xml" indent="yes"/>

<xsl:key name="answer" match="answer" use="@question"/>

<xsl:template match="*[ques]">
	<h1>My questions</h1>
	<xsl:apply-templates select="ques"/>
</xsl:template>

<xsl:template match="question">
	<h2><xsl:apply-templates/></h2>
	key method:
	<ol>
		<xsl:for-each select="key('answer',@id)">
			<li><xsl:apply-templates/></li>
		</xsl:for-each>
	</ol>
	xpath method:
	<ol>
		<xsl:for-each select="//answer[@question=current()/@id]">
			<li><xsl:apply-templates/></li>
		</xsl:for-each>
	</ol>
</xsl:template>

</xsl:stylesheet>


regards, -- Joris Gillis (http://users.telenet.be/root-jg/me.html) Ceterum censeo XML omnibus esse utendum

Current Thread