如何使用 XSLT 删除重复的 XML 节点 [英] How to remove duplicate XML nodes using XSLT

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

问题描述

我有一个很长的 XML 文件,比如

<ele1><child1>context1</child1><child2>test1</child2><child1>context1</child1><ele2><child1>context2</child1><child2>test2</child2><child1>context2</child1><ele3>...........<elen></Root>

现在我想使用 xslt 删除每个 中的所有第二个 ,这可能吗?结果是这样的:

<ele1><child1>context1</child1><child2>test1</child2><ele2><child1>context2</child1><child2>test2</child2><ele3>...........<elen></Root>

谢谢你,BR

艾伦

解决方案

这个问题需要更详细的答案,而不仅仅是指出一个好的 慕尼黑分组来源.

原因是所需的分组需要识别ele[SomeString]"元素的所有子元素及其父元素的名称.这种分组需要定义一个键由两个唯一来源唯一定义,通常通过串联.

这种转变:

<前><xsl:stylesheet version="1.0"xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output omit-xml-declaration="yes" indent="yes"/><xsl:key name="kElByName" match="*"use="concat(generate-id(..), '+',name())"/><xsl:template match="node()|@*"><xsl:apply-templates select="node()|@*"/></xsl:copy></xsl:模板><xsl:template match="*[starts-with(name(), 'ele')]"><xsl:copy-of select="@*"/><xsl:apply-templates select="*[生成 ID()=生成 id(key('kElByName',concat(generate-id(..), '+',name()))[1])]"/></xsl:copy></xsl:模板></xsl:样式表>

应用于此 XML 文档时:

<前><根><child1>context1</child1>test1<child1>context1</child1><child1>context2</child1>test2<child1>context2</child1>context2test2<child1>context1</child1>

产生想要的结果:

<前><根><child1>context1</child1>test1<child1>context2</child1>test2context2<child1>context1</child1>

I've got an extremely long XML file, like

<Root>
   <ele1>
      <child1>context1</child1>
      <child2>test1</child2>
      <child1>context1</child1>
   </ele1>

   <ele2>
      <child1>context2</child1>
      <child2>test2</child2>
      <child1>context2</child1>
   </ele2>
   <ele3>...........<elen>
</Root>

Now I want to remove all the second <child1> in each <ele> using xslt, is it possible? The result would be like this:

<Root>
   <ele1>
      <child1>context1</child1>
      <child2>test1</child2>
   </ele1>

   <ele2>
      <child1>context2</child1>
      <child2>test2</child2>
   </ele2>
       <ele3>...........<elen>
</Root>

Thank u, BR

Allen

解决方案

This question requires a little bit more detailed answer than just pointing to a good Muenchian Grouping source.

The reason is that the needed grouping requires to identify both the names of all children of an "ele[SomeString]" element and their parent. Such grouping requires to define a key that is uniquely defined by both unique sources, usually via concatenation.

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>

 <xsl:key name="kElByName" match="*"
      use="concat(generate-id(..), '+',name())"/>

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

    <xsl:template match="*[starts-with(name(), 'ele')]">
      <xsl:copy>
        <xsl:copy-of select="@*"/>
        <xsl:apply-templates select=
         "*[generate-id()
           =
            generate-id(key('kElByName',
                        concat(generate-id(..), '+',name())
                        )[1])
            ]"
         />
      </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

when applied on this XML document:

<Root>
    <ele1>
        <child1>context1</child1>
        <child2>test1</child2>
        <child1>context1</child1>
    </ele1>
    <ele2>
        <child1>context2</child1>
        <child2>test2</child2>
        <child1>context2</child1>
    </ele2>
    <ele3>
        <child2>context2</child2>
        <child2>test2</child2>
        <child1>context1</child1>
    </ele3>
</Root>

produces the wanted result:

<Root>
    <ele1>
        <child1>context1</child1>
        <child2>test1</child2>
    </ele1>
    <ele2>
        <child1>context2</child1>
        <child2>test2</child2>
    </ele2>
    <ele3>
        <child2>context2</child2>
        <child1>context1</child1>
    </ele3>
</Root>

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

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