1

XSLT V1.0で、重複したノードの削除に問題があります。私はこれをエントリー用に持っています

    <?xml version="1.0" encoding="utf-8"?>
<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>

私はこのXSLファイルを持っています

    <?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:output method="xml" encoding="utf-8" indent="yes"/>

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

    <xsl:template match="Mappings">
        <xsl:if test="not(following::Mappings[Mapping/@fieldName=current()/Mapping/@fieldName])">
            <xsl:copy>
                <xsl:apply-templates select="@*|node()"/>
            </xsl:copy>
        </xsl:if>       
    </xsl:template>


</xsl:stylesheet>

そして、結果として同じエントリXMLファイルがあります!!

重複したノード()を取り除くにはどうすればよいですか?

私はすべてを試しましたが、結果はありません:(

xslt Transformを使用してxml内の重複を削除し、重複を削除して残りをコピー してみ ましたXSLT XSLT 1.0テキストリストを使用して連続する重複を個々の要素に削除し、重複を削除します

.....。

この結果を得るにはどうすればよいですか?

 <?xml version="1.0" encoding="utf-8"?>
    <myRoot xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <Mappings>
            <Mapping fieldName="field1">
            </Mapping>
            <Mapping fieldName="field2" >
            </Mapping>
            <Mapping fieldName="field3" >
            </Mapping>
            <Mapping fieldName="field4">
            </Mapping>
        </Mappings>
    </myRoot>

ありがとう

4

2 に答える 2

1

解決策は非常に単純です (名前付きテンプレートも使用せず、xsl:call-templateテンプレートを 2 つだけ使用し、完全に「プッシュ スタイル」) :

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

この変換が提供された XML ドキュメントに適用されると、次のようになります。

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

必要な正しい結果が生成されます。

<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>
于 2013-03-22T04:04:14.057 に答える