我有这个用户列表并希望对其进行过滤,因此它只显示年龄相同且姓氏相同的用户 - 所需的输出是
25,史密斯,哈利 25,史密斯,查理 36,琼斯,托马斯 36,琼斯,亨利<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="duplo.xsl"?>
<report>
<user>
<age>25</age>
<name>
<lastName>Smith</lastName>
<firstName>Harry</firstName>
</name>
</user>
<user>
<age>25</age>
<name>
<lastName>Smith</lastName>
<firstName>Charlie</firstName>
</name>
</user>
<user>
<age>36</age>
<name>
<lastName>Jones</lastName>
<firstName>Thomas</firstName>
</name>
</user>
<user>
<age>36</age>
<name>
<lastName>Smith</lastName>
<firstName>Henry</firstName>
</name>
</user>
<user>
<age>36</age>
<name>
<lastName>Jones</lastName>
<firstName>Henry</firstName>
</name>
</user>
<user>
<age>47</age>
<name>
<lastName>Jones</lastName>
<firstName>Paul</firstName>
</name>
</user>
<user>
<age>47</age>
<name>
<lastName>Smith</lastName>
<firstName>Simon</firstName>
</name>
</user>
</report>
到目前为止,我已经来到这个样式表,它给出了每个“对”中第二个人的列表。
25,史密斯,查理 36,琼斯,亨利我的猜测是我应该为每个人写一些比[2]更聪明的东西,但到目前为止我还没有找到使用什么。有什么想法吗?
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="no"/>
<xsl:key name="duplo" match="user" use="concat(age, '+', name/lastName)"/>
<xsl:template match="/">
<duplo>
<xsl:for-each select="//user[generate-id(.)=generate-id(key('duplo', concat(age, '+', name/lastName))[2])]">
<xsl:sort select="age"/>
<xsl:sort select="name/lastName"/>
<xsl:sort select="name/firstName"/>
<user>
<age><xsl:value-of select="age"/></age>
<name>
<lastName><xsl:value-of select="name/lastName"/></lastName>
<firstName><xsl:value-of select="name/firstName"/></firstName>
</name>
</user>
</xsl:for-each>
</duplo>
</xsl:template>
</xsl:stylesheet>
答案 0 :(得分:1)
你只需要一个内部xsl:for-each
来获取该组中的所有项目,而不仅仅是选择“第二个”....
<xsl:for-each select="//user[generate-id(.)=generate-id(key('duplo', concat(age, '+', name/lastName))[2])]">
<xsl:sort select="age"/>
<xsl:sort select="name/lastName"/>
<xsl:sort select="name/firstName"/>
<xsl:for-each select="key('duplo', concat(age, '+', name/lastName))">
<user>
<age><xsl:value-of select="age"/></age>
<name>
<lastName><xsl:value-of select="name/lastName"/></lastName>
<firstName><xsl:value-of select="name/firstName"/></firstName>
</name>
</user>
</xsl:for-each>
</xsl:for-each>