XML technologies / XSLT / Introduction to XSLT 2.0 / XSLT and XPath – round two / Creating nodes

(Excerpt from “XSLT 2.0 & XPath 2.0” by Frank Bongers, chapter 2.)

Attribute value templates provide flexibility for Literal Result Element attributes, but not for the elements themselves, which are still copied unchanged. The method is simple, but ideal only when runtime requirements are known in advance.

When Literal Result Elements are insufficient

Literal Result Elements cannot cope when their names are unknown beforehand. Dynamically generating element or attribute names, or both, like attribute value templates fails because of syntax restrictions.

This approach is interesting but prohibited:

<{elementname} {attributname}="{attributwert}"/>

XPath expressions can be evaluated only inside attribute values, not at tag level. Since the capability is useful, special XSLT instructions construct nodes explicitly.

XSLT instructions for creating nodes

XSLT 2.0 has a creation instruction for each of the seven node kinds.

The instructions create:

  • attributes: xsl:attribute

  • elements: xsl:element

  • document nodes: xsl:document

  • text nodes: xsl:value-of

  • comments: xsl:comment

  • processing instructions: xsl:processing-instruction

  • namespace nodes: xsl:namespace.

Element and attribute creation is especially interesting here, particularly dynamically naming the constructed node.

Explicit document node creation with xsl:document relates to variables and validation of their values when created, and is not covered here.

Namespace node creation is similarly specialized, seemingly provided for completeness rather than necessity. Normally, namespace nodes are generated during serialization by Namespace FixUp without intervention.

Processing instructions and comments, however, need construction. They cannot be copied into results as Literal Result Elements: literal comments would be ignored and processing instructions executed prematurely. These instructions are nevertheless rarely needed.

Text nodes are officially created by xsl:value-of. This matters because instruction xsl:text does not do the same in this scope: it cannot evaluate XPath expressions because it lacks attribute select. It can only protect existing literal text nodes.

Since attributes often form coordinated collections, XSLT has a declaration for such sets: xsl:attribute-set. It does not itself create attribute nodes, merely wrapping their definitions.

Before examining that, we first outline element and attribute creation.

   

   

back to top