Giuliani Sanches
Giuliani Sanches

Reputation: 672

What the /.. xpath does in a xsl template

Look this xsl template:

<xsl:template match="root">
    <xsl:param name="bla" select="/.." />

    <ha>
        <xsl:value-of select="$bla" />
    </ha>
</xsl:template>

The part "select="/.." don't throw an exception (for me, the right xpath is ../), but does nothing.

Why define a parameter like that ?

If i execute the template without pass the "bla" parameter, "ha" will be empty, otherwise it will contain the value passed.

Thanks

Upvotes: 1

Views: 95

Answers (3)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243479

The part "select="/.." don't throw an exception (for me, the right xpath is ../), but does nothing.

Why define a parameter like that ?

This is useful in XSLT 1.0 to indicate that the type of an xsl:param or an xsl:variable is node-set.

Then the XSLT processor will not produce an error on expression like:

$bla | $myNodeSet

On the contrary, if you just define the parameter without giving it any default value, the expression above produces an error -- sometnhing like:

"Expression must evaluate to a node-set"

Easy verification:

Try this (works OK):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
 <xsl:param name="blah" select="/.."/>

 <xsl:copy-of select=". | $blah"/>
</xsl:template>
</xsl:stylesheet>

and this (results in error):

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

 <xsl:template match="/">
 <xsl:param name="blah"/>

 <xsl:copy-of select=". | $blah"/>
</xsl:template>
</xsl:stylesheet>

Upvotes: 4

Wayne
Wayne

Reputation: 60414

That is indeed a pretty useless thing to try. You're selecting the parent of the root node, which has no parent node by definition. Use the following to show that nothing is selected:

<xsl:value-of select="count($bla)"/>

Output:

<ha>0</ha>

Upvotes: 1

Chriseyre2000
Chriseyre2000

Reputation: 2053

It may be a mistake in the query and xslt ingnores invalid parts of queries.

Upvotes: 0

Related Questions