2

使用 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 输出所有匹配节点(无论路径如何),属性为check="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。任何帮助,将不胜感激。

4

1 回答 1

1

下面的模板应该可以做到这一点,通过使用document()模板来过滤checked='false'在“主”模板(file2)中定义的元素,并使用部分标识模板来复制其他属性。为了复制包装器元素(例如 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>
于 2012-10-26T11:32:04.487 回答