11

xpathを使用して完全なドキュメント内の親ノードの位置を取得するには?

次のxmlがあるとします:

<catalog>
  <cd>
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd>
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

そして、それを HTML に変換するための XSLT があります。これは次のとおりです (スニペットのみ)。

<xsl:template match="/">
<html>
  <body>  
  <xsl:apply-templates/>  
  </body>
  </html>
</xsl:template>

<xsl:template match="cd">
  <p>
    <xsl:number format="1. "/><br/>
    <xsl:apply-templates select="title"/>  
    <xsl:apply-templates select="artist"/>
  </p>
</xsl:template>

<xsl:template match="title">
  <xsl:number format="1" select="????" /><br/>
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

????の所には何を書けばいいですか?ドキュメント内の親 cd タグの位置を取得します。多くの表現を試しましたが、何も機能していないようです。私はそれを完全に間違っているかもしれません。

  1. <xsl:number format="1" select="catalog/cd/preceding-sibling::..[position()]" />
  2. <xsl:number format="1" select="./parent::..[position()]" /><br/>
  3. <xsl:value-of select="count(cd/preceding-sibling::*)+1" /><br/>

現在のノードの親軸を選択し、現在のノードの親の位置を伝えるように2番目を解釈しています。なぜ機能しないのですか?これを行う正しい方法は何ですか。

参考までに: 現在のタイトル タグ uder 処理の親 cd タグの位置を出力するコードを期待しています。

誰かがこれを行う方法を教えてください。

4

3 に答える 3

22
count(../preceding-sibling::cd) + 1

ここで実行できます(わかりやすくするために、出力していた他の数値を削除したことに注意してください)。

あなたは正しい道を進んでいましたが、述語はノードをフィルタリングするためだけに使用され、情報を返すためではないことに注意してください。そう:

../*[position()]

...効果的に「ポジションを持つ親を見つけてください」と言います。位置自体ではなく、ノードを返します。述語は単なるフィルターです。

いずれにせよ、 using には落とし穴があり、別のノードではなくposition()、現在のコンテキスト ノードの位置のみを返すために使用できます。

于 2012-06-30T09:40:12.250 に答える
4

Utkanos の答えは問題なく機能しますが、私の経験では、xml ドキュメントが大きい場合、パフォーマンスの問題が発生する可能性があります。

このシナリオでは、親の位置をパラメーターで渡すだけです。

<xsl:template match="/">
<html>
  <body>  
  <xsl:apply-templates/>  
  </body>
  </html>
</xsl:template>

<xsl:template match="cd">
  <p>
    <xsl:number format="1. "/><br/>
    <xsl:apply-templates select="title">  
        <xsl:with-param name="parent_position" select="position()"/> <!-- Send here -->
    </xsl:apply-templates>
    <xsl:apply-templates select="artist"/>
  </p>
</xsl:template>

<xsl:template match="title">
  <xsl:param name="parent_position"/> <!-- Receive here -->
  <xsl:number format="1" select="$parent_position"/><br/>
  Title: <span style="color:#ff0000">
  <xsl:value-of select="."/></span>
  <br />
</xsl:template>

結果:

<html xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><body>
  <p>1. <br>1<br>
  Title: <span style="color:#ff0000">Empire Burlesque</span><br>Bob Dylan</p>
  <p>2. <br>1<br>
  Title: <span style="color:#ff0000">Hide your heart</span><br>Bonnie Tyler</p>
</body></html>
于 2015-05-16T19:38:47.407 に答える
1
  <xsl:number format="1" select="????" /> 

の場所に何を書けばいいの???? ドキュメント内の親cdタグの位置を取得します。

まず第一に、上記のXSLT命令は構文的に違法です-命令は属性<xsl:number>を持っていません(できません)select

使用

   <xsl:number format="1" count="cd" /> 
于 2012-06-30T15:57:48.960 に答える