2012-10-26 103 views
2

使用XSL我想比較兩個文件並生成輸出文件。使用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 =「true」複製到結果文件。我的結果文件應該是這樣

結果文件:

<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

通過使用document(),模板可以過濾掉'master'模板(file2)中定義的checked='false'以及用於複製其他屬性的部分標識模板。爲了複製包裝元素(例如配置/ 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> 
+1

這就是我想要的目的。非常感謝你** nonnb **你拯救了我的一天。 –