2017-08-23 11 views
1

私はかなりグーグルであり、特定の子ノードの特定の属性を削除する方法を理解することはできません。この例では、「名前」属性を削除したいのですが、「代替」親アイテムの下にある場合に限り、他の場所では削除できません。この例では、例えば参照の下で、「名前」属性を保持しますが、このXMLXSLT:特定の子どもの属性を削除する方法

<products> 
<product id="123456"> 
    <alternate-products> 
     <alternate> 
     <number>2002</number> 
     <name>2002</name>   <-- want to remove this one 
     </alternate> 
     <alternate> 
     <number>2002</number> 
     <name>2002</name>   <--- remove this one too 
     </alternate> 
    </alternate-products> 
    <references> 
     <reference> 
     <name>2002</name>   <-- keep this one - not under alternate 
     <date>2002</date> 
     </reference> 
    </references> 
</products> 

理想のXMLで.....代替下

スタートしたものを削除します。

<products> 
    <product id="123456"> 
    <alternate-products> 
     <alternate> 
     <number>2002</number> 
     </alternate> 
     <alternate> 
     <number>2002</number> 
     </alternate> 
    </alternate-products> 
    <references> 
     <reference> 
     <name>2002</name>   <-- still there - good! 
     <date>2002</date> 
     </reference> 
    </references> 
</products> 

誰でもいくつかのヒントを提供できますか?

+0

用語は重要です: 'あなたのXMLでNAME'は*要素*、*ない属性*です。 –

答えて

-2

あなたはこれを試すことができます。

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

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

    <xsl:template match="alternate/name"/> 
</xsl:stylesheet> 
+0

これはOPが求めていることをしません - 結果は偶然によってのみ "正しい"です。 –

+0

ありがとうございます。 – Rupesh

+0

なぜあなたは私のコピーではなくあなたの答えを削除しないでください。 –

1

が、私は「名前」 属性 要素を削除したいのですが、場合にのみ、「代替」 親項目の下に - ではないどこか他。

行うのは簡単:

XSLT 1.0

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

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

<xsl:template match="alternate/name"/> 

</xsl:stylesheet> 
+0

これはトリックでした。この回答のためにあなたとルペシュの両方に感謝します! KDS

関連する問題