删除重复节点,使用xslt将第一个节点保留在XML中

时间:2012-09-12 14:10:10

标签: xml xslt

如果名称重复,我如何删除类型包的节点。我的意思是应该保留第一次出现,其他的应该被删除。请参阅package_one名称在包节点中重复

<?xml version="1.0"?>
<coverage branch-rate="0.53968253968278" branch-total="50" line-rate="0.66864343958488" line-total="2000" timestamp="1346297959" version="gcovr 2.3">
<sources>
    <source>
        ./.
    </source>
</sources>
<packages>
<package branch-rate="0.52380952380967" branch-total="15" line-rate="0.75148257968866" line-total="1000" complexity="0.0" name="package_one">
<classes>
<class branch-rate="0.0" complexity="0.0" filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
                <lines>
                    <line branch="false" hits="0" number="128" condition-coverage="100%"/>
                    <line branch="false" hits="0" number="192" condition-coverage="100%"/>                      
                </lines>
            </class>
        </classes>
<class branch-rate="0.5" complexity="0.0" filename="good.cpp" line-rate="1.0" name="good1.cpp">
                <lines>
                    <line branch="false" hits="96" number="8" condition-coverage="100%"/>                       
                </lines>
            </class></package>
    <package branch-rate="0.571428571429" branch-total="10" complexity="0.0" line-rate="0.00593031875463" line-total="1000" name="package_one">
        <classes>
            <class branch-rate="0.0" complexity="0.0" filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
                <lines>
                    <line branch="false" hits="0" number="128" condition-coverage="100%"/>                      
                </lines>
            </class>
        </classes>
    </package>      
</packages>

预期:

<?xml version="1.0"?>
<coverage branch-rate="0.53968253968278" branch-total="50" line-rate="0.66864343958488" line-total="2000" timestamp="1346297959" version="gcovr 2.3">
<sources>
    <source>
        ./.
    </source>
</sources>
<packages><package branch-rate="0.52380952380967" branch-total="15" line-rate="0.75148257968866" line-total="1000" complexity="0.0" name="package_one"><classes>
            <class branch-rate="0.0" complexity="0.0" filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
                <lines>
                    <line branch="false" hits="0" number="128" condition-coverage="100%"/>
                    <line branch="false" hits="0" number="192" condition-coverage="100%"/>                      
                </lines>
            </class>
        </classes><class branch-rate="0.5" complexity="0.0" filename="good.cpp" line-rate="1.0" name="good1.cpp">
                <lines>
                    <line branch="false" hits="96" number="8" condition-coverage="100%"/>                       
                </lines>
            </class></package>      
</packages>

1 个答案:

答案 0 :(得分:2)

<强>予。这个简单的XSLT 1.0转换:

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

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

 <xsl:template match="package[preceding::package]"/>
</xsl:stylesheet>

应用于提供的XML文档时:

<coverage branch-rate="0.53968253968278"
 branch-total="50" line-rate="0.66864343958488"
 line-total="2000" timestamp="1346297959" version="gcovr 2.3">
    <sources>
        <source>
        ./.
        </source>
    </sources>
    <packages>
        <package branch-rate="0.52380952380967"
          branch-total="15" line-rate="0.75148257968866"
          line-total="1000" complexity="0.0" name="package_one">
            <classes>
                <class branch-rate="0.0" complexity="0.0"
                 filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
                    <lines>
                        <line branch="false" hits="0" number="128" condition-coverage="100%"/>
                        <line branch="false" hits="0" number="192" condition-coverage="100%"/>
                    </lines>
                </class>
            </classes>
            <class branch-rate="0.5" complexity="0.0"
             filename="good.cpp" line-rate="1.0" name="good1.cpp">
                <lines>
                    <line branch="false" hits="96" number="8" condition-coverage="100%"/>
                </lines>
            </class>
        </package>
        <package branch-rate="0.571428571429"
         branch-total="10" complexity="0.0" line-rate="0.00593031875463"
         line-total="1000" name="package_one">
            <classes>
                <class branch-rate="0.0" complexity="0.0"
                 filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
                    <lines>
                        <line branch="false" hits="0" number="128" condition-coverage="100%"/>
                    </lines>
                </class>
            </classes>
        </package>
    </packages>
</coverage>

生成想要的正确结果

<coverage branch-rate="0.53968253968278" branch-total="50" line-rate="0.66864343958488" line-total="2000" timestamp="1346297959" version="gcovr 2.3">
   <sources>
      <source>
        ./.
        </source>
   </sources>
   <packages>
      <package branch-rate="0.52380952380967" branch-total="15" line-rate="0.75148257968866" line-total="1000" complexity="0.0" name="package_one">
         <classes>
            <class branch-rate="0.0" complexity="0.0" filename="eBayBotAPI.cpp" line-rate="0.0" name="BotAPI.cpp">
               <lines>
                  <line branch="false" hits="0" number="128" condition-coverage="100%"/>
                  <line branch="false" hits="0" number="192" condition-coverage="100%"/>
               </lines>
            </class>
         </classes>
         <class branch-rate="0.5" complexity="0.0" filename="good.cpp" line-rate="1.0" name="good1.cpp">
            <lines>
               <line branch="false" hits="96" number="8" condition-coverage="100%"/>
            </lines>
         </class>
      </package>
   </packages>
</coverage>

解释

正确使用和覆盖identity rule


<强> II。使用密钥的解决方案:

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

 <xsl:key name="kElemByName" match="*" use="name()"/>

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

 <xsl:template match=
 "package[not(generate-id()=generate-id(key('kElemByName', name())[1]))]"/>
</xsl:stylesheet>

当在同一个XML文档(上面)上应用此转换时,会生成相同的想要的正确结果。

<强>解释

正确使用identity rule Muenchian grouping


<强> III。 XSLT 2.0解决方案:

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

 <xsl:key name="kElemByName" match="*" use="name()"/>

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

 <xsl:template match=
 "package[not(. is (//package)[1])]"/>
</xsl:stylesheet>

<强>解释

正确使用XPath 2.0运算符 is