Re: [xsl] differentiating between *the* document node and anonymous document nodes

Subject: Re: [xsl] differentiating between *the* document node and anonymous document nodes
From: "Chris Papademetrious christopher.papademetrious@xxxxxxxxxxxx" <xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 19 Mar 2023 14:46:59 -0000
Thank you Eliot! Yes, the term I was previously trying to remember was
"implicit" document node, not "anonymous" document node.

And once I added the <xsl:document> constructor within the variable
definition, my template works as desired.

It adds a bit of bulk to explicitly declare and construct document nodes where
they would be implicitly inferred, but I prefer to always explicitly declare
the @as type.


  *   Chris

From: Eliot Kimber eliot.kimber@xxxxxxxxxxxxxx
<xsl-list-service@xxxxxxxxxxxxxxxxxxxxxx>
Sent: Sunday, March 19, 2023 10:10 AM
To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
Subject: Re: [xsl] differentiating between *the* document node and anonymous
document nodes

The type document-node() is what you are looking for, but you may need to use
the <xsl:document> constructor:

<xsl:variable name="result" as="document-node()">
  <xsl:document>
    <xsl:copy select=".">
     <xsl:apply-templates select="@*, node()"/>
    </xsl:copy>
  </xsl:document>
</xsl:variable>

When no @as is specified the value of the variable will be a document node,
per 9.3 Values of Variables and Parameters:

If a variable-binding
element<https://urldefense.com/v3/__https:/www.w3.org/TR/xslt-30/*dt-variable
-binding-element__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzATha
A2AzLrVGEUW4bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDjicyM-o$>
has no select attribute and has non-empty content (that is, the
variable-binding element has one or more child nodes), and has no as
attribute, then the content of the variable-binding element specifies the
supplied
value<https://urldefense.com/v3/__https:/www.w3.org/TR/xslt-30/*dt-supplied-v
alue__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4
bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDmvC9_Z1$>. The content
of the variable-binding element is a sequence
constructor<https://urldefense.com/v3/__https:/www.w3.org/TR/xslt-30/*dt-sequ
ence-constructor__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzATha
A2AzLrVGEUW4bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDkLAvKys$>; a
new document is constructed with a document node having as its children the
sequence of nodes that results from evaluating the sequence constructor and
then applying the rules given in 5.7.1 Constructing Complex
Content<https://urldefense.com/v3/__https:/www.w3.org/TR/xslt-30/*constructin
g-complex-content__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzATh
aA2AzLrVGEUW4bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDocAaXUE$>.
The value of the variable is then a singleton sequence containing this
document node. For further information, see 9.4 Creating Implicit Document
Nodes<https://urldefense.com/v3/__https:/www.w3.org/TR/xslt-30/*temporary-tre
es__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bP
u-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDgF7Ybz5$>.

[https://www.w3.org/TR/xslt-30/#variable-values<https://urldefense.com/v3/__h
ttps:/www.w3.org/TR/xslt-30/*variable-values__;Iw!!A4F2R9G_pg!fCt2BdwT5Ikw_Qs
f2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4
RKtd68nLzq-mbK-MDuo-Vd0L$>]

So very important to specify @as when you *don't* want a document node (which
is usually the case) and useful to specify @as="document-node()" when you
really do want a document node (for example, because you will be applying
key() to the value of the variable, which requires a document and not just an
element).

This is a particularly subtle aspect of XSLT 2+ and can lead to some subtle
bugs, especially when migrating from XSLT 1 (no @as) to XSLT 2+.

Cheers,

E.
_____________________________________________
Eliot Kimber
Sr Staff Content Engineer
O: 512 554 9368
M: 512 554 9368
servicenow.com<https://urldefense.com/v3/__https:/www.servicenow.com__;!!A4F2
R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9pwyt3PrRRj
R0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDi0cKdc-$>
LinkedIn<https://urldefense.com/v3/__https:/www.linkedin.com/company/servicen
ow__;!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-
9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDoPzE-bV$> |
Twitter<https://urldefense.com/v3/__https:/twitter.com/servicenow__;!!A4F2R9G
_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9pwyt3PrRRjR0u
2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDkvQFiDU$> |
YouTube<https://urldefense.com/v3/__https:/www.youtube.com/user/servicenowinc
__;!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9p
wyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDic805zr$> |
Facebook<https://urldefense.com/v3/__https:/www.facebook.com/servicenow__;!!A
4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9pwyt3Pr
RRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDpGaLGgM$>
XSL-List info and
archive<https://urldefense.com/v3/__http:/www.mulberrytech.com/xsl/xsl-list__
;!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzAThaA2AzLrVGEUW4bPu-9pwy
t3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDtzQkBBm$>
EasyUnsubscribe<https://urldefense.com/v3/__http:/lists.mulberrytech.com/unsu
b/xsl-list/3380743__;!!A4F2R9G_pg!fCt2BdwT5Ikw_Qsf2XLfqCV46bdLpUy-6sOkrkzATha
A2AzLrVGEUW4bPu-9pwyt3PrRRjR0u2bZiX0x6a0YlOSaaTW4RKtd68nLzq-mbK-MDkkHDExr$>
(by email<>)

Current Thread