XSL 以及如何删除重复项 [英] XSL and how to remove duplicates

查看:30
本文介绍了XSL 以及如何删除重复项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在编写 xslt 转换时遇到问题.我想显示来自节点的值的列:记录/记录/PersonalData/PersonalDataDetail每人以下:根/数据/响应/人

I have an issue with writing xslt transformation. I want to show column with values from node: Records/Record/PersonalData/PersonalDataDetail Per each person under: Root/Data/Response/Person

所以我开始显示所有值 - 代码如下:

So I started with showing all values - code is below:

转换.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 更改为:

Now I'm trying to remove duplicates, but only on person level. I tried with changing for-each to:

Records/Record/PersonalData/PersonalDataDetail[not(.=preceding::*) 和(@title='Country1' or @title='Country2' or @title='Country3')]

Records/Record/PersonalData/PersonalDataDetail[not(.=preceding::*) and (@title='Country1' or @title='Country2' or @title='Country3')]

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

But it removes duplicates considering content of full document, not only for Person node.

你能帮我吗?我必须使用 XSLT 1.0.

Could you help me? I have to use XSLT 1.0.

推荐答案

在 XSLT 1.0 中删除重复项的首选方法是 慕尼黑分组.您的情况的复杂之处在于您只想在祖先 Person 元素内分组.这是通过将 Person 的唯一 id 添加到分组键来解决的:

The preferred method to remove duplicates in XSLT 1.0 is Muenchian grouping. The complication in your case is that you only want to group within the ancestor Person element. This is solved by adding the unique id of the Person to the grouping key:

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() 扩展函数,它允许您将过程简化为:


That said, some XSLT 1.0 processors support the EXSLT set:distinct() extension function, which allows you to simplify the process to:

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>

这篇关于XSL 以及如何删除重复项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆