2016-05-31 101 views
1

XMLファイルの現在の日付の値に応じて、配列内の特定の要素にアクセスしようとしています。配列XSLTの変数インデックス

<CurrentMonth>5</CurrentMonth> 

次に、XSLTで

例えば、XMLに - これは

<xsl:variable name="current-month"> 
     xsl:value-of select="//CurrentMonth" /> 
    </xsl:variable> 

として変数として設定されている私はまた、「月名」の配列を宣言しています

<xsl:variable name="array" as="element()*"> 
    <Item>Jan</Item> 
    <Item>Feb</Item> 
    <Item>Mar</Item> 
    <Item>Apr</Item> 
    <Item>May</Item> 
    <Item>Jun</Item> 
    <Item>Jul</Item> 
    <Item>Aug</Item> 
    <Item>Sept</Item> 
    <Item>Oct</Item> 
    <Item>Nov</Item> 
    <Item>Dec</Item> 
</xsl:variable> 

XSLTでは、変数をインデックスとして使用して月の名前(たとえば「Jan」)を返すことは可能ですか配列?

例:

<xsl:value-of select="$array[$current-month]"> 

上記のコードは、事前に私に

[FATAL]: Error checking type of the expression 'filter-expr(variable-ref(array/result-tree) 

感謝を投げています。

+0

XSLT 1.0または2.0のどちらかを選択してください、両方ではありません。 –

+0

申し訳ありません、これはXSLT 2.0です – Cdok

答えて

2

あなたは、いくつかの構文エラーがあります:あなたが持っている

<xsl:variable name="current-month" select="//CurrentMonth" /> 

次へ:

<xsl:value-of select="$array[$current-month]"> 
をする

<xsl:variable name="current-month"> 
    xsl:value-of select="//CurrentMonth" /> 
</xsl:variable> 

ニーズを:

<xsl:variable name="current-month"> 
    <xsl:value-of select="//CurrentMonth" /> 
</xsl:variable> 

または好ましくは0 クローズする必要があり

<xsl:value-of select="$array[$current-month]"/> 

をして、あなたは変数を定義する最初の形式を使用している場合には、それはする必要があります:

<xsl:value-of select="$array[number($current-month)]"> 
+1

シナリオを再入力して謝罪したため、構文エラーはスタックオーバーフローにすぎませんでした。ご迷惑をおかけしましたが、お詫び申し上げます。 $ array [number($ current-month)]はそれを解決しました。私。 ありがとう! – Cdok

1

変数を<xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/>と定義すると、$array[$current-month](配列ではなく配列ではありますが)を使用できます。あなたのコードでは$array[position() = $current-month]が必要です。サクソンと私のために正常に動作

最小限のが、完全なスタイルシートは、入力<CurrentMonth>5</CurrentMonth>に対して実行とき、彼は

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:xs="http://www.w3.org/2001/XMLSchema" 
    exclude-result-prefixes="xs" 
    version="2.0"> 

    <xsl:variable name="array" as="element()*"> 
     <Item>Jan</Item> 
     <Item>Feb</Item> 
     <Item>Mar</Item> 
     <Item>Apr</Item> 
     <Item>May</Item> 
     <Item>Jun</Item> 
     <Item>Jul</Item> 
     <Item>Aug</Item> 
     <Item>Sept</Item> 
     <Item>Oct</Item> 
     <Item>Nov</Item> 
     <Item>Dec</Item> 
    </xsl:variable> 

    <xsl:variable name="current-month" select="xs:integer(//CurrentMonth)"/> 

    <xsl:template match="/"> 
     <xsl:value-of select="$array[$current-month]"/> 
    </xsl:template> 

</xsl:stylesheet> 

出力Mayである9.6.0.7。

+0

私はあなたの最初の提案を試みて、受け取った [エラー]:名前空間接頭辞 'xs'は宣言されていません。 – Cdok

+1

はい、まあ、 'xs:integer'のようなスキーマデータ型を使うには、スタイルシートのルートに' xmlns:xs = "http://www.w3.org/2001/XMLSchema" 'を宣言する必要があります。 –

+0

これは今私を投げています "[ERROR]:非静的なJava関数 'integer'の最初の引数は有効なオブジェクト参照ではありません。 [エラー]スタイルシートをコンパイルできませんでした [FATAL]式 'filter-expr(variable-ref(array/result-tree)、[pred(variable-ref(current-month/void)) ]) '。 " – Cdok

関連する問題