2011-01-27 12 views
2

XSLTを記述して、最も近い辞書値の値を与えノードに取得する必要があります。例えば、私の構造は以下のように条件付き兄弟値

<rootnode> 
<rootcontainer> 
    <dictionary> 
    <key1> value /<key1> 
    </dictionary> 
    <pages> 
    <page1> 
    <!--xslt goes here--> 
    </page1> 
    </pages> 
</rootcontainer> 
<dictionary> 
    <key1> 
    independent value 
    </key1> 
    <key2> 
    value 2 
    </key2> 
</dictionary> 
</rootnode> 

私は、変数$key1page1内部$key2を作成することができます。 $key1の値は「値」になり、$key2の値は「値2」になります。 rootcontainer\dictionary\key1が存在しない場合、$key1の値は「独立した値」になります。

これは意味があると思います。

+0

良い質問、+1。最もコンパクトで洗練された(これまでの)1つの1ライナーのXPathソリューションについては、私の答えをご覧ください。 :) –

+0

あなたの質問は不明です。 * "与えられた[n]ノード"に最も近い辞書値の値を取得する*ためには、入力サンプルノードとコンテキストノードを提供する必要があります。 * '変数' $ key1'と '$ key'の値を出力したいと言っているのでなければ、' page1'内の変数$ key1と$ key2を作成します。要素 'page1'コンテンツテンプレート。 –

答えて

0

私は質問を理解していないが、あなたは2つの方法で変数に条件値を割り当てることができます。succintly

<xsl:choose> 
    <xsl:when test="your test condition"> 
     <xsl:variable name="key1" select="your value"> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:variable name="key1" select="your alternative value"> 
    </xsl:otherwise> 
</xsl:choose> 

以上:

<xsl:variable name="key1" select="if(your test condition) then your value else your alternative value;"/> 

は更新日:おかげで質問の更新のために。私は今すぐ行くよ。

<xsl:template match="page1"> 
<xsl:choose> 
    <xsl:when test="../../preceding-sibling:dictionary[1]/key1"> 
     <xsl:variable name="key1" select="../../preceding-sibling:dictionary[1]/key1"> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:variable name="key1" select="../../../following-sibling:dictionary[1]/key1"> 
    </xsl:otherwise> 
</xsl:choose> 
</xsl:template> 

存在しないのであれば$key1の値が1である場合、先行辞書に<key1>ノードであり、そして次の辞書に<key1>ノードう。あれは正しいですか?

(。あなたがしたい場合は、あまりにもif/then/else構造を使用することができますが、それはおそらく、読みやすくなりますので、私はxsl:chooseを使用)

+0

こんにちはBiziclop ..応答のためにありがとう...私は私のXML構造を置くために投稿を更新しました。希望はもう少し明確になります。私の問題は、このkey1の変数の名前は辞書の下のノードの名前から取られなければならず、選択値は問題のノードからの最も近いkey1の値でなければならないということです。 –

+0

@Technocrat Aspireいいえ、それを取得しないでください。同じ要素名の次の要素が欲しいですか? – biziclop

+0

この**各 'key1'変数**は' xsl:when'と 'xsl:otherwise'の間にありますが、' xsl:choose'の外側にはありません。 –

2

ここでは、必要な変数を定義するコンパクトな方法である:

<xsl:variable name="vKey1" select= 
    "(/*/rootcontainer/dictionary/key1 
    | 
    /*/dictionary/key1 
    ) 
    [1] 
    "/> 

<xsl:variable name="vKey2" select= 
    "(/*/rootcontainer/dictionary/key2 
    | 
    /*/dictionary/key2 
    ) 
    [1] 
    "/> 

簡単なXSLTスタイルシートに包まれた:

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

<xsl:variable name="vKey1" select= 
    "(/*/rootcontainer/dictionary/key1 
    | 
    /*/dictionary/key1 
    ) 
    [1] 
    "/> 

<xsl:variable name="vKey2" select= 
    "(/*/rootcontainer/dictionary/key2 
    | 
    /*/dictionary/key2 
    ) 
    [1] 
    "/> 

<xsl:template match="/"> 
    Key1: <xsl:value-of select="$vKey1"/> 
    Key2: <xsl:value-of select="$vKey2"/> 
</xsl:template> 
</xsl:stylesheet> 

、提供XML文書(それがひどく奇形たように、補正された)上に塗布:希望、正しい結果がを生成さ

<rootnode> 
    <rootcontainer> 
     <dictionary> 
      <key1> value </key1> 
     </dictionary> 
     <pages> 
      <page1> </page1> 
     </pages> 
    </rootcontainer> 
    <dictionary> 
     <key1> independent value </key1> 
     <key2> value 2 </key2> 
    </dictionary> 
</rootnode> 

Key1: value 
    Key2: value 2 

説明

式:

(/*/rootcontainer/dictionary/key1 
| 
/*/dictionary/key1 
) 
    [1] 

意味:

は、ドキュメント順で最初の1を取る(潜在的に)二つの要素のノードセットを取り、そこから。

これらの2つの要素のうちの2番目の要素がドキュメントの順序で後に来るため、最初の(選択された)ものになります.Union(|)演算子を囲む2つのXPath式のうち、素子。

+0

+1。非常に簡潔です。 – Flack

関連する問題