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元素,并获取其子选择元素。

同样对于链接元素

<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 回答