使用 XSLT 合并两个或多个 XML 节点 [英] Merge two or more XML node using XSLT

查看:38
本文介绍了使用 XSLT 合并两个或多个 XML 节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是输入文件:

<root> 
    <node id="N1">
        <fruit id="1" action="aaa">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                    <year>2012</year>
                </attribute>
            </orange>
            <orange id="x" action="change">
                <attribute>
                    <color>Red</color>
                </attribute>
            </orange>
            <orange id="x" action="change">
                <attribute>
                    <color>Blue</color>
                    <condition>good</condition>
                </attribute>
            </orange>
        </fruit>

    </node>

    <node id="N2">
        <car id="1">    
            <bmw id="i" action="change">
                <attribute>
                    <color>Blue</color>    
                </attribute>
            </bmw>      
            <bmw id="i" action="change">
                <attribute>
                    <color>Yellow</color>    
                </attribute>
            </bmw>
            <bmw id="i" action="change">
                <attribute>
                    <color>Pink</color>    
                </attribute>
            </bmw>


        <bmw id="j" action="delete">
            <attribute>
                <color>Blue</color>    
            </attribute>
        </bmw>
        <bmw id="j" action="delete">
            <attribute>
                <color>Yellow</color>    
            </attribute>
        </bmw>

    </car>
    </node>
</root>

这是预期的输出:

<root> 
    <node id="N1">
        <fruit id="1" action="aaa">
            <orange id="x" action="create">
                <attribute>
                    <color>Blue</color>
                    <year>2012</year>
                    <condition>good</condition>
                </attribute>
            </orange>                      
        </fruit>       
    </node>

    <node id="N2">
        <car id="1">    

            <bmw id="i" action="change">
                <attribute>
                    <color>Pink</color>    
                </attribute>
            </bmw>


        <bmw id="j" action="delete">
            <attribute>
                <color>Yellow</color>    
            </attribute>
        </bmw>
      </car>
    </node>
</root>

规则:

  1. 如果有一个带有create"方法的节点,然后是一个或多个change"方法,我们将它们合并在一起,并使用最后一个change"的所有子节点到带有create"方法的节点中.(注:好是加)

  1. if there is node with 'create' method followed by one or more 'change' method, we merge them together and use ALL children from the last 'change' into the node with 'create' method. (Note: good is added )

如果有一个或多个更改"方法,我们将它们合并并仅使用最后一个更改"方法的子项.

if there is one or more 'change' method we merge them and use only children from the last 'change' method.

如果有一个或多个 'delete' 方法,我们将它们合并并仅使用最后一个 'delete' 方法的子项.

if there is one or more 'delete' method we merge them and use only children from the last 'delete' method.

注意要合并的节点的id必须相同.

Note that the id must be the same for node to be merged.

请告诉我有关此问题的 XSLT 解决方案.非常感谢.

Please advise me on XSLT solution for this problem. Thanks so much.

亲切的问候,约翰

推荐答案

这种转变:

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

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

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

     <xsl:template match=
      "node/*/*
        [@action='delete'
       and
         following-sibling::*[1][@action='delete']
        ]"/>

     <xsl:template match=
      "node/*/*
        [@action='change'
       and
         following-sibling::*[1][@action='change']
       or
         preceding-sibling::*[@action='create']
        ]"/>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<root>
    <node id="N1">
        <fruit id="1" action="aaa">
            <orange id="x" action="create">
                <attribute>
                    <color>Orange</color>
                </attribute>
            </orange>
            <orange id="x" action="change">
                <attribute>
                    <color>Red</color>
                </attribute>
            </orange>
            <orange id="x" action="change">
                <attribute>
                    <color>Blue</color>
                    <condition>good</condition>
                </attribute>
            </orange>
        </fruit>
    </node>
    <node id="N2">
        <car id="1">
            <bmw id="i" action="change">
                <attribute>
                    <color>Blue</color>
                </attribute>
            </bmw>
            <bmw id="i" action="change">
                <attribute>
                    <color>Yellow</color>
                </attribute>
            </bmw>
            <bmw id="i" action="change">
                <attribute>
                    <color>Pink</color>
                </attribute>
            </bmw>
            <bmw id="j" action="delete">
                <attribute>
                    <color>Blue</color>
                </attribute>
            </bmw>
            <bmw id="j" action="delete">
                <attribute>
                    <color>Yellow</color>
                </attribute>
            </bmw>
        </car>
    </node>
</root>

产生想要的、正确的结果:

<root>
   <node id="N1">
      <fruit id="1" action="aaa">
         <orange id="x" action="create">
            <attribute>
               <color>Blue</color>
               <condition>good</condition>
            </attribute>
         </orange>
      </fruit>
   </node>
   <node id="N2">
      <car id="1">
         <bmw id="i" action="change">
            <attribute>
               <color>Pink</color>
            </attribute>
         </bmw>
         <bmw id="j" action="delete">
            <attribute>
               <color>Yellow</color>
            </attribute>
         </bmw>
      </car>
   </node>
</root>

这篇关于使用 XSLT 合并两个或多个 XML 节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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