我有一个看起来像这样的XML文档。
<Results>
<Name>Lab Asst1 </Name>
<Subject> Math </Subject>
<Marks>96</Marks>
<Grade>A</Grade>
<Name>Student1</Name>
<Subject>Math</Subject>
<Marks>90</Marks>
<Grade>A</Grade>
<Name>Student1</Name>
<Subject>English</Subject>
<Marks>70</Marks>
<Grade>B</Grade>
<Name>Lab Asst1 </Name>
<Subject> Science</Subject>
<Marks>99</Marks>
<Grade>A</Grade>
<Name>Student2</Name>
<Subject>Science</Subject>
<Marks>70</Marks>
<Grade>B</Grade>
</Results>
使用XSL,最简单的显示方式是什么,而不显示<Name>
元素两次?我想首先显示Lab Asst1结果,假设有一个使用此值的文本框(来自C#代码):
基本上,首先要完成这一部分:
User: <Name>
Your results are:
<table>
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Grade </td>
</tr>
然后调用另一个模板等,或者做一个for-each或者什么......
<tr>
<td> <xsl:value-of select="Subject"/> </td>
<td> <xsl:value-of select="Marks"/> </td>
<td> <xsl:value-of select="Grade"/> </td>
</tr>
这样我的结果就像这样:
User: Lab Asst1
Your results are:
Subject | Marks | Grade
-------------------------------------
Science| 99 | A
Math | 96 | A
User:Student1
Your results are:
Subject | Marks | Grade
-----------------------------
Math | 95 | A
English | 70 | B
User:Student2
Your results are:
Subject | Marks | Grade
-----------------------------
Math | 70 | B
答案 0 :(得分:2)
此转化:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:key name="kPersByName" match="Name" use="."/>
<xsl:key name="kData" match="*"
use="preceding-sibling::Name[1]"/>
<xsl:template match="/*">
<xsl:for-each select=
"Name[generate-id()
=
generate-id(key('kPersByName', .)[1])
]
">
<xsl:variable name="vData" select="key('kData', .)"/>
User: <xsl:value-of select="."/>
Your results are:
<table border="1">
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Grade </td>
</tr>
<xsl:for-each select="$vData[self::Subject]">
<tr>
<td><xsl:value-of select="."/></td>
<td><xsl:value-of select="following-sibling::Marks[1]"/></td>
<td><xsl:value-of select="following-sibling::Grade[1]"/></td>
</tr>
</xsl:for-each>
</table>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
应用于提供的XML文档:
<Results>
<Name>Lab Asst1 </Name>
<Subject> Math </Subject>
<Marks>96</Marks>
<Grade>A</Grade>
<Name>Student1</Name>
<Subject>Math</Subject>
<Marks>90</Marks>
<Grade>A</Grade>
<Name>Student1</Name>
<Subject>English</Subject>
<Marks>70</Marks>
<Grade>B</Grade>
<Name>Lab Asst1 </Name>
<Subject> Science</Subject>
<Marks>99</Marks>
<Grade>A</Grade>
<Name>Student2</Name>
<Subject>Science</Subject>
<Marks>70</Marks>
<Grade>B</Grade>
</Results>
生成想要的正确结果:
User: Lab Asst1
Your results are:
<table border="1"> <tr>
<td> Subject </td>
<td> Marks </td>
<td> Grade </td>
</tr>
<tr>
<td> Math </td>
<td>96</td>
<td>A</td>
</tr>
<tr>
<td> Science</td>
<td>99</td>
<td>A</td>
</tr>
</table>
User: Student1
Your results are:
<table border="1">
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Grade </td>
</tr>
<tr>
<td>Math</td>
<td>90</td>
<td>A</td>
</tr>
<tr>
<td>English</td>
<td>70</td>
<td>B</td>
</tr>
</table>
User: Student2
Your results are:
<table border="1">
<tr>
<td> Subject </td>
<td> Marks </td>
<td> Grade </td>
</tr>
<tr>
<td>Science</td>
<td>70</td>
<td>B</td>
</tr>
</table>
请注意:
在XSLT 2.0中,使用<xsl:for-each-group>
指令更方便,更方便。