使用XSL比较两个文件并生成输出文件

时间:2012-10-26 05:45:46

标签: xml xslt merge compare transform

使用XSL我想比较两个文件并生成一个输出文件。

文件1:

<SalesExtractProcess>
  <PackageFormatVersion>3</PackageFormatVersion>
  <VersionComments></VersionComments>
  <CreatorName>Demouser</CreatorName>
  <CreatorComputerName>DemoComputer</CreatorComputerName>
  <CreationDate>10/1/2012 9:00:09 AM</CreationDate>
  <PackageType>5</PackageType>
  <Configurations>
    <SalesConfigurations>
      <ConfigurationType>1</ConfigurationType>
      <ConfigurationString>SalesExtractPackageConfig.dtsConfig</ConfigurationString>
      <ConfigurationVariable></ConfigurationVariable>
    </SalesConfigurations>
  </Configurations>
<SalesExtractProcess>

文件2:

<Package>
    <PackageFormatVersion checked="false">3</PackageFormatVersion>
    <VersionComments checked="false"></VersionComments>
    <CreatorName checked="true">Testuser</CreatorName>
    <CreatorComputerName checked="true">TestComputer</CreatorComputerName>
    <CreationDate checked="true">10/1/2012 9:00:09 AM</CreationDate>
    <PackageType checked="false">5</PackageType>
    <Configurations>
        <Config>
            <ConfigurationType checked="false">1</ConfigurationType>
            <ConfigurationString checked="true">Package.dtsConfig</ConfigurationString>
            <ConfigurationVariable checked="false"></ConfigurationVariable>
        </Config>
    </Configurations>
<Connections>
    <LocalHost.AdventureWorks>
        <ObjectName  checked="true">LocalHost.AdventureWorks</ObjectName>
    </LocalHost.AdventureWorks>
</Connections>
</Package>  

我想将文件1与文件2进行比较,并从文件1输出所有匹配的节点(无论路径如何),并将属性 checked =&#34; true&#34; 输出到结果文件。我的结果文件应该看起来像

结果文件:

<SalesExtractProcess>
    <CreatorName>Demouser</CreatorName>
    <CreatorComputerName>DemoComputer</CreatorComputerName>
    <CreationDate>10/1/2012 9:00:09 AM</CreationDate>
    <Configurations>
      <SalesConfigurations>
         <ConfigurationString>SalesExtractPackageConfig.dtsConfig</ConfigurationString>
      </SalesConfigurations>
    </Configurations>
<SalesExtractProcess>

我无法弄清楚如何为此任务创建xsl。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

下面的模板可以使用document(),模板来过滤掉'master'模板(file2)中定义的checked='false'的元素,以及复制其他属性的部分身份模板。为了复制包装器元素(例如Configurations / SalesConfigurations),它只排除具有checked='false')的元素

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0" 
                exclude-result-prefixes="xmlns">

    <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />

    <xsl:template match="/">
        <xsl:apply-templates select="document('file1.xml')/node()" />
    </xsl:template>

    <!--Partial identity - just copy attributes-->
    <xsl:template match="@*">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
        </xsl:copy>
    </xsl:template>

    <!--Element filter - just elements which don't have @checked='false'-->
    <xsl:template match="*" xml:space="default">
        <xsl:variable name="eleToCheck" select="local-name()"/>
        <xsl:if test="not(document('file2.xml')//*[local-name() = $eleToCheck and @checked='false'])">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()" />
            </xsl:copy>
        </xsl:if>
    </xsl:template>

</xsl:stylesheet>

<强>输出:

<SalesExtractProcess>


    <CreatorName>Demouser</CreatorName>
    <CreatorComputerName>DemoComputer</CreatorComputerName>
    <CreationDate>10/1/2012 9:00:09 AM</CreationDate>

    <Configurations>
        <SalesConfigurations>

            <ConfigurationString>SalesExtractPackageConfig.dtsConfig</ConfigurationString>

        </SalesConfigurations>
    </Configurations>
</SalesExtractProcess>