使用XSLT根据子属性值删除重复的标签

时间:2019-07-03 13:12:15

标签: xml xslt-2.0

Have couple of element with same Parent and child node.
So need to remove the duplicates.

预期的输出应使用XSLT。 提前致谢 在输入XML中,存在具有相同子节点的元素,我需要根据标签属性值删除该元素。如果两个标签中的属性值相同(“ identity”属性),则应删除整个重复标签并仅显示一次

Input XML file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MainDeclaration xmlns="http://webstds.ipc.org/175x/2.0" version="2.0">
   <Product comment="1. This is a generic description of the substances as the actual compostion of the substances are either considered proprietary or no official CAS number is available. If a CAS number is given, it is the closest match available." unitType="Each">
      <MaterialInfo>
         <ExemptionList>
            <ExemptionListID authority="IPC" identity="EL2011/534/EU"/>
            <Exemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
         </ExemptionList>
         <ExemptionList>
            <ExemptionListID authority="IPC" identity="EL2011/534/EU"/>
            <Exemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
         </ExemptionList>
         <HomogeneousMaterialList>
            <HomogeneousMaterial materialGroupName="Other Non-ferrous Metals and Alloys" name="Solder Paste 1">
               <Amount UOM="mg" value="7.02"/>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="IPC" identity="EUROHS-0508"/>
                  <SubstanceCategory name="Lead/lead compounds">
                     <Substance name="Lead">
                        <SubstanceID authority="CAS" identity="7439-92-1"/>
                        <Amount UOM="mg" value="6.4935"/>
                        <SubstanceExemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
                     </Substance>
                     <SubstanceCatExemptionList>
                        <ExemptionListID authority="IPC" identity="EL2011/534/EU"/>
                        <Exemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
                     </SubstanceCatExemptionList>
                  </SubstanceCategory>
               </SubstanceCategoryList>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Lead alloy">
                     <Substance name="Silver (Ag)">
                        <SubstanceID authority="CAS" identity="7440-22-4"/>
                        <Amount UOM="mg" value="0.1755"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Lead alloy">
                     <Substance name="Tin (Sn)">
                        <SubstanceID authority="CAS" identity="7440-31-5"/>
                        <Amount UOM="mg" value="0.351"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
            </HomogeneousMaterial>
            <HomogeneousMaterial materialGroupName="Other Non-ferrous Metals and Alloys" name="Post-plating">
               <Amount UOM="mg" value="3.85"/>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Pure metal">
                     <Substance name="Tin (Sn)">
                        <SubstanceID authority="CAS" identity="7440-31-5"/>
                        <Amount UOM="mg" value="3.85"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
            </HomogeneousMaterial>
        </HomogeneousMaterialList>      
     </MaterialInfo>
    </Product>
</MainDeclaration>

Expected Output file:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MainDeclaration xmlns="http://webstds.ipc.org/175x/2.0" version="2.0">
   <Product comment="1. This is a generic description of the substances as the actual compostion of the substances are either considered proprietary or no official CAS number is available. If a CAS number is given, it is the closest match available." unitType="Each">
      <MaterialInfo>        
         <ExemptionList>
            <ExemptionListID authority="IPC" identity="EL2011/534/EU"/>
            <Exemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
         </ExemptionList>
         <HomogeneousMaterialList>
            <HomogeneousMaterial materialGroupName="Other Non-ferrous Metals and Alloys" name="Solder Paste 1">
               <Amount UOM="mg" value="7.02"/>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="IPC" identity="EUROHS-0508"/>
                  <SubstanceCategory name="Lead/lead compounds">
                     <Substance name="Lead">
                        <SubstanceID authority="CAS" identity="7439-92-1"/>
                        <Amount UOM="mg" value="6.4935"/>
                        <SubstanceExemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
                     </Substance>
                     <SubstanceCatExemptionList>
                        <ExemptionListID authority="IPC" identity="EL2011/534/EU"/>
                        <Exemption description="Lead in high melting temperature type solders (i.e., lead based solder alloys containing 85% by weight or more lead)" identity="7(a)"/>
                     </SubstanceCatExemptionList>
                  </SubstanceCategory>
               </SubstanceCategoryList>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Lead alloy">
                     <Substance name="Silver (Ag)">
                        <SubstanceID authority="CAS" identity="7440-22-4"/>
                        <Amount UOM="mg" value="0.1755"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Lead alloy">
                     <Substance name="Tin (Sn)">
                        <SubstanceID authority="CAS" identity="7440-31-5"/>
                        <Amount UOM="mg" value="0.351"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
            </HomogeneousMaterial>
            <HomogeneousMaterial materialGroupName="Other Non-ferrous Metals and Alloys" name="Post-plating">
               <Amount UOM="mg" value="3.85"/>
               <SubstanceCategoryList>
                  <SubstanceCategoryListID authority="Supplier" identity="Supplier"/>
                  <SubstanceCategory name="Pure metal">
                     <Substance name="Tin (Sn)">
                        <SubstanceID authority="CAS" identity="7440-31-5"/>
                        <Amount UOM="mg" value="3.85"/>
                     </Substance>
                  </SubstanceCategory>
               </SubstanceCategoryList>
            </HomogeneousMaterial>
        </HomogeneousMaterialList>      
     </MaterialInfo>
    </Product>
</MainDeclaration>

1 个答案:

答案 0 :(得分:0)

在XSLT 3中,您可以执行以下操作:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    xpath-default-namespace="http://webstds.ipc.org/175x/2.0"
    exclude-result-prefixes="#all"
    version="3.0">

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:mode on-no-match="shallow-copy"/>

  <xsl:template match="*[ExemptionList]">
      <xsl:copy>
          <xsl:apply-templates select="@*"/>
          <xsl:for-each-group select="*" composite="yes" group-by="boolean(self::ExemptionList), Exemption/@identity">
              <xsl:choose>
                  <xsl:when test="head(current-grouping-key())">
                      <xsl:apply-templates select="."/>
                  </xsl:when>
                  <xsl:otherwise>
                      <xsl:apply-templates select="current-group()"/>
                  </xsl:otherwise>
              </xsl:choose>
          </xsl:for-each-group>
      </xsl:copy>
  </xsl:template>

</xsl:stylesheet>

Saxon 9.8或更高版本或AltovaXML 2017 R3或更高版本支持XSLT 3。

相关问题