XSLT 删除任意重复的同级元素

2024-03-26

答案here https://stackoverflow.com/a/16715399/288341正在做我想要的事情,除了我不想只删除特定元素的重复同级元素,我想删除所有元素的重复同级元素。

此外,出于我的目的,“重复”元素将具有与其同级元素相同的属性、后代元素和文本。

如何修改该答案以实现我的目标?

这是我当前的样式表:

XSL:

<!--
    When a file is transformed using this stylesheet the output will be
    formatted as follows:

    1.)  Elements named "info" will be removed
    2.)  Duplicate sibling elements will be removed
    3.)  Attributes named "file_line_nr" or "file_name" will be removed
    4.)  Comments will be removed
    5.)  Processing instructions will be removed
    6.)  XML declaration will be removed
    7.)  Extra whitespace will be removed
    8.)  Empty attributes will be removed
    9.)  Elements which have no attributes, child elements, or text will be removed
    10.) All elements will be sorted by name recursively
    11.) All attributes will be sorted by name
-->
<xsl:stylesheet
    version="1.0"
    xmlns:xalan="http://xml.apache.org/xalan"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <!--
        Elements/attributes to remove.  Note that comments are not elements or
        attributes.  Since there is no template to match comments they are
        automatically ignored.
    -->
    <xsl:template match="@*[normalize-space()='']|info|@file_line_nr|@file_name"/>

    <!-- Match any attribute -->
    <xsl:template match="@*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        </xsl:copy>
    </xsl:template>

    <!-- Match any element -->
    <xsl:template match="*">
        <xsl:variable name="elementFragment">
            <xsl:copy>
                <xsl:apply-templates select="@*">
                    <xsl:sort select="name()"/>
                </xsl:apply-templates>
                <xsl:apply-templates>
                    <xsl:sort select="name()"/>
                </xsl:apply-templates>
            </xsl:copy>
        </xsl:variable>
        <xsl:variable name="element" select="xalan:nodeset($elementFragment)/*"/>
        <xsl:if test="$element/@* or $element/* or normalize-space($element)">
            <xsl:copy-of select="$element"/>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

输入 XML:

<?xml version="1.0" encoding="UTF-8" standalone="no" ?><!-- XML declaration should be removed -->
<z b="b" a="a" c="c">
    <?some-app inst="some instruction"?><!-- Processing instructions should be removed -->
    <a><!-- Keep elements like this because it has child elements -->
        <x c="c" b="b" a="a"/><!-- Keep elements like this because it has attributes -->
        <c>some text</c><!-- Keep elements like this because it has text -->
        <info a="a"/><!-- Elements named "info" are to be removed -->
        <w file_line_nr="42" file_name="somefile.txt"/><!-- Attributes named "file_line_nr" and "file_name" are to be removed which will leave this element empty, so it should be removed too -->
        <d/><!-- Remove elements like this because it has not attributes, no children, and no text -->

        <v a="a"><!-- Keep this element because it and it sibling "v" element are unique.. It does not have the same exact descendants as its sibling "v" element -->
            some text
            <i a="a">some text</i>
            <q a="a">some text</q>
        </v>
        <v a="a">
            some text
            <i a="a">some different text</i><!-- text is different -->
            <q a="a">some text</q>
        </v>

        <e a="a"><!-- Keep this element because it and it sibling "e" element are unique.. It does not have the same exact descendants as its sibling "e" element -->
            some text
            <j a="a">
                <p>some text</p>
            </j>
        </e>
        <e a="a">
            some text
            <j a="a">
                <p>some different text</p><!-- text is different -->
            </j>
        </e>

        <u a="a"><!-- Keep this element because it and it sibling "e" element are unique.. It does not have the same exact descendants as its sibling "e" element -->
            some text
            <k a="a">some text</k>
            <n a="a">some text</n>
        </u>
        <u a="a">
            some text
            <k b="b">some text</k><!-- attribute is different -->
            <n a="a">some text</n>
        </u>

        <f a="a"><!-- Keep this element because it and it sibling "f" element are unique.. It does not have the same exact attributes as its sibling "f" element -->
            some text
            <l a="a">some text</l>
            <m a="a">some text</m>
        </f>
        <f b="b"><!-- attribute is different -->
            some text
            <l a="a">some text</l>
            <m a="a">some text</m>
        </f>

        <t a="a"><!-- Keep this element because it and it sibling "t" element are unique. It does not have the same exact text as its sibling "t" element -->
            some text
            <az a="a">some text</az>
            <aa a="a">some text</aa>
        </t>
        <t a="a">
            some different text<!-- text is different -->
            <az a="a">some text</az>
            <aa a="a">some text</aa>
        </t>

        <g a="a"><!-- Remove this element because it is NOT unique. Its attributes, descendants, and text are exactly the same as its sibling "g" element -->
            some text
            <ay a="a">some text</ay>
            <ab a="a">some text</ab>
        </g>
        <g a="a">
            some text
            <ay a="a">some text</ay>
            <ab a="a">some text</ab>
        </g>

        <s a="a"/>
    </a>
    <y a="a"/>
    <b>
        <h a="a" />
        <r a="a"/>
    </b>
</z>

所需的输出 XML:(元素和属性已排序。注释和缩进/空格也将被删除,但我已将它们添加回此处以提高可读性。)

<z a="a" b="b" c="c">
    <a>
        <c>some text</c>
        <e a="a">
            some text
            <j a="a">
                <p>some text</p>
            </j>
        </e>
        <e a="a">
            some text
            <j a="a">
                <p>some different text</p>
            </j>
        </e>
        <f a="a">
            some text
            <l a="a">some text</l>
            <m a="a">some text</m>
        </f>
        <f b="b">
            some text
            <l a="a">some text</l>
            <m a="a">some text</m>
        </f>
        <g a="a"><!-- The sibling "g" element of this element was removed because it was an exact duplicate -->
            some text
            <ab a="a">some text</ab>
            <ay a="a">some text</ay>
        </g>
        <s a="a"/>
        <t a="a">
            some text
            <aa a="a">some text</aa>
            <az a="a">some text</az>
        </t>
        <t a="a">
            some different text
            <aa a="a">some text</aa>
            <az a="a">some text</az>
        </t>
        <u a="a">
            some text
            <k a="a">some text</k>
            <n a="a">some text</n>
        </u>
        <u a="a">
            some text
            <k b="b">some text</k>
            <n a="a">some text</n>
        </u>
        <v a="a">
            some text
            <i a="a">some text</i>
            <q a="a">some text</q>
        </v>
        <v a="a">
            some text
            <i a="a">some different text</i>
            <q a="a">some text</q>
        </v>
        <x a="a" b="b" c="c"/>
    </a>
    <b>
        <h a="a"/>
        <r a="a"/>
    </b>
    <y a="a"/>
</z>

这是我的建议来展示如何deep-equalXSLT 2.0 可以帮助:

<xsl:stylesheet
    version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

    <xsl:output indent="yes" method="xml" omit-xml-declaration="yes"/>
    <xsl:strip-space elements="*"/>

    <!-- identity for most attributes -->
    <xsl:template match="@*">
        <xsl:copy/>
    </xsl:template>

    <xsl:template match="*">
        <xsl:copy>
          <xsl:apply-templates select="@*">
            <xsl:sort select="local-name()"/>
           </xsl:apply-templates>
           <xsl:for-each-group select="node() except (processing-instruction(), comment())" group-adjacent="boolean(self::*)">
             <xsl:choose>
               <xsl:when test="current-grouping-key()">
                 <xsl:apply-templates select="current-group()">
                   <xsl:sort select="local-name()"/>
                 </xsl:apply-templates>
               </xsl:when>
               <xsl:otherwise>
                 <xsl:apply-templates select="current-group()"/>
               </xsl:otherwise>
             </xsl:choose>
           </xsl:for-each-group>
        </xsl:copy>
    </xsl:template>

    <!--
        Elements/attributes to remove.
    -->
    <xsl:template match="@*[normalize-space()='']|info|@file_line_nr|@file_name
                         | *[not(@* | node())]"/>


    <!-- remove (well, don't copy) element nodes which are deep-equal to
         a preceding sibling element 
    -->
    <xsl:template match="*[some $ps in preceding-sibling::* satisfies deep-equal(., $ps)]"/>


</xsl:stylesheet>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

XSLT 删除任意重复的同级元素 的相关文章

随机推荐

  • 具有完全相同 pod 标签的两个 Kubernetes 部署

    假设我有两个部署 除了部署名称之外 它们完全相同 apiVersion apps v1 kind Deployment metadata name nginx d spec replicas 3 selector matchLabels a
  • Paintcode 和 Snap SVG

    我正在尝试桥接示例油漆代码 https www youtube com watch v 9SE2tmgYbCU and Snap SVG http snapsvg io docs Here http yepher com svgGearEx
  • Xamarin - 在 XAML 中将集合设置为自定义可绑定属性

    我有一个习惯ContentView具有定义的可绑定属性 public IEnumerable
  • SQL Server 2008 无法删除约束

    我正在尝试使用以下命令从表中删除主键约束 ALTER TABLE SchemaName LabourGrade DROP CONSTRAINT Labour Grade pk 并得到错误Labour Grade pk is not a co
  • Clojure/Scala 互操作?

    我正在尝试与这个简单的 scala 代码进行互操作 但遇到了一些麻烦 package indicators class DoubleRingBuffer val capacity Int 1000 var elements new Arra
  • 故事板的多个入口点

    我需要在启动时在 AppDelegate 中做出一系列决定 根据这些决定的结果 我需要转到故事板的特定部分 所以我的问题是 在不使用任何导航或选项卡控制器的情况下 我如何转到故事板的特定部分 OR 唯一受支持的选项是否具有多个故事板 对于每
  • 如何根据搜索到的字符串过滤列表框中的项目

    我有一个 Windows 窗体应用程序 C 其中包含一个列表框 我在其中添加了一些项目 我没有使用数据源 我想过滤 ListBox 中的项目以仅显示包含我正在搜索的字符串的项目 我通过保留原始项目的列表并在每次搜索字符串更改时从该列表中选择
  • 使用 URL::action() 时如何更改域

    我正在尝试复制 Laravel 3 中可用的内容 我希望能够为路由指定备用域名 例如 我有一条使用此代码生成以下内容的路线 URL action DashboardController something Produces http som
  • 无法转换“AppDelegate Proxy”类型的值

    我已经集成了 Localtyics iOS SDK 之后我收到如下错误 无法转换 LLAppDelegateProxy 类型的值 这意味着我无法获取应用程序委托对象的引用 我现在遇到麻烦了 因为我想要 Localytics 并且还想要 Ap
  • 批处理脚本在文件夹中查找空文件

    我需要识别文件夹中的零 KB 文件并将输出写入文本文件 下面是我使用批处理脚本找到的代码 我想根据我的以下要求进行自定义 echo off set out found txt for r c myfolderfilestosearch F
  • CS0234:System.Web 命名空间中不存在 Mvc

    我根据 Scott Hanselmen Phil Haack 和 Rob Conery 出版的 Professional ASP NET 3 5 MVC 第 13 章 将 ASP net 4 webform 项目转换为 Asp net MV
  • Angular JS:ng-click 范围集在 ng-if 中不起作用

    今天 我看到了 angularjs 中的一个错误 当您尝试直接在 ng click 中设置范围值时 当您的 ng click 位于测试相同范围值的 ng if 中时 它不起作用 gt http jsfiddle net 9j2TL 26 h
  • Android Management API - 使用密码退出 kiosk 模式

    我有问题 我有完全托管的设备并使用 Android Management API https developers google com android management https developers google com andr
  • Jquery 对象选择器作为字符串

    有没有办法获取 jquery 对象的选择器 例如 在 Firefox 中 我看到一个 jquery 对象为 p basket 但 jquery 中似乎没有办法获得这个选择器 有什么办法吗 Phil 如果 jQuery 对象是使用选择器字符串
  • 在 IL 中对空引用调用实例方法

    在 IL 中可以对空引用调用实例方法是否正确 有没有例子可以证明这一点 是的 这是可能的 只要该方法不使用this因为 CLR 不会对以下内容进行空检查call指示 You would have to modify the IL by ha
  • 如何检查我的应用程序是否在 android 中运行(不是作为服务)?

    Problem 我必须检查我的应用程序是否正在运行 当服务已经在后台运行时 基于它 我应该启动特定的活动或应用程序 我尝试或想出但失败的事情我尝试检查当前正在运行的进程 并根据它尝试确定应用程序是否正在运行 失败原因 获取应用程序运行状态一
  • 如何使用 CGImageRef 图像在 NSView 中显示图像

    我想在 NSview 或 NSImageView 中显示图像 在我的头文件中我有 interface FVView NSView NSImageView imageView end 这是我在实现文件中尝试做的事情 void drawRect
  • 将日期时间转换为时间戳并再次转换回来

    我在 Python 中的日期时间方面遇到了一些问题 我尝试将日期时间转换为时间戳 然后再转换回来 无论我如何尝试 最终结果都不一样 我总是以 datetime 2014 1 30 23 59 40 1998 的日期时间结束 import d
  • 使用 REST 是否必须使用适当的方法类型(POST、PUT、GET、DELETE)?

    我们在项目中使用 REST 调用 Web 服务来处理从用户界面进行的所有调用 目前 对于每个方法 签名都是以这种方式配置的 public class SaveNewAddressLabelService GET Consumes appli
  • XSLT 删除任意重复的同级元素

    答案here https stackoverflow com a 16715399 288341正在做我想要的事情 除了我不想只删除特定元素的重复同级元素 我想删除所有元素的重复同级元素 此外 出于我的目的 重复 元素将具有与其同级元素相同