[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

RE: [xsl] Type error with variable as=xs:string using xsl:choose


Subject: RE: [xsl] Type error with variable as=xs:string using xsl:choose
From: "Michael Kay" <mike@xxxxxxxxxxxx>
Date: Thu, 7 Sep 2006 11:06:40 +0100

One of the more intricate parts of the XSLT 2.0 spec is the distinction
between strings and text nodes, and the question of when text nodes are
concatenated implicitly and when they aren't.

A single text node is created whenever you use xsl:text or xsl:value-of (or
xsl:number).

The basic rules are: 

(a) you can coerce ("atomize") a single text node to a string, but not a
sequence of text nodes

(b) text nodes are concatenated only if they are used within a node
constructor instruction, for example

<elem><xsl:text>123</xsl:text><xsl:value-of select="456"/></elem>

or

<xsl:value-of><xsl:text>123</xsl:text><xsl:value-of
select="456"/></xsl:value-of>

Personally if I'm creating a string then I try to avoid doing it by creating
text nodes and then atomizing them. I prefer to create the string directly,
in the select expression of xsl:sequence, using concat() if appropriate.

Michael Kay
http://www.saxonica.com/



> -----Original Message-----
> From: Trevor Nicholls [mailto:trevor@xxxxxxxxxxxxxxxxxx] 
> Sent: 07 September 2006 10:29
> To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx
> Subject: [xsl] Type error with variable as=xs:string using xsl:choose
> 
> The following variable definition is giving me the error
> "XTTE0570: A sequence of more than one item is not allowed as 
> the value of variable normsrc"
> 
> I'm using Saxon 8.6.1 on Windows XP.
> 
> ====
> <xsl:variable name="normsrc" as="xs:string"> <xsl:choose> 
> <xsl:when test="contains($xsrc,'/images/images/')">
> <xsl:text>images/</xsl:text>
> <xsl:value-of 
> select="substring-after($xsrc,'/images/images/')" /> 
> </xsl:when> <xsl:when test="contains($xsrc,'/images/')">
> <xsl:text>images/</xsl:text>
> <xsl:value-of select="substring-after($xsrc,'/images/')" /> 
> </xsl:when> <xsl:otherwise> <xsl:value-of select="$xsrc" /> 
> </xsl:otherwise> </xsl:choose> </xsl:variable> ====
> 
> If I drop the as="xs:string" attribute it works perfectly, 
> but I'm sure I've got exactly this construction elsewhere 
> without complaint. One difference with this logic is that the 
> 'when' tests overlap: any $xsrc that matches the first 'when' 
> will also match the second, but I understood that 'choose' 
> will only make one choice. Am I confused?
> 
> Cheers
> Trevor


Current Thread
Keywords