2017-09-05 15 views
1

私はヘルプを探しています 空ノードを削除するためにXSLを書いていますが、動作していますが、XMLノードの1つでxsi:xsi = trueがある場合、そのノード、私は空のノードを削除するスタイルシート、XSIを含み、空の属性やノードを必要とする:XSI =真XSLTを使用して空のXMLノードを削除する

INPUT XML

<root> 
<para> 
<Description>This is my test description</Description> 
<Test></Test> 
<Test1 attribute="1"/> 
<Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
</para> 
</root> 

出力XML

<root> 
<para> 
<Description>This is my test description</Description> 
<Test1 attribute="1"/> 
<Received_Date xsi:nil="true" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/> 
</para> 

予想される出力

<root> 
<para> 
<Description>This is my test description</Description> 
<Test1 attribute="1"/> 
</para> 
</root> 

XSLコードは

<?xml version="1.0" ?> 

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes" /> 
    <xsl:template match="node()|SDLT"> 
     <xsl:if test="count(descendant::text()[string-length(normalize-space(.))>0]|@*)"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()" /> 
      </xsl:copy> 
     </xsl:if> 

    </xsl:template> 
    <xsl:template match="@*"> 
     <xsl:copy /> 
    </xsl:template> 
    <xsl:template match="text()"> 
     <xsl:value-of select="normalize-space(.)" /> 
    </xsl:template> 

</xsl:stylesheet> 
+1

'XSI:nil'は属性であり、そして、あなたのスタイルシートが要素を考慮していません属性は「空」となります。 –

+0

また、なぜSDLTという名前の要素にマッチしますか?この要素は入力ファイルには存在していないようですが、とにかく 'node()'でマッチングすることですでにカバーされています。 –

+0

はい私は同意します、SDLTは必要ありません。私にとっては、属性xsi:nil = trueを持つ要素を削除する必要があります – user2631055

答えて

0

あなたはこれを使用することができます:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    version="1.0"> 
    <xsl:output omit-xml-declaration="yes" indent="yes" /> 
    <xsl:template match="node()|SDLT"> 
     <xsl:if test="(node() or @*) and not(@xsi:nil = 'true')"> 
      <xsl:copy> 
       <xsl:apply-templates select="@*|node()" /> 
      </xsl:copy> 
     </xsl:if> 

    </xsl:template> 

    <xsl:template match="@*"> 
     <xsl:copy /> 
    </xsl:template> 

    <xsl:template match="text()"> 
     <xsl:value-of select="normalize-space(.)" /> 
    </xsl:template> 

</xsl:stylesheet> 
関連する問題