Raju G
Raju G

Reputation: 97

Using XSL compare two files and generate an output file

Using XSL I want to compare two file and generate an output file.

File 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>

File 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>  

I want to compare File 1 with File 2 and from File 1 output all matching nodes (irrespective of path) with attribute checked="true" to result file. My result file should look like

Result File :

<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>

I couldnt figure out how to create xsl for this task. Any help would be appreciated.

Upvotes: 2

Views: 1391

Answers (1)

StuartLC
StuartLC

Reputation: 107327

The below template should do the trick, by using document(), a template to filter out elements with checked='false' defined in the 'master' template (file2), and partial identity template to copy other attributes. In order to copy the wrapper elements (e.g. Configurations / SalesConfigurations), it only excludes elements which have 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>

Output:

<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>

Upvotes: 1

Related Questions