我在编写xslt转换时遇到问题。我想显示带有节点值的列: 记录/记录/个人数据/个人数据详细信息 每人以下: 根/数据/响应/人
所以我开始显示所有值-代码如下:
transformation.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<body>
<table border="1">
<xsl:for-each select="Root/Data/Response/Person">
<tr>
<td><xsl:value-of select="Name"/></td>
<td>
<xsl:for-each select="Records/Record/PersonalData/PersonalDataDetail[(@title='Country1' or @title='Country2' or @title='Country3')]">
<xsl:value-of select="."/>
<xsl:element name="br"/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
示例数据:
<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="transformation.xsl"?>
<Root>
<Data>
<Response>
<Person>
<Name>Robert A.</Name>
<Records>
<Record>
<PersonalData>
<PersonalDataDetail title="Lucky Number">529</PersonalDataDetail>
<PersonalDataDetail title="Favorite Color">Blue</PersonalDataDetail>
</PersonalData>
</Record>
</Records>
</Person>
<Person>
<Name>Robert B.</Name>
<Records>
<Record>
<PersonalData>
<PersonalDataDetail title="Lucky Number">3</PersonalDataDetail>
<PersonalDataDetail title="Country1">USA</PersonalDataDetail>
</PersonalData>
</Record>
<Record>
<PersonalData>
<PersonalDataDetail title="Favorite Color">Red</PersonalDataDetail>
<PersonalDataDetail title="Country2">UK</PersonalDataDetail>
</PersonalData>
</Record>
<Record>
<PersonalData>
<PersonalDataDetail title="Flight">AAA000</PersonalDataDetail>
<PersonalDataDetail title="Country2">UK</PersonalDataDetail>
</PersonalData>
</Record>
</Records>
</Person>
<Person>
<Name>Robert C.</Name>
<Records>
<Record>
<PersonalData>
<PersonalDataDetail title="Lucky Number">529</PersonalDataDetail>
<PersonalDataDetail title="Country1">UK</PersonalDataDetail>
</PersonalData>
</Record>
<Record>
<PersonalData>
<PersonalDataDetail title="Country3">Argentina</PersonalDataDetail>
</PersonalData>
</Record>
<Record>
<PersonalData>
<PersonalDataDetail title="Country3">Argentina</PersonalDataDetail>
<PersonalDataDetail title="Flight">BBB000</PersonalDataDetail>
</PersonalData>
</Record>
</Records>
</Person>
</Response>
</Data>
</Root>
现在,我正在尝试删除重复项,但仅限个人级别。 我尝试将for-each更改为:
Records / Record / PersonalData / PersonalDataDetail [不(。=上一个:: *)和(@ title ='Country1'或@ title ='Country2'或@ title ='Country3') ]
但是考虑到整个文档的内容,它会删除重复项,而不仅限于Person节点。
你能帮我吗?我必须使用XSLT 1.0。
答案 0 :(得分:1)
在XSLT 1.0中删除重复项的首选方法是Muenchian grouping。这种情况的复杂之处在于,您只想在祖先Person
元素内分组。这可以通过将Person
的唯一ID添加到分组键来解决:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="country" match="PersonalDataDetail[starts-with(@title, 'Country')]" use="concat(., '|', generate-id(ancestor::Person))" />
<xsl:template match="/Root">
<html>
<body>
<table border="1">
<xsl:for-each select="Data/Response/Person">
<xsl:variable name="person-id" select="generate-id()" />
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:for-each select="Records/Record/PersonalData/PersonalDataDetail[starts-with(@title, 'Country')][count(. | key('country', concat(., '|', $person-id))[1]) = 1]">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
也就是说,某些XSLT 1.0处理器支持EXSLT set:distinct()
扩展功能,该功能使您可以简化以下过程:
XSLT 1.0 + EXSLT
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:set="http://exslt.org/sets"
extension-element-prefixes="set">
<xsl:template match="/Root">
<html>
<body>
<table border="1">
<xsl:for-each select="Data/Response/Person">
<xsl:variable name="person-id" select="generate-id()" />
<tr>
<td>
<xsl:value-of select="Name"/>
</td>
<td>
<xsl:for-each select="set:distinct(Records/Record/PersonalData/PersonalDataDetail[starts-with(@title, 'Country')])">
<xsl:value-of select="."/>
<br/>
</xsl:for-each>
</td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>