Xslt XSL和如何删除重复项

Xslt XSL和如何删除重复项,xslt,xslt-1.0,Xslt,Xslt 1.0,我在编写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:

我在编写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>
现在我正在尝试删除重复项,但仅限于个人级别。 我试着将每一项更改为:

Records/Record/PersonalData/PersonalDataDetail[不在前面:*和@title='Country1'或@title='Country2'或@title='Country3']

但考虑到完整文档的内容,它会删除重复项,而不仅仅是个人节点


你能帮我吗?我必须使用XSLT1.0。

在XSLT1.0中删除重复项的首选方法是。这种情况的复杂性在于,您只希望在祖先Person元素中分组。这可以通过将人员的唯一id添加到分组密钥来解决:

XSLT1.0


非常感谢。我使用XSLT1.0解决方案,效果良好。我还不知道确切的数字[count.| key'country',concat.,'|',$person id[1]=1]。你能描述一下吗?@hotel这是我链接到的文章中描述的。
<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>
<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>