Questions and answers Edit

In XSLT, why am I not getting the expected results from a select attribute? Edit

XSLT allows the select attribute on a number of instruction elements, such as xsl:apply-templates and xsl:value-of. This attribute works basically the same way on each of these elements: it locates a node-set using the XPath expression stored in the attribute, and the resulting node-set is provided to the instruction element for further processing. Your select attribute may not be selecting the node or nodes that you expect for one of several reasons. One common reason is that your XPath expression may refer to element names with no namespace prefix, but those elements are actually in a namespace. Please see the question on namespaces in XPath for more information.

Another reason for incorrect select results may be that your context node is not what you expect. All XPath expressions are evaluated with respect to a context node.[1] The context node starts off as the document root (which can be selected from any context using the XPath expression /), and two XSLT instructions change the context node. First, a template defined with an xsl:template element that has a match attribute will change the context node to the node currently being matched by the match attribute. Consider the following template:

<xsl:template match="ex:person[ex:height-in-cm &lt; 189]">
  <out:eligible><xsl:value-of select="ex:name"/></out:eligible>

In this example, the context node for the XPath expression ex:name will be the currently matched ex:person, so this expression will look for the child element of that ex:person element named ex:name. Note that calling a template by name with an instruction like <xsl:call-template name="foo"/> does not change the context node. The only other way to change the context node is with the xsl:for-each element, which will change the context node for the children of the xsl:for-each element to the node currently being processed in the loop.

In XSLT, why am I seeing output for an element with no matching template? Edit

XSLT provides a set of built-in template rules that can provide some useful default behavior.[2] The built-in rules will walk through the root node and any element nodes, and will output any text nodes that are found. This default behavior can be confusing if you don't expect it or don't know how to deal with it. (On the other hand, thinking about how this small set of rules works recursively over your input document can help you understand XSLT much better.) You can override any or all of these actions using explicit templates in your XSLT script.

How do I deal with namespaces in XPath? Edit

XPath 1 requires you to use a namespace prefix to indicate the namespace for an element or attribute; a name test with no prefix matches an element or attribute that is not in a namespace.[3]

For example, the XPath expression /invoice/@version would match the version attribute on the root element of this document:

<invoice version="1.0">
  ... additional content here

But not of this document:

<invoice version="1.0" xmlns="">
  ... additional content here

To match the version attribute in the second document above, you would have to declare a prefix for the namespace and use that prefix in the XPath expression. In an XML host document, such as XSLT, this would look like xmlns:inv="". (You can choose your own prefix instead of inv.) Then your XPath expression (with respect to this namespace declaration) would be /inv:invoice/@version.

What namespace applies to my unprefixed attributes? Edit

Unlike elements, attributes without a namespace prefix have no namespace.[4] For example, in this document:

<invoice version="1.0" xmlns="">
  ... additional content here

The invoice element is in the namespace, but the version attribute is not in a namespace. This can be a source of confusion for those trying to query XML documents with XPath, such as when using XSLT or XQuery.

References Edit

  1. "Introduction" section to the XPath 1 Recommendation
  2. "Built-in Template Rules" section of the XSLT 1 Recommendation
  3. "Node Tests" section of the XPath 1 Recommendation
  4. "Namespace Defaulting" section of the Namespaces in XML 1.0 Recommendation

Ad blocker interference detected!

Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.