Reputation: 27
I am fairly new to XSLT and I am having trouble assigning a path to a variable.
Say i have the following example xml......
<CINEMA>
<FILMS>
<FILM_NAME>SomeFilmName</FILM_NAME>
<FILM_NAME>SomeOtherFilmName</FILM_NAME>
</FILMS>
</CINEMA>
And i declare the following variable....
<xsl:variable name="POS1" select="child::FILMS/descendant::FILM_NAME[1][. = 'SomeFilmName']"/>
If i call the variable using the following test I recieve no result as it appears it in not evaluating correctly
<xsl:template match="CINEMA">
<xsl:if test="$POS1">
.....Do some processing here if the above test evaluates to true........
</xsl:if>
</xsl:template>
However if i specify the actual path in the test without the call to a variable it seems to evaluate correctly.
Can anyone explain if what i want is possible please? If so, can anyone identify what is wrong when using the variable.
Thanks in advance, any help would be most appreciated.
Upvotes: 0
Views: 1053
Reputation: 167581
If that variable is a global one then you need to use an absolute path <xsl:variable name="POS1" select="/CINEMA/FILMS/descendant::FILM_NAME[1][. = 'SomeFilmName']"/>
or one relative to the root node <xsl:variable name="POS1" select="CINEMA/FILMS/descendant::FILM_NAME[1][. = 'SomeFilmName']"/>
.
Your current attempt only makes sense if the context node is the CINEMA
element, as in
<xsl:template match="CINEMA">
<xsl:variable name="POS1" select="child::FILMS/descendant::FILM_NAME[1][. = 'SomeFilmName']"/>
<xsl:if test="$POS1">
.....Do some processing here if the above test evaluates to true........
</xsl:if>
</xsl:template>
Of course in that case it might be easier to simply put a predicate on the match pattern
<xsl:template match="CINEMA[FILMS/descendant::FILM_NAME[1][. = 'SomeFilmName']]">
.....Do some processing here if the above test evaluates to true........
</xsl:template>
Upvotes: 1