XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition (262 page)

BOOK: XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition
4.76Mb size Format: txt, pdf, ePub

Note that if an expression is used to assign a literal string value to a variable, the string literal must be enclosed in quotes, and these quotes are additional to the quotes used around the XML attribute. So to assign the string value
London
to the variable named
city
, you can write either of the following:



You can also write:

London

A common mistake is to write:

 

This sets the value of
$city
to a sequence containing all the element children of the context node that have element name

. This will probably be an empty sequence. If you get into the habit of using the
as
attribute, (in this case
as=“xs:string”
), the compiler will tell you about this mistake. A schema-aware processor might also warn you that there is no

element defined in the schema. But in other cases, you won't get any error message, because it's a perfectly valid thing to write; it will just cause your stylesheet to produce the wrong output.

You won't be alone if you make this mistake; there's an example of it in the original XSLT 1.0 specification, which had to be fixed in a subsequent erratum.

You don't need the extra quotes if you want the value to be a number:


Standard Conversion Rules

This section provides the detailed conversion rules used when converting the supplied value of a variable (the result of evaluating its
select
attribute or its sequence constructor) to the type required by the
as
attribute. The same conversion rules are used under a number of other circumstances, such as when values are supplied for stylesheet parameters. They are the same as the
function conversion rules
defined in XPath 2.0, which define what happens when the supplied arguments in a function call differ from the declared types of the parameters, with the exception that the rules described here never use XPath 1.0 backward-compatibility mode. The reason for this is that they are always invoked in contexts where backward compatibility issues do not arise.

Other books

Ghosts of Chinatown by Wesley Robert Lowe
Dragon Spear by Jessica Day George
Recoil by Andy McNab
Armand el vampiro by Anne Rice
And Darkness Fell by David Berardelli
Thrive by Rebecca Sherwin
Kicking the Can by Scott C. Glennie