using '/..' notation in XSLT XPath
Asked Answered
N

3

5

Here is a beginner's question. What does this XPath expression /.. mean? Does it always select an empty node-set?

I've noticed that this pattern is used extensively in FXSL library, for example:

<xsl:template name="map">
  <xsl:param name="pFun" select="/.."/>
  <xsl:param name="pList1" select="/.."/>

  <xsl:for-each select="$pList1">
    <xsl:copy>
      <xsl:apply-templates select="$pFun">
        <xsl:with-param name="arg1" select="."/>
      </xsl:apply-templates>
    </xsl:copy>
  </xsl:for-each>
</xsl:template>

Is it necessary to specify /.. in parameters' values here?

Nonu answered 5/10, 2011 at 9:55 Comment(0)
L
6

The purpose of:

 <xsl:param name="pFun" select="/.."/> 

is to specify (at least in XPath 1.0 whose type system is very weak) that the type of the parameter $pFun is a node (or more generally a node-set). So, the reader knows that the value passed for this parameter shouldn't be a string or an integer.

It also specifies that the default value, if this parameter hasn't explicitly been specified on calling or applying the template, is the empty node-set.

This notation has a primarily documentation significance (but see the update at the end of this answer) and is useful only in XPath 1.0 where the type system is weak. In XPath 2.0 it is recommended to explicitly specify the type, so that the XPath processor can perform better type checking and the generated code can be more optimized and efficient.

So, in XPath 2.0 /XSLT 2.0 (in this particular case) it would be better to refactor the above to:

 <xsl:param name="pFun" as="element()"> 

There are other variations of such notation. In the early books of @Michael Kay one can also find expressions like:

@comment()

Update: the expression /.. has more practical uses than merely serving documentation purposes.

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:strip-space elements="*"/>

 <xsl:template match="/*">
  <xsl:param name="pNode"/>
     <xsl:copy-of select=".|$pNode"/>
 </xsl:template>
</xsl:stylesheet>

Try to apply this transformation to any XML document. In our case, the simplest possible:

<t/>

The result is the following Saxon error message ( a similar error with other XSLT 1.0 processors):

Error at xsl:copy-of on line 8 of file:/(Untitled):
  The value is not a node-set
Transformation failed: Run-time errors were reported

Now replace:

  <xsl:param name="pNode"/>

with:

  <xsl:param name="pNode" select="/.."/>

and run the transformation again.

This time the transformation is performed without a runtime error.

Conclusion: The ability to specify an empty node-set is important in practice and the expression /.. is one way of achieving this.

Lingwood answered 5/10, 2011 at 12:22 Comment(0)
F
2

The expression /.. selects the parent of the root node, which is guaranteed to select nothing.

/ refers to the root node of the XML document, which is really an abstract concept for the top level of the document and used in XPath as the starting point to reference top-level items. It's children will always include the document element, and may also include top-level comment() and processing-instruction() that are siblings of the document element.

Is it necessary to specify /.. in parameters' values here?

No, it is not necessary to provide a default select statement for the xsl:param. It is being used in this case to explicitly define the default for those parameters to be "nothing".

Fahy answered 5/10, 2011 at 10:58 Comment(0)
W
0

/ is the root node (when at the start of an XPath expression), so /.. would be the parent of the root node.

This doesn't make sense.

Unless it should be ./..: ie. the parent of the current (context) node.

Wilkins answered 5/10, 2011 at 10:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.