将两个xml与xslt进行比较并打印差异

时间:2013-06-17 17:45:04

标签: java xml xslt

我有两个xml文件,如下面的file1.xml和file2.xml

file1.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="test.xsl"?>
<serviceProviders>
            <code>Test</code>
            <skin>
                <code>default</code>
                <label>Default</label>
                <sortOrder>99</sortOrder>
            </skin>
            <serviceProviderProperties>
            <propertyGroup>
                    <code>tabConfiguration</code>
                    <label>Tab Configuration</label>
                    <sortOrder>99</sortOrder>
                </propertyGroup>
                <name>clinicalTabContainer</name>
                <value>clinical</value>
                <sortOrder>1</sortOrder>
            </serviceProviderProperties>
</serviceProviders>

file2.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<serviceProviders>
            <code>Test</code>
            <idPrefix>KNWC</idPrefix>
            <skin>
                <code>default</code>
                <label>Default</label>
                <!--<sortOrder>99</sortOrder>-->
            </skin>
            <serviceProviderProperties>
            <propertyGroup>
                    <code>tabConfiguration</code>
                    <label>Tab Configuration</label>
                    <sortOrder>99</sortOrder>
                </propertyGroup>
                <name>clinicalTabContainer</name>
                <value>clinical</value>
                <sortOrder>1</sortOrder>
            </serviceProviderProperties>
            <serviceProviderProperties>
                <name>autoSubmitServiceDescription</name>
                <value>Primary Mental Health</value>
                <sortOrder>99</sortOrder>
            </serviceProviderProperties>
</serviceProviders>

我想比较这两个xml并使用test.xsl打印差异。 我也有这个XSL文件。但是我没有得到理想的结果......我哪里出错了你能解释并解决它吗?

test.xsl

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:exslt="http://exslt.org/common" version="1.0">

  <!-- Replace // with / everywhere if we're only interested
    in immediate children of /RootElement. -->

  <xsl:variable name="docA" select="/" />
  <xsl:variable name="docB" select="document('file3.xml')"/>

  <!-- This produces a whole nother copy of both docs!
       So, is the performance cost worth it?? -->

  <xsl:variable name="sortedNodesA">
    <!-- produce a sorted, flattened RTF of A's nodes -->
    <xsl:for-each select="$docA/RootElement//*">
      <xsl:sort select="name()" />
      <xsl:copy-of select="." />
    </xsl:for-each>
  </xsl:variable>

  <xsl:variable name="sortedNodesB">
    <!-- produce a sorted, flattened RTF of B's nodes -->
    <xsl:for-each select="$docB/RootElement//*">
      <xsl:sort select="name()" />
      <xsl:copy-of select="." />
    </xsl:for-each>
  </xsl:variable>

  <xsl:template name="recurse">
    <xsl:param name="nodesA" />
    <xsl:param name="nodesB" />
    <xsl:if test="$nodesA | $nodesB">
      <xsl:variable name="nameA" select="name($nodesA[1])" />
      <xsl:variable name="nameB" select="name($nodesB[1])" />
      <xsl:variable name="compar">
        <xsl:call-template name="compare-names">
          <xsl:with-param name="a" select="$nodesA[1]" />
          <xsl:with-param name="b" select="$nodesB[1]" />
        </xsl:call-template>
      </xsl:variable>

  <xsl:template match="/">
    <xsl:call-template name="recurse">
      <xsl:with-param name="nodesA"
        select="exslt:node-set($sortedNodesA)/*" />
      <xsl:with-param name="nodesB"
        select="exslt:node-set($sortedNodesB)/*" />
    </xsl:call-template>
  </xsl:template>

      <xsl:choose>
        <xsl:when test="0 > $compar"> <!-- $nodesA[1] is alph. first -->
          <p><xsl:value-of select="$nameA" /> is only in Old XML.</p>
          <xsl:call-template name="recurse">
            <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
            <xsl:with-param name="nodesB" select="$nodesB" />
          </xsl:call-template>
        </xsl:when>

        <xsl:when test="$compar > 0"> <!-- $nodesB[1] is alph. first -->
          <p><xsl:value-of select="$nameB" /> is only in New XML.</p>
          <xsl:call-template name="recurse">
            <xsl:with-param name="nodesA" select="$nodesA" />
            <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
          </xsl:call-template>
        </xsl:when>

        <xsl:otherwise>
          <p><xsl:value-of select="$nameB" /> is in both documents.
            <!-- Do I need string(text(...))? -->
            <xsl:if
              test="string($nodesA[1]/text()) != string($nodesB[1]/text())">
              But their contents differ:
              '<xsl:value-of select="$nodesA[1]/text()" />' !=
              '<xsl:value-of select="$nodesB[1]/text()" />'.
            </xsl:if><br></br>
          </p>
          <xsl:call-template name="recurse">
            <xsl:with-param name="nodesA" select="$nodesA[position()>1]" />
            <xsl:with-param name="nodesB" select="$nodesB[position()>1]" />
          </xsl:call-template>
        </xsl:otherwise>
      </xsl:choose>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

提前感谢...

1 个答案:

答案 0 :(得分:1)

如果XML作为单个文件输入,例如;

<root>
   <old>
      file 1 xml
   </old>
   <new>
      file 2 xml
   </new>
</root>

你可以使用下面的东西,尽管我正在对此进行测试并且正在努力!但是你可能在XSLT中有比我更好的背景,并且能够弄清楚

 <xsl:key name="old" match="root/old/serviceProviders/*" use="." />
    <xsl:key name="new" match="root/new/serviceProviders/*" use="." />

<xsl:template match="*">
    <xsl:copy>
        <xsl:copy-of select="@*" /> 
        <xsl:apply-templates /> 
    </xsl:copy> 
</xsl:template>

<xsl:template match="root/new/serviceProviders/*[key('old', .)]">       
</xsl:template>

<xsl:template match="root/old/serviceProviders/*[key('new', .)]">       
</xsl:template>

希望你能看到我在这里尝试做什么,我之前使用过这个,只是我检查某些字段名称并且可以添加此限制以及输出到新字段名称(不需要现有结构

以上面建议的格式使用你的XML,输出如下,这不是完美的,但它越来越近了.....

  <old>
    <serviceProviders>

        <skin>
            <code>default</code>
            <label>Default</label>
            <sortOrder>99</sortOrder>
        </skin>

          </serviceProviders>
   </old>
   <new>
          <serviceProviders>

        <idPrefix>KNWC</idPrefix>
        <skin>
            <code>default</code>
            <label>Default</label>

        </skin>

        <serviceProviderProperties>
            <name>autoSubmitServiceDescription</name>
            <value>Primary Mental Health</value>
            <sortOrder>99</sortOrder>
        </serviceProviderProperties>
          </serviceProviders>
   </new>

还有很长的路要走,因为我正在努力拿起某些元素的父节点等等,但希望这会有所帮助,有人可以把我的无意义变成一些有用的代码给你!