2016-12-30 5 views
0

私は私の入力XMLがあり、その属性値を使用して、2つの段落を削除したい、私は以下の事を試してみましたが、それは出力で発生だ、属性値を使用して要素を含むコンテンツを削除するにはどうすればよいですか?

:私が使用

<chapter outputclass="chapter-Body"> 
<body class="- chapter/body "> 
<p class="- chapter/p ">why now?</p> 
<p class="- chapter/p " outputclass="Image_Size">Banner</p> 
<fig> 
<image href="3.jpg" outputclass="Fig-Image_Ref"/> 
</fig> 
<p class="- chapter/p ">But for others, it may not be.</p> 
<image ref="4.png" outputclass="Image_Ref"/> 
<p class="- chapter/p " outputclass="Image_Size">Small</p> 
<p class="- chapter/p ">As result</p> 
</body> 
</chapter> 

XSL:

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

<xsl:template match="chapter[@outputclass='chapter-Body']"> 
<body> 
<text> 
<text_top><p><xsl:value-of select="body/fig/preceding-sibling::p[1]"/></p> 
</text_top> 

<text_bottom><p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p> 
    <p><xsl:value-of select="body/fig/following-sibling::p[2]"/></p> 
</text_bottom> 
</text> 
<xsl:apply-templates/> 
</body> 
</xsl:template> 

<xsl:template match="p[@outputclass='Image_Size']"/> 

</xsl:stylesheet> 

XML出力私が得たとして:

<body> 
<text> 
<text_top> 
<p>Banner</p> 
</text_top> 
<text_bottom> 
<p>But for others, it may not be.</p> 
<p>Small</p> 
</text_bottom> 
</text> 
</body> 

しかし、私は次のようになると期待しています:

<body> 
<text> 
<text_top> 
<p>why now?</p> 
</text_top> 
<text_bottom> 
<p>But for others, it may not be.</p> 
<p>As result</p> 
</text_bottom> 
</text> 
</body> 

私はその特定の2つのパラの適用テンプレートを使用しましたが、出力されています。同じXSLTを使用してこれを修正するにはどうすればよいですか?

+0

私はを外し、角括弧で数値を変更しようとします。それはうまくいくかもしれない。この理論をテストするのに適切な環境を用意していないのですが、うまくいくと思うでしょう。 – barbwire

答えて

2

先行兄弟軸の順序はドキュメントの逆順であるため、body/fig/preceding-sibling::p[1]を実行すると、fig要素の直前にあるpが取得されます。あなたは前にそれが必要なので、あなたはbody/fig/preceding-sibling::p[2]をやっているはずです。

同様に、figの後に1番目と3番目のp要素が必要なので、それに合わせてインデックスも調整する必要があります。 p[@outputclass='Image_Size']と一致する現在のテンプレートは、XSLT内にxsl:apply-templatesを選択するものがないため、まったく使用されません。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="chapter[@outputclass='chapter-Body']"> 
     <body> 
     <text> 
      <text_top> 
       <p><xsl:value-of select="body/fig/preceding-sibling::p[2]"/></p> 
      </text_top> 

      <text_bottom> 
       <p><xsl:value-of select="body/fig/following-sibling::p[1]"/></p> 
       <p><xsl:value-of select="body/fig/following-sibling::p[3]"/></p> 
      </text_bottom> 
     </text> 
     </body> 
    </xsl:template> 
</xsl:stylesheet> 

が、それはあなたが達成しようとしているかに見える、と述べた。このXSLTを試してみてくださいimage_sizeでは「のoutputclassを持つものを除いて、figの前に、次のすべてのpの要素を選択することです"

この場合は、このより汎用的なXSLTをお試しください。最初のテンプレートにxsl:apply-templatesがあるため、p[@outputclass='Image_Size']に一致するテンプレートがどのように使用されているかに注意してください。

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
    <xsl:output method="xml" indent="yes"/> 

    <xsl:template match="chapter[@outputclass='chapter-Body']"> 
     <body> 
     <text> 
      <text_top> 
       <xsl:apply-templates select="body/fig/preceding-sibling::p" /> 
      </text_top> 

      <text_bottom> 
       <xsl:apply-templates select="body/fig/following-sibling::p" /> 
      </text_bottom> 
     </text> 
     </body> 
    </xsl:template> 

    <xsl:template match="p[@outputclass='Image_Size']"/> 

    <xsl:template match="p"> 
     <p><xsl:value-of select="." /></p> 
    </xsl:template> 
</xsl:stylesheet> 
+0

ありがとう@Tim。私はあなたが提供した2番目のメソッドのXSLが欲しいですが、私は2番目のメソッドを使用している間、パラのタグは2回来ています。私に提案をお願いします。 – User501

+0

paraタグは2番目の方法、例えばhttp://xsltransform.net/6rewNymを参照して2回出てきません。オリジナルのXSLTに表示される ' 'を削除してください。 –

+0

はい。あなたが正しいです。今は正しく動作しています。ありがとう@Tim – User501

関連する問題