从XML文件中删除重复的节点(XSLT转换)

时间:2018-11-22 21:05:09

标签: xml xslt

如果有人可以帮助我创建xslt以根据重复属性的值从XML中删除重复节点,我将不胜感激。在下面的示例中,属性是PublisherName和Data

输入

        <RuleCollection>
<FilePublisherRule Id="1">
    <Conditions>
        <FilePublisherCondition PublisherName="O=Vendor1">
            <BinaryVersionRange LowSection="*" HighSection="*" />
        </FilePublisherCondition>
    </Conditions>
</FilePublisherRule>
<FilePublisherRule Id="2">
    <Conditions>
        <FilePublisherCondition PublisherName="O=Vendor2">
            <BinaryVersionRange LowSection="*" HighSection="*" />
        </FilePublisherCondition>
    </Conditions>
</FilePublisherRule>
<FilePublisherRule Id="3">
    <Conditions>
        <FilePublisherCondition PublisherName="O=Vendor1">
            <BinaryVersionRange LowSection="*" HighSection="*" />
        </FilePublisherCondition>
    </Conditions>
</FilePublisherRule>    
<FileHashRule Id="10">
    <Conditions>
        <FileHashCondition>
            <FileHash Type="SHA256" Data="0xF11BD15D6A565071AED8D3581012C5B5EE0AB46795CEF4ECD712F7572A3DBE36" SourceFileName="_IU14D2N.TMP" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0xF11BD15D6A565071AED8D3581012C5B5EE0AB46795CEF4ECD712F7572A3DBE36" SourceFileName="UNINS000.EXE" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0x5831F348B9B2744F56631CFB2FCAB676BD8B9D3EE407A2DE2C66BC3C395A9E73" SourceFileName="CO.EXE" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0x4BA8285055A9CF8EE146F738DC8526CCD1E509B03984FE1914A6826C7CD530EE" SourceFileName="SETUPCO.EXE" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0x4BA8285055A9CF8EE146F738DC8526CCD1E509B03984FE1914A6826C7CD530EE" SourceFileName="SETUPDAR.EXE" SourceFileLength="0" />
        </FileHashCondition>
    </Conditions>
</FileHashRule>
<RuleCollection/>

我需要

<RuleCollection>
<FilePublisherRule Id="1">
    <Conditions>
        <FilePublisherCondition PublisherName="O=Vendor1">
            <BinaryVersionRange LowSection="*" HighSection="*" />
        </FilePublisherCondition>
    </Conditions>
</FilePublisherRule>
<FilePublisherRule Id="2">
    <Conditions>
        <FilePublisherCondition PublisherName="O=Vendor2">
            <BinaryVersionRange LowSection="*" HighSection="*" />
        </FilePublisherCondition>
    </Conditions>
</FilePublisherRule>
<FileHashRule Id="10">
    <Conditions>
        <FileHashCondition>
            <FileHash Type="SHA256" Data="0xF11BD15D6A565071AED8D3581012C5B5EE0AB46795CEF4ECD712F7572A3DBE36" SourceFileName="UNINS000.EXE" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0x5831F348B9B2744F56631CFB2FCAB676BD8B9D3EE407A2DE2C66BC3C395A9E73" SourceFileName="CO.EXE" SourceFileLength="0" />
            <FileHash Type="SHA256" Data="0x4BA8285055A9CF8EE146F738DC8526CCD1E509B03984FE1914A6826C7CD530EE" SourceFileName="SETUPCO.EXE" SourceFileLength="0" />
        </FileHashCondition>
    </Conditions>
</FileHashRule>
<RuleCollection/>

我检查了一些已经存在的情况,看来我需要在xslt中使用一些模板并使用Munechian分组,但是我没有经验。

我已经使用此xslt删除了复制的FileHash节点

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
 version="1.0">
 <xsl:output omit-xml-declaration="no" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:key name="kPropertyByName" match="FileHash" use="@Data"/>

 <xsl:template match="@*|node()">
 <xsl:copy>
  <xsl:apply-templates select="@*|node()"/>
 </xsl:copy>
 </xsl:template>

 <xsl:template
 match="FileHash[
         not(
           generate-id() =
           generate-id(key('kPropertyByName', @Data)[1])
         )
       ]"/>

 </xsl:stylesheet>

我还有一个比原始文件大的输出文件,为什么?我需要小一点。 我想下一步是添加第二个模板来处理基于PublisherName值的FilePublisherRule节点

我添加了第二个模板

         <xsl:template
       match="FileHash[
            not(
             generate-id() =
             generate-id(key('kPropertyByName', @Data)[1])
              )
           ]"/>

              <xsl:template
       match="FilePublisherCondition[
             not(
               generate-id() =
              generate-id(key('kProperty1ByName', @PublisherName)[1])
             )

删除节点FilePublisherCondition,但保留父节点

        <FilePublisherRule Id="0355cba7-7d4a-4a74-9579-8d2192fa0514" Name="Signed by O=ADOBE SYSTEMS INCORPORATED, L=SAN JOSE, S=CALIFORNIA, C=US" Description="" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions>
    <FilePublisherCondition PublisherName="O=ADOBE SYSTEMS INCORPORATED, L=SAN JOSE, S=CALIFORNIA, C=US" ProductName="*" BinaryName="*">
    <BinaryVersionRange LowSection="*" HighSection="*"></BinaryVersionRange>
    </FilePublisherCondition>
    </Conditions>
    </FilePublisherRule>
    <FilePublisherRule Id="0355cba7-7d4a-4a74-9579-8d2192fa0514" Name="Signed by O=ADOBE SYSTEMS INCORPORATED, L=SAN JOSE, S=CALIFORNIA, C=US" Description="" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions></Conditions>
    </FilePublisherRule>
    <FilePublisherRule Id="0355cba7-7d4a-4a74-9579-8d2192fa0514" Name="Signed by O=ADOBE SYSTEMS INCORPORATED, L=SAN JOSE, S=CALIFORNIA, C=US" Description="" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions></Conditions>
    </FilePublisherRule>
    <FilePublisherRule Id="e9a23377-81af-41dd-b65d-d8f45c7eac7f" Name="myAgent (O=FLEXERA SOFTWARE LLC, L=SCHAUMBURG, S=ILLINOIS, C=US)" Description="myAgent (6.2.1.172)" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions>
    <FilePublisherCondition PublisherName="O=FLEXERA SOFTWARE LLC, L=SCHAUMBURG, S=ILLINOIS, C=US" ProductName="*" BinaryName="*">
    <BinaryVersionRange LowSection="*" HighSection="*"></BinaryVersionRange>
    </FilePublisherCondition>
    </Conditions>
    </FilePublisherRule>
    <FilePublisherRule Id="e9a23377-81af-41dd-b65d-d8f45c7eac7f" Name="myAgent (O=FLEXERA SOFTWARE LLC, L=SCHAUMBURG, S=ILLINOIS, C=US)" Description="myAgent (6.2.1.172)" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions></Conditions>
    </FilePublisherRule>
    <FileHashRule Id="c4232cc1-563b-4fa7-84da-19331af01de4" Name="communication clients" Description="" UserOrGroupSid="S-1-1-0" Action="Allow">
    <Conditions>    

2 个答案:

答案 0 :(得分:0)

您关于消除重复的FileHash元素的解决方案是正确的, 尽管可以简化一点(请参见下面的代码)。

要消除包含重复的FilePublisherRule属性的PublisherName个元素, 我们必须采取类似的方式:

  1. 创建一个key,与kPubl匹配的FilePublisherCondition, 使用@PublisherName
  2. 在其中写入匹配FilePublisherRule的模板:
    • Conditions/FilePublisherCondition保存在变量下(我称它为 fpCond
    • 检查generate-id$fpCond的相等性,并读取第一个键 来自kPubl,代表$fpCond/@PublisherName
    • 如果它们相等,则复制当前元素(我使用copy apply-templates,例如在身份模板中)。
    • 否则,什么也不做(跳过此元素)。

因此整个XSLT脚本可以如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
  <xsl:output omit-xml-declaration="no" indent="yes"/>
  <xsl:strip-space elements="*"/>

  <xsl:key name="kData" match="FileHash" use="@Data"/>
  <xsl:key name="kPubl" match="FilePublisherCondition" use="@PublisherName"/>

  <xsl:template match="@*|node()">
    <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
  </xsl:template>

  <xsl:template match="FileHash[generate-id() !=
    generate-id(key('kData', @Data)[1])]"/>

  <xsl:template match="FilePublisherRule">
    <xsl:variable name="fpCond" select="Conditions/FilePublisherCondition"/>
    <xsl:if test="generate-id($fpCond) = generate-id(key('kPubl', $fpCond/@PublisherName)[1])">
      <xsl:copy><xsl:apply-templates select="@* | node()"/></xsl:copy>
    </xsl:if>
  </xsl:template>
</xsl:stylesheet>

有关工作示例,请参见http://xsltransform.net/6qaFCEf

答案 1 :(得分:0)

要消除基于孙子代属性的FilePublisherRule个重复项,您可以简单地使用该密钥添加一个密钥和另一个空模板:

  <xsl:key name="publisherName" match="FilePublisherRule" use="Conditions/FilePublisherCondition/@PublisherName"/>

  <xsl:template match="FilePublisherRule[not(generate-id() = generate-id(key('publisherName', Conditions/FilePublisherCondition/@PublisherName)[1]))]"/>

https://xsltfiddle.liberty-development.net/bFDb2Dj