XSLT 和 xpath v1.0 查找重复项并聚合 [英] XSLT and xpath v1.0 find duplicates and aggregate

查看:24
本文介绍了XSLT 和 xpath v1.0 查找重复项并聚合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有一种方法可以在 xml 中搜索重复项,然后在找到时将所有重复项聚合到一个节点.F.eks

I wonder if there´s a way to search an xml for duplicates, then when found to aggregate all the duplicates to one node. F.eks

<car name="one">
 <person>john</person>
 <person>Jay</person>
</car>
<car name="two">
 <person>kim</person>
 <person>chris</person>
</car>
<car name="one">
 <person>jane</person>
 <person>liz</person>
</car>

应该是:

<car name="one">
 <person>john</person>
 <person>Jay</person>
 <person>jane</person>
 <person>liz</person>
</car>
<car name="two">
 <person>kim</person>
 <person>chris</person>
</car>

非常感谢所有帮助!

金先生

推荐答案

在 XSLT 1.0 中,您必须使用称为 Muenchian 方法,这是使用 keygenerate-id 函数对元素进行分组的过程.

In XSLT 1.0 you have to use something called the Muenchian Method which is a process of grouping elements using the key and generate-id functions.

流程如下:

您首先定义一个键,该键表示要与之分组的数据.

You first define a key that represents the data that you want to group with.

<xsl:key name="car-by-name" match="car" use="@name"/>

然后,您可以通过基于该键生成 id 来在模板匹配中使用该键.

Then you you use that key in a template match by generating an id based on that key.

<xsl:apply-templates select="car[generate-id() = generate-id(key('car-by-name', @name)[1])]" mode="group"/>

现在您已将节点分组,您要做的就是再次使用该键来获取该键内的所有节点.

Now that you have your nodes grouped, all you do is use that key again to grab all of the nodes within that key.

<xsl:apply-templates select="key('car-by-name', @name)"/>

现在将所有内容一起展示,当您使用此 XML 时(添加了 document 以使其格式良好).

So now to show it all together, when you take this XML (added document to make it well-formed).

<document>
  <car name="one">
    <person>john</person>
    <person>Jay</person>
  </car>
  <car name="two">
    <person>kim</person>
    <person>chris</person>
  </car>
  <car name="one">
    <person>jane</person>
    <person>liz</person>
  </car>
</document>

并应用此 XSLT

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes" />
  <xsl:key name="car-by-name" match="car" use="@name"/>

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="document">
    <xsl:copy>
      <xsl:apply-templates select="car[generate-id() = generate-id(key('car-by-name', @name)[1])]" mode="group"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="car" mode="group">
    <car name="{@name}">
      <xsl:apply-templates select="key('car-by-name', @name)"/>
    </car>
  </xsl:template>

  <xsl:template match="car">
    <xsl:apply-templates select="@*|node()"/>
  </xsl:template>

</xsl:stylesheet>

它产生了这个结果.

<?xml version="1.0" encoding="UTF-8"?>
<document>
  <car name="one">
    <person>john</person>
    <person>Jay</person>

    <person>jane</person>
    <person>liz</person>
  </car>
  <car name="two">
    <person>kim</person>
    <person>chris</person>
  </car>
</document>

这篇关于XSLT 和 xpath v1.0 查找重复项并聚合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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