How to get all the IDs from two XML and merge to a single XML using XSL

只谈情不闲聊 提交于 2019-12-11 18:39:02

问题


I am trying to compare two responses and find out the same and different IDs from them. After that I need to merge them to a single XML document.

Here is one XML document, which is saved in some variable called $one:

<test>
<console>
<consoles>
<ID>123</ID>
</consoles>
<consoles>
<ID>122</ID>
</consoles>
<consoles>
<ID>134</ID>
</consoles>
</console>
</test>

Here is the other XML document, which is saved in some variable called $two:

<test>
<console>
<consoles>
<ID>123</ID>
</consoles>
<consoles>
<ID>122</ID>
</consoles>
<consoles>
<ID>134</ID>
</consoles>
<consoles>
<ID>456</ID>
</consoles>
</console>
</test>

What I need to do is, I need to compare both the XML documents and get the values of the IDs which are same in both the XML documents as well as the different one. So the output will look something like this:

<test>
<console>
<consoles>
<ID>123</ID>
</consoles>
<consoles>
<ID>122</ID>
</consoles>
<consoles>
<ID>134</ID>
</consoles>
<consoles>
<ID>456</ID>
</consoles>
</console>
</test>

Any help will be really appreciated. Thanks in advance.


回答1:


Here is a simple, pure XSLT 1.0 solution:

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

 <xsl:variable name="vRtfDoc2">
    <test>
        <console>
            <consoles>
                <ID>123</ID>
            </consoles>
            <consoles>
                <ID>122</ID>
            </consoles>
            <consoles>
                <ID>134</ID>vDoc2
            </consoles>
            <consoles>
                <ID>456</ID>
            </consoles>
        </console>
    </test>
 </xsl:variable>

 <xsl:variable name="vDoc2" select="document('')/*/xsl:variable[@name = 'vRtfDoc2']/*"/>

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

  <xsl:template match="/">
    <test>
        <console>
          <xsl:apply-templates select="/*/*/*"/>
          <xsl:apply-templates select="$vDoc2/*/*[not(ID = current()/*/*/*/ID)]"/>
        </console>
    </test>
  </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the first of the two provided XML documents:

<test>
    <console>
        <consoles>
            <ID>123</ID>
        </consoles>
        <consoles>
            <ID>122</ID>
        </consoles>
        <consoles>
            <ID>134</ID>
        </consoles>
    </console>
</test>

the wanted, correct result is produced:

<test>
   <console>
      <consoles>
         <ID>123</ID>
      </consoles>
      <consoles>
         <ID>122</ID>
      </consoles>
      <consoles>
         <ID>134</ID>
      </consoles>
      <consoles xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
         <ID>456</ID>
      </consoles>
   </console>
</test>

Note:

The xsl namespace in the result will not appear if the second document was in its own XML file and not embedded in the transformation (which here is done only for convenience).



来源:https://stackoverflow.com/questions/58836770/how-to-compare-two-xml-responses-saved-in-a-variable-and-print-the-distinct-and

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!