2

Look at the code below:

<?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:output indent="yes"/>    
<xsl:template match="/">
    <root>
      <xsl:variable name="v1">
          <xsl:variable name="a1" select="137"/>
          <xsl:variable name="a2" select="(1, 3, 'abc')"/>              
          <xsl:variable name="a3" select="823"/>

          <xsl:sequence select="$a1"/>
          <xsl:sequence select="$a2"/>
          <xsl:sequence select="$a3"/>
      </xsl:variable>

        <xsl:variable name="v2" as="item()+">
            <xsl:variable name="b1" select="137"/>
            <xsl:variable name="b2" select="(1, 3)"/>
            <xsl:variable name="b3" select="823"/>
            <xsl:variable name="b4" select="'abc'"/>

            <xsl:sequence select="$b1"/>
            <xsl:sequence select="$b2"/>
            <xsl:sequence select="$b3"/>
            <xsl:sequence select="$b4"/>
        </xsl:variable>

      <count>
          <xsl:text>v1 count is: </xsl:text>
          <xsl:value-of select="count($v1)"/>              
      </count>
        <count>
            <xsl:text>v2 count is: </xsl:text>
            <xsl:value-of select="count($v2)"/>              
        </count>            
        <count>
            <xsl:text>a2 count is: </xsl:text>
            <xsl:value-of select="count((1, 3, 'abc'))"/>              
        </count>        
    </root>
</xsl:template>

</xsl:stylesheet>

The result ouput is:

<root>
    <count>v1 count is: 1</count>
    <count>v2 count is: 5</count>
    <count>a2 count is: 3</count>
</root>

Why v2 count is different from v1 count? They seems to have the same items. How the sequence splice? Why is v1 treated as the 'document-node' type?

Words "It looks like your post is mostly code; please add some more details." always prevent me to submit.

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65
cmf41013
  • 107
  • 1
  • 9

1 Answers1

3

Well, you have different variable declarations, as one uses the as attribute and the other not.

And you seem to have inferred that your first case without any as declaration results in a document node (containing content).

As for the gory details of the various options, the spec treats your first case in https://www.w3.org/TR/xslt-30/#temporary-trees and outlines the various options of how as, select and content constructors in xsl:variable interact in https://www.w3.org/TR/xslt-30/#variable-values.

Martin Honnen
  • 160,499
  • 6
  • 90
  • 110
  • Does a variable without any `as` declaration always be treated as an document-node type whatever its value is? – cmf41013 Dec 27 '19 at 08:06
  • Can you use the materials I linked to? It says in the first sentence "A document node is created implicitly when evaluating an xsl:variable, xsl:param, or xsl:with-param element that has non-empty content **and** that has no as attribute". So `` does not create a document node, it simply gives the variable the `xs:integer` value `1`, but `1` creates a document node containing a text node with the string value `1`. – Martin Honnen Dec 27 '19 at 08:17
  • Actually I haven't found the answer in the links, your repliy is precise. Thanks. – cmf41013 Dec 27 '19 at 08:33
  • @cmf41013, The lesson from this experience id to learn to use the `select` attribute of an `` as often as possible (and, of course, to specify the type of the variable!). Only when it is impossible to comply with this rule should one define the value inside the sequence constructor (body) of the variable. Typically, these are the cases when we want to capture in a variable the result of processing an XSLT instruction (such as `` – Dimitre Novatchev Dec 27 '19 at 22:52