Xml xslt xpath+多样式表问题
我正在为学校创建一个选择题程序。为此,我必须编写一个xslt样式表来显示正确的答案 我的XML具有以下结构Xml xslt xpath+多样式表问题,xml,xslt,xpath,Xml,Xslt,Xpath,我正在为学校创建一个选择题程序。为此,我必须编写一个xslt样式表来显示正确的答案 我的XML具有以下结构 <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE QUIZ SYSTEM "quiz.dtd"> <?xml-stylesheet type="text/xsl" href="quizanswers.xsl"?> <quiz> <multipleChoice solution="3">
<?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>
使用以下xslt文件
<?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>
我想在问题[attribute of solution]中设置属性solution中的数字。我做到了,但没有成功。有人对这个问题提出了解决方案/建议吗
我还希望这个xml文件有多个样式表。。。可能吗
提前感谢…是的,您可以使用多个样式表,方法如下所示:
<xsl:include href="mutiple.xsl"/>
看起来你应该想要答案而不是问题?所以您的XSLT应该是
最终版本
在DevNull提出有用的意见后,我同意这是最干净的解决方案
<?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>
试试这个:
<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>
最后两个模板会忽略所有答案,除非答案节点在答案节点中的位置仅等于其父节点的@solution属性。我认为这个答案有两个问题。1格式数字返回一个字符串,因此您无法获得预期的返回值。2您需要从父多回音ICE获取@solution。你的选择应该是这样的:select=answer[number../@solution]更新后的注释:我仍然会使用你的第一个样式表并修改你的选择。它比创建变量imo更简洁。刚才注意到根元素是小写的,但doctype声明是大写的。您应该更改doctype以匹配根元素的大小写。