我正在尝试使用xslt打印平均值,并且很难找到方法。 我在单独的节点中有答案,在单独的节点中有问题。需要将问题ID与答案进行匹配并计算平均值
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<details>
<detail>
<answersall>
<answers>
<question id="1"/>
<answer>4</answer>
<note>test</note>
</answers>
<answers>
<question id="2"/>
<answer>2</answer>
</answers>
<answers>
<question id="3"/>
<answer>2</answer>
</answers>
<answers>
<question id="4"/>
<answer>3</answer>
</answers>
</answerall>
</detail>
<questions>
<question id="1" text="Hello how are you" section="a"/>
<question id="2" text="how was your day" section="a"/>
<question id="3" text="it was good" section="b"/>
<question id="4" text="take care" section="b"/>
</questions>
</details>
我正在尝试打印这样的东西
Question Answer
Section A 3 (average)
1. Hello how are you 4
2.How was your day 2
Section B 2.5
3. It was good 2
4. Take Care 3
我有部分打印问题和答案,但不是平均值。我知道我不能在for循环中使用变量。
下面是我的xslt。
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<h2><center>Details</center></h2>
<table>
<tr>
<td>Section A</td>
<td>????</td>
</tr>
<xsl:for-each select="details/questions/question[@section='a']">
<xsl:variable name="id" select="@id"/>
<tr>
<td><xsl:value-of select="@text"/></td>
<td><xsl:value-of select="//answers[question/@id=$id]/answer"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
答案 0 :(得分:2)
以下是您可以用作起点的内容:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="question-by-section" match="question" use="@section" />
<xsl:key name="answer-by-question" match="answers" use="question/@id" />
<xsl:template match="/">
<table border="1">
<tr>
<th>Question</th>
<th>Answer</th>
</tr>
<xsl:call-template name="generate-section">
<xsl:with-param name="section">a</xsl:with-param>
</xsl:call-template>
<xsl:call-template name="generate-section">
<xsl:with-param name="section">b</xsl:with-param>
</xsl:call-template>
</table>
</xsl:template>
<xsl:template name="generate-section">
<xsl:param name="section"/>
<xsl:variable name="questions" select="key('question-by-section', $section)" />
<xsl:variable name="answers" select="key('answer-by-question', $questions/@id)" />
<tr>
<th>
<xsl:value-of select="concat('Section ', $section)" />
</th>
<th>
<xsl:value-of select="sum($answers/answer) div count($answers)" />
</th>
</tr>
<xsl:for-each select="$questions">
<tr>
<td><xsl:value-of select="@text"/></td>
<td><xsl:value-of select="key('answer-by-question', @id)/answer"/></td>
</tr>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于您的输入(在纠正answersall
vs answerall
不匹配后!),结果将如下所示:
我强烈建议您采用Muenchian grouping来选择不同的部分,而不是对此处显示的命名模板进行硬编码的重复调用。