我在编写XSLT转换方面有问题。我想显示带有Node值的列:记录/记录/个人数据/个人达达尾巴每个人以下:根/数据/响应/人
所以我开始显示所有值 - 代码如下:
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-for-for-for更改为:
records/record/personaldata/personalDataDatail [ not(。= recting ::*)]
,但它删除了考虑完整文档的内容的重复,不仅适用于人节点。
你能帮我吗?我必须使用XSLT 1.0。
在XSLT 1.0中删除重复项的首选方法是Muenchian分组。在您的情况下,复杂性是您只想将祖先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>