2011-07-03 19 views
4

私は以下のようなxmlを持っています。それが結果xml.Ifであると私は上記のブロックをコピーする必要が上記のブロックを取得すると私は値XSLT null比較

​​

と、以下のブロックを得る

<attributes> 
     <attribute> 
      <attributeName>agenda-group</attributeName> 
      <value>generic</value> 
     </attribute> 
     <attribute> 
      <attributeName>auto-focus</attributeName> 
      <value>true</value> 
     </attribute> 
     <attribute> 
      <attributeName>no-loop</attributeName> 
      <value>true</value> 
     </attribute> 
     <attribute> 
      <attributeName>salience</attributeName> 
      <value>73</value> 
     </attribute> 
    </attributes> 

私はこのブロックを省略する必要があります私の結果のxml.私は翻訳のためにxsltを使用しています。 希望の出力を得るためのいくつかのポインタを提供してください。

答えて

1

identity templateを使用して、これらのテンプレートを追加します。

<xsl:template match="attributes[not(attribute/value/text())]" /> 
<xsl:template match="attribute[not(value/text())]" /> 

は、これらの2つの空のテンプレートは、効果的にそれらを削除し、値を持たないし、彼らのために何も出力されない<attributes><attribute>要素をキャッチ。

-1

これを試してみてください:

<xsl:for-each select="//attributes[descendant::attribute]"> 
    some stuff 
</xsl:for-each> 
0

この変換を:(末尾の空<attributes>attribute/attributeNameに注意してください)このXMLドキュメントに適用

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

<xsl:template match= 
"attributes[not(node())] 
| 
    attribute[not(attributeName/text())] 
"/> 
</xsl:stylesheet> 

<attributes> 
    <attribute> 
     <attributeName>agenda-group</attributeName> 
     <value>generic</value> 
    </attribute> 
    <attribute> 
     <attributeName>auto-focus</attributeName> 
     <value>true</value> 
    </attribute> 
    <attribute> 
     <attributeName>no-loop</attributeName> 
     <value>true</value> 
    </attribute> 
    <attribute> 
     <attributeName>salience</attributeName> 
     <value>73</value> 
    </attribute> 

    <attribute> 
     <attributeName></attributeName> 
     <value></value> 
    </attribute> 

    <attributes/> 
</attributes> 

(空の要素は無視 - コピーされていない)所望の結果生成:

<attributes> 
    <attribute> 
    <attributeName>agenda-group</attributeName> 
    <value>generic</value> 
    </attribute> 
    <attribute> 
    <attributeName>auto-focus</attributeName> 
    <value>true</value> 
    </attribute> 
    <attribute> 
    <attributeName>no-loop</attributeName> 
    <value>true</value> 
    </attribute> 
    <attribute> 
    <attributeName>salience</attributeName> 
    <value>73</value> 
    </attribute> 
</attributes> 

説明:(コピー各ノード「であるように、」こと)単一のテンプレートによってオーバーライドされるアイデンティティ・ルールをその必要な「空の」要素に一致し、本体を持たないため、単純にスキップ/無視されます。

+0

私はあなたが想定しているようOPが必要とする入力文書があるか分かりません。私の解釈については私の答えを見てください。 –

+0

@empo:はい、これは私の質問に対する私の解釈です。値が空/不足の場合でも、名前と値のペアは意味があります。 –

0

次変換は省略することにより、出力内の任意のattributesをコピーします:

  • attributes
  • attributesattribute子供(または空のサブ要素を持つ)
  • 任意の空の子供attributeのみ空に(または空の下位要素を含む)

XSLT 1.0

01以下の入力に適用さ
<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:template match="node()|@*"> 
    <xsl:copy> 
    <xsl:apply-templates select="node()|@*"/> 
    </xsl:copy> 
</xsl:template> 

<xsl:template match="attributes[not(*)] 
    | 
    attributes[count(*)=count(attribute[.=''])] 
    | 
    attribute[.='']"/> 

</xsl:stylesheet> 

<root> 
    <attributes> 
     <attribute> 
      <attributeName>agenda-group</attributeName> 
      <value>generic</value> 
     </attribute> 
     <attribute/> 
     <attribute> 
      <attributeName></attributeName> 
      <value></value> 
     </attribute> 
     <attribute> 
      <attributeName>salience</attributeName> 
      <value>73</value> 
     </attribute> 
    </attributes> 
    <attributes> 
     <attribute> 
      <attributeName></attributeName> 
      <value></value> 
     </attribute> 
    </attributes> 
    <attributes/> 
</root> 

は生成します。

<root> 
    <attributes> 
     <attribute> 
     <attributeName>agenda-group</attributeName> 
     <value>generic</value> 
     </attribute> 
     <attribute> 
     <attributeName>salience</attributeName> 
     <value>73</value> 
     </attribute> 
    </attributes> 
</root> 
+0

属性タグに基づいてxmlを検証する必要があります。属性タグがnullです。実行時にjavaを使用して例外を生成する必要があります。同じ方法を実現する方法。私にいくつかの指摘をしてください。 – thogadam

+0

@thogなぜJavaで質問にタグを付けていないのですか? –

+0

あなたのJavaアプリケーションの@thogでは、不要な属性タグを数えるために単一のXPath式を使うことができます(たとえば、 'count> 0')。それは良いと思いますか? –