1

2つのノードの要素を反転する必要があります。元々、変数は次のコマンドで設定されていました。

    <xsl:variable name="matchesLeft" select="$questionObject/descendant::simpleMatchSet[position()=1]/simpleAssociableChoice"/>
    <xsl:variable name="matchesRight" select="$questionObject/descendant::simpleMatchSet[position()=2]/simpleAssociableChoice"/>

次のコードで変数を反転させたいと思います。

    <xsl:variable name="matchesRight">
        <xsl:choose>
            <xsl:when test="$flippedQuestions='true'">
                <xsl:value-of select="$questionObject/descendant::simpleMatchSet[position()=2]/simpleAssociableChoice"/>
            </xsl:when>
            <xsl:otherwise>
                <xsl:value-of select="$questionObject/descendant::simpleMatchSet[position()=1]/simpleAssociableChoice"/>
            </xsl:otherwise>
        </xsl:choose>
    </xsl:variable>

ただし、ノード内のすべての要素ではなく、最初の要素からのみ値を取得します。どうすればこれを達成できますか?

4

2 に答える 2

2

問題は、xsl:variable / @ selectがノードセットを提供することですが、xsl:value-ofはノードセットをその文字列値に変換します。ノードセットが必要です。XSLT 1.0では、コンテンツを含むxsl:variableは、常にresult-tree-fragmentを提供します。ただし、select属性では、条件式のないXPath1.0の使用に制限されています。

もちろん、最善の解決策は、これらすべての問題を解決するXSLT2.0に移行することです。1.0を維持する正当な理由の数は、常に減少しています。1.0を維持する必要がある場合、Dimitreで示されているような条件式がないため、XPath1.0には複雑な回避策があります。

于 2012-09-12T14:39:59.910 に答える
0

使用

<xsl:variable name="matchesRight" select=
 "$questionObject/descendant::simpleMatchSet
                                  [1+($flippedQuestions='true')]
                                          /simpleAssociableChoice"/>

説明

$someBValXPathでは、ブール値がなどの数値演算子に渡されるたびに、ブール値は。を+使用して数値(0または1)に変換されnumber($someBVal)ます。

定義により:

number(false()) = 0

number(true()) = 1

したがって

1+($flippedQuestions='true')

flippedQuestionsの文字列値が文字列でない場合は1と評価され、の文字列値が文字列である場合"true"は同じ式が2と評価されflippedQuestionsます"true"

于 2012-09-12T12:04:42.920 に答える