Why storing an attribute in xsl: variable results in error XTDE0420?

This XSLT creates an attribute and stores the result in a variable. Then the variable is copied as the only child of the element <test>

:

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema"
    exclude-result-prefixes="#all">    

  <xsl:template match="/">        
    <xsl:variable name="some-attribute">
      <xsl:attribute name="test">value</xsl:attribute>
    </xsl:variable>
    <test>
      <xsl:copy-of select="$some-attribute" />
    </test>
  </xsl:template>

</xsl:stylesheet>

      

Although it seems to be simply inserts the attribute as a child element, the result is an error: XTDE0420: Cannot create an attribute node whose parent is a document node

.

+3


source to share


1 answer


Basic information is explained in section 9.3 of the XSLT 2.0 specification, "Variable and parameter values" :

If the variable-bound element does not have a select attribute and has non-empty content (that is, the variable-bound element has one or more child nodes) and has no attribute, then the variable-bind element content indicates the value specified in the application. The content variable bind element is a sequence constructor; the new document is built with a node document having as its children a sequence of nodes resulting from evaluating the sequence constructor and then applying the rules given in 5.7.1. Building Complex Content. The value of the variable is the singleton sequence containing this document node. For more information see 9.4. Creation of implicit document nodes.

Essentially, the value of a variable with no attribute select

and no attribute as

is a node document.



There is no way to change the variable in the example to use select

, but it can be changed to use as

:

<xsl:variable name="some-attribute" as="item()*">
  <xsl:attribute name="test">value</xsl:attribute>
</xsl:variable>

      

+7


source







All Articles