-1

如果名称重复,我如何删除类型包的节点。我的意思是应该保留第一次出现,而应该删除其他的。看到 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>

4

1 回答 1

2

I. 这个简单的 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>

说明

正确使用和覆盖身份规则


二、使用密钥的解决方案

<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 文档(上图)时,会产生同样想要的正确结果。

说明

正确使用同一性规则孟池分组


三、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 运算符.

于 2012-09-13T04:35:32.190 回答