2016-04-25 20 views
0

次のXMLを考える:私はタイトルといくつかの新しいタグでの次の内容をラップ各段落部に動作するようにテンプレートを実行したいXPath1 - 選択兄弟

<p class='sectiontitle'>Examples</p> 
<p class='paragraphtitle'>Example1</p> 
<p>That's some text.</p> 
<p>That's some text again.</p> 
<p class='paragraphtitle'>Example2</p> 
<p>That's some other text.</p> 
<p>That's some other text again.</p> 
<!-- potentially add more paragraphtitles --> 

<p class='sectiontitle'>New title</p> 
<p>Some non-needed text.</p> 

を。

私はすべてのp [@ class = 'paragraphtitle']を選択するXSLT1.1/Xpath1ファイルを持っています。テンプレートの中で次の字幕またはセクションタイトルまで次の兄弟を選択します。

私は期待してこのような場合には、期待される出力を生成しません
<xsl:template match="p[@class='paragraphtitle']" mode="create-example-block"> 
    <example> 
    <title> 
     <xsl:value-of select="." /> 
    </title> 
    <xsl:apply-templates 
     select="//p[@class='sectiontitle' or @class='paragraphtitle']/preceding-sibling::*[preceding-sibling::p[@class='paragraphtitle'][1] = text() and not(self::p[@class='sectiontitle' or @class='paragraphtitle'] or not(self::*[following-sibling::p[@class='sectiontitle']]))]" /> 
    </example> 
</xsl:template> 

<example> 
    <title>Example1</title> 
    <p>That's some text.</p> 
    <p>That's some text again.</p> 
</example> 
<example> 
    <title>Example2</title> 
    <p>That's some other text.</p> 
    <p>That's some other text again.</p> 
</example> 

は誰が私にヒントを与えることはできますか?

+0

HTTPを参照してください。 ://stackoverflow.com/a/36781226/252228などの質問と回答があります。 –

答えて

1

あなたのテンプレートに、この小さな変更を試みること - 最初の兄弟の前にあるように、現在の要素を持っているすべての次の兄弟を探して :以下の出力で

<xsl:template match="p[@class='paragraphtitle']" mode="create-example-block"> 
     <example> 
      <title> 
       <xsl:value-of select="." /> 
      </title> 
      <xsl:variable name="thisgid" select="generate-id(.)" /> 
      <xsl:apply-templates select="following-sibling::p[ not (@class='sectiontitle' or @class='paragraphtitle')] 
          [generate-id(preceding-sibling::p[ @class='paragraphtitle' or @class='sectiontitle'] [1]) = $thisgid ]"/>    
     </example> 
    </xsl:template> 

 <example> 
      <title>Example1</title> 
      <p>That's some text.</p> 
      <p>That's some text again.</p> 
     </example> 
     <example> 
      <title>Example2</title> 
      <p>That's some other text.</p> 
      <p>That's some other text again.</p> 
     </example> 
+0

こんにちは、hr_117、おかげで、私の最後にはほとんど働いています。 最初のサンプルブロックで正しい結果が得られましたが、2番目のサンプルブロックで失敗しました。次の

'要素の次の要素もキャプチャされているようです。 – Flag

+0

更新を見てください(前の兄弟としてのセクションタイトルも考慮してください) –

+0

偉大な、それは動作します!大変ありがとうございました。あなたは大きな助けになりました! :) – Flag

関連する問題