Reputation: 1422
I'm creating for school a multiple choice program. For this I have to write an xslt stylesheet to show the right answer.
My XML has the following strucure
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE QUIZ SYSTEM "quiz.dtd">
<?xml-stylesheet type="text/xsl" href="quizanswers.xsl"?>
<quiz>
<multipleChoice solution="3">
<question>Question 1</question>
<answer>answer 1</answer>
<answer>answer 2</answer>
<answer>answer 3</answer>
<answer>answer 4</answer>
</multipleChoice>
<multipleChoice solution="4">
<question>Question 1</question>
<answer>answer 1</answer>
<answer>answer 2</answer>
<answer>answer 3</answer>
<answer>answer 4</answer>
</multipleChoice>
</quiz>
With the following xslt file
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Answers</h1>
<xsl:for-each select="quiz/multipleChoice">
<u><br></br><xsl:value-of select="question"/></u><br></br>
- <xsl:value-of select="question[../multipleChoice/@solution]"/> <br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
What i want is to set the number from attribute solution in the question[attribute of solution]. Which I achieved but doesn't work. Has anyone a solution/suggestion for thi problem?
I also want to have this xml file to have multiple stylesheets... Is that possible?
Thanks in advance...
Upvotes: 1
Views: 707
Reputation: 12075
Try this:
<xsl:template match="/">
<html>
<body>
<h1>Answers</h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
<xsl:template match="question">
<u>
<br />
<xsl:apply-templates />
</u>
<br />
</xsl:template>
<xsl:template match="answer" />
<xsl:template match="answer[position() = ../@solution]">
<xsl:text>- </xsl:text>
<xsl:apply-templates />
<br />
</xsl:template>
The last two templates ignore all answers, except where the position of the answer node (among answer nodes only) is equal to the @solution attribute of it's parent.
Upvotes: 1
Reputation: 10754
Yes you can use Multiple stylesheets by including them as follows:
<xsl:include href="mutiple.xsl"/>
It looks like you should be wanting the answer and not the question? So your XSLT should be something like
Final version
After useful comments from DevNull I agree this is the cleanest solution
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h1>Answers</h1>
<xsl:for-each select="quiz/multipleChoice">
<u>
<br></br>
<xsl:value-of select="question"/>
</u>
<br></br>
<br />
<xsl:value-of select="answer[number(../@solution)]" /><br />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
Upvotes: 1