XSLT 1.0 - Remove duplicates fields

穿精又带淫゛_ 提交于 2019-12-06 21:01:26

The solution is very simple (no named templates and no use of xsl:call-template, only two templates, completely "push style"):

<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="kFieldNameByVal" match="@fieldName" use="."/>

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

  <xsl:template match=
   "Mapping[not(generate-id(@fieldName)
           = generate-id(key('kFieldNameByVal', @fieldName)[1]))]"/>

</xsl:stylesheet>

When this transformation is applied on the provided XML document:

<myRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Mappings>
        <Mapping fieldName="field1" >
        </Mapping>
        <Mapping fieldName="field1">
        </Mapping>
        <Mapping fieldName="field2" >
        </Mapping>
        <Mapping fieldName="field3" >
        </Mapping>
        <Mapping fieldName="field4">
        </Mapping>
    </Mappings>
</myRoot>

the wanted, correct result is produced:

<myRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <Mappings>
      <Mapping fieldName="field1"/>
      <Mapping fieldName="field2"/>
      <Mapping fieldName="field3"/>
      <Mapping fieldName="field4"/>
   </Mappings>
</myRoot>

The problem here is that your template is matching Mappings and attempting to exclude following duplicate Mappings elements, but there are none.

In either case, following:: and preceding:: are not good ways to select distinct values in XSLT. Instead, you should use Muenchian grouping:

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

  <xsl:key name="kMapping" match="Mapping" use="@fieldName"/>

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

  <xsl:template match="Mapping[generate-id() = 
                               generate-id(key('kMapping', @fieldName)[1])]">
    <xsl:call-template name="Copy" />
  </xsl:template>
  <xsl:template match="Mapping" />
</xsl:stylesheet>

When run on your sample input, this produces:

<myRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  <Mappings>
    <Mapping fieldName="field1" />
    <Mapping fieldName="field2" />
    <Mapping fieldName="field3" />
    <Mapping fieldName="field4" />
  </Mappings>
</myRoot>
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!