0

ソース:

<Data>
    <AB>
        <choice>Disclose</choice>
        <image>
            <img alt="No Image" xlink:href="abcd:202-11587" xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Image" />
        </image>
        <link>abcd</link>
    </AB>
    <AB>
        <choice>All</choice>
        <image>
            <img alt="No Image" xlink:href="abcd:202-2202" xmlns="http://www.w3.org/1999/xhtml" xmlns:xlink="http://www.w3.org/1999/xlink" xlink:title="Image" />
        </image>
        <link>all</link>
    </AB>       
</Data>

XSLT

    <xsl:template match="Data">
         <xsl:for-each select="AB">
         <xsl:variable name="temp" select="choice"/>
            <xsl:choose>
                <xsl:when test="$temp='Disclose'">
                <xsl:apply-templates select="image/node()"/>                  
                </xsl:when>
            </xsl:choose>
         </xsl:for-each>

    </xsl:template>

    <xsl:template match="simple:image/xhtml:img">
    <!-- I want to get the the name of the "choice" here-->

    <!-- some other process-->
    <!-- how to access the value of the <choice> element of that section-->
    <!-- how to access <link> element of that section-->
  </xsl:template>

誰でもそれを行う方法を助けることができますか。

4

1 に答える 1

2

まず、これはコード サンプルの単なる見落としである可能性があるため、一致するテンプレートで名前空間を指定しました。

<xsl:template match="simple:image/xhtml:img">

ただし、サンプル XML には「単純な」名前空間への参照がないため、この場合は次のようにする必要があります。

<xsl:template match="image/xhtml:img">

しかし、あなたの質問に答えて、選択要素を取得するには、現在img要素に位置付けられているため、次のように階層を検索できます

<xsl:value-of select="../../choice" />

「..」は親要素を表します。つまり、 AB要素に戻り、その子の Choice要素を取得します。

同様にlink要素についても

<xsl:value-of select="../../link" />

ここで xsl:value-of である必要はありません。複数のリンク要素がある場合は、xsl:apply-templatesを使用できます。

<xsl:apply-templates select="../../link" />

そして、親の画像要素の後に発生するリンク要素のみが必要な場合は、次のようにすることができます

<xsl:apply-templates select="../following-sibling::link" />
于 2012-06-22T08:02:53.853 に答える