使用 XSLT 根据子属性值删除重复标签 [英] Removing duplicate Tag based on child attribute value using XSLT

查看:26
本文介绍了使用 XSLT 根据子属性值删除重复标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

预期输出应使用 XSLT.提前致谢在输入 XML 中,有具有相同子节点的元素,我需要根据标签属性值将其删除.如果两个标签中的属性值(身份"属性)相同,则应删除整个重复标签并只显示一次

Expected output should be using XSLT. Thanks in advance In the input XML ,there are element with same child node, which i need to remove based on the tag attribute value.If same attribute value("identity" attribute) in both tag then the entire duplicate tag should be removed and displayed only once

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>

推荐答案

在 XSLT 3 中,你可以这样做:

In XSLT 3 you could do it as follows:

<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.

XSLT 3 is supported with Saxon 9.8 or later or AltovaXML 2017 R3 and later.

这篇关于使用 XSLT 根据子属性值删除重复标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆