2009-05-17 11 views
0

"blog"ノード以外のすべての子孫を選択したいと思います。この例では、出力にはサブツリーのみが表示されます。このXMLのxsl filter on select

<xsl:template match="rdf:RDF"> 
    <xsl:copy>  
     <xsl:copy-of select="descendant::*[not(descendant::blog)]"/> 
    </xsl:copy> 
    </xsl:template> 

: 私はこのXSLコードをしようとしている

<rdf:RDF> 
    <profesor rdf:ID="profesor_39"> 
    <nombre rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
    >Augusto</nombre> 
    </profesor> 
    <blog rdf:ID="blog_41"> 
    <entradas> 
     <entrada_blog rdf:ID="entrada_blog_42"> 
     <etiquetas> 
      <tecnologia rdf:ID="tecnologia_49"> 
      <termino rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
      >Atom</termino> 
      </tecnologia> 
     </etiquetas> 
     <autor> 
      <alumno rdf:ID="alumno_38"> 
      <nombre rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
      >Jesus</nombre> 
      </alumno> 
     </autor> 
     </entrada_blog> 
    </entradas> 
    <autores rdf:resource="#alumno_38"/> 
    <direccion rdf:datatype="http://www.w3.org/2001/XMLSchema#string" 
    >http://tfg1.unex.es/10comunidad/wordpress/</direccion> 
    </blog> 
</rdf:RDF> 

は、私が何をしないのですか? "ブログ"ノードは依然として出力に印刷されます。 ありがとうございます。ブログとそのすべての子を省略する

答えて

1

<xsl:template match="RDF"> 
    <xsl:copy-of select="child::node()[name() != 'blog']"/> 
</xsl:template> 

その子のブログを省略したが、まだO/Pに:

<xsl:template match="RDF"> 
    <xsl:copy-of select="descendant::node()[name() != 'blog']"/> 
</xsl:template> 
+1

2つ目は結果を爆発させます。おそらくあなたが望むものではありません。 とその子孫(などを含む)をすべてコピーします。 *すると、とその子孫がコピーされます。だから、たくさんの重複したサブツリーで終わるだろう。修正された恒等変換(私の答えを見てください)はどちらの場合でも最大の力を与えます。 –

5

はここに完全なソリューションです:

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

    <!-- By default, recursively copy all nodes unchanged --> 
    <xsl:template match="@* | node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@* | node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <!-- But strip out <blog> --> 
    <xsl:template match="blog"/> 

    <!-- If you want to strip out just the <blog> start and end tags, use this instead: 
    <xsl:template match="blog"> 
    <xsl:apply-templates/> 
    </xsl:template> 
    --> 

</xsl:stylesheet> 
0

condition not(descendant :: blog)は、 "blog"という名前の子孫を持つノードを除外します。

あなたが持っているので、場合:

<blog id="1"> 
    <test id="1.1"> 
    <blog id="1.1.1" /> 
    </test> 
</blog> 
<blog id="2"> 
    <test id="2.1" /> 
</blog> 

それは<ブログIDを除外します= "1" >と<テストID = "1.1" >、これらのノードの両方を持っているので、<ブログID = "1.1.1 "子孫として>。

しかし、彼らは

を「ブログ」という名前の子孫はまた、あなたの選択ということに注意していないので、それは、ID =「2」>を<ブログID =「1.1.1」>または<ブログを除外しません。子孫:: *(子孫::ブログ)]は< test id = "2.1" >を2回出力します。< blog id = "2" >に1回、もう一度それ自身でもう一度出力します。

完全な解決策として、Evan Lenz(「ブログ」ノード用の空白のオーバーライドテンプレートでの恒等変換)が提案したものが、おそらくあなたが望む結果を与えるものです。