Patan
Patan

Reputation: 17893

getting the value of the previous elements in other template in xslt

Source:

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

Can any one help how to do it.

Upvotes: 0

Views: 464

Answers (1)

Tim C
Tim C

Reputation: 70648

Firstly, as this may just be an oversight with your code sample, you have specified namespaces in your matching template

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

However, there are no references to the "simple" namespace in your sample XML, so in this case it should just be the following

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

But in answer to you question, to get the choice element, because you currently posisioned on the img element, you can search back up the hierarchy, like so

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

The '..' represents the parent element. So, you are going back up to the AB element, and getting its child choice element.

And similarly for the link element

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

Note, it doesn't have to be xsl:value-of here, if there were multiple link elements, you could use xsl:apply-templates

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

And, if you required only link elements that occurred after the parent image element, you could do something like this

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

Upvotes: 2

Related Questions