cmf41013
cmf41013

Reputation: 107

Why variable goes wrong as "document-node()" in sub xsl:template?

Looking directly at the example:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="3.0">

<xsl:template match="/">
    <xsl:variable name="v1" select="." as="document-node()"/>
    <xsl:apply-templates select=".//name"/>
</xsl:template>

<xsl:template match="name">
    <xsl:variable name="v2" select="." as="document-node()"/>
    <xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet>

source xml:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item>
        <name>John</name>
        <age>21</age>
    </item>
</root>

The question is that: variable v1 can be declarated well, but v2 has an error "Required item type of value of variable $v2 is document-node(); supplied expression (.) has item type element(Q{}name)".

I looked up the definition of document-node() in w3c xpath document, it says "document-node() matches any document node." 2.5.5.2 Matching an ItemType and an Item. I still can't understand why v2 is wrong with type document-node().

I have make a online test demo

Upvotes: 0

Views: 425

Answers (1)

Max Toro
Max Toro

Reputation: 28618

Because it's not a document-node(), it's an element(). Use element() instead. <xsl:template match="/"> matches a document-node(), and <xsl:template match="name"> matches an element() node.

Upvotes: 0

Related Questions