Read XSLT 2.0 and XPath 2.0 Programmer's Reference, 4th Edition Online
Authors: Michael Kay
Changes in 2.0
Under XPath 2.0 it is an error to supply a sequence containing more than one node, except when running in backward-compatibility mode.
Signature
Argument | Type | Meaning |
node (optional) | node()? | Identifies the node whose local name is required. If the argument is an empty sequence, the function returns a zero-length string. If the argument is omitted, the target node is the context node. It is then an error if there is no context item, or if the context item is not a node |
Result | xs:string | A string value: the local part of the name of the target node . |
Effect
The local name of a node depends on the kind of node, as follows:
Node kind | Local name |
document | None, a zero-length string is returned |
element | The element name, after any colon |
attribute | The attribute name, after any colon |
text | None, a zero-length string is returned |
processing instruction | The target used in the processing instruction to identify the application for which it is intended |
comment | None, a zero-length string is returned |
namespace | The namespace prefix; or the zero-length string if this is the default namespace |
Examples
Consider the source document:
Expression | Result |
local-name(/) | “” |
local-name(/*) | “doc” |
local-name(/*/@*[.=‘high’]) | “security” |
local-name(/*/@*[.=‘A23’]) | “id” |
Usage
This function can be useful if you need to test the local name without also testing the namespace URI. For example, if you want to select both
*[local-name()=‘title’]
However, XPath 2.0 allows you to achieve this more directly by writing:
*:title
In some ways this can be seen as a misuse of the XML Namespaces facility. The names in one namespace are supposed to bear no relation to the names in another, so any similarity between the names
In practice, this isn't always true. What often happens is that one namespace is adapted from another. For example, the US Post Office might devise a schema (and associated namespace) for representing US names and addresses, and the Canadian Post Office might then create a variant of this, with a different namespace URI, for Canadian names and addresses. The two schemas will have many elements in common, and it's quite reasonable to try to write a stylesheet that can handle either. If you want to write template rules that match on both a
Either list both possibilities:
or match on the local name only:
or equivalently:
It's not a good idea to use this construct simply to avoid the hassle of declaring the namespace prefix. Your code will almost certainly be less efficient, and it runs the risk of producing incorrect results because it can match elements in namespaces you weren't expecting.
XSLT Example
The following stylesheet fragment outputs an HTML table listing the attributes of the current element, sorted first by namespace and then by local name:
See Also
name()
on page 835
namespace-uri()
on page 837
local-name-from-QName
The function
local-name-from-QName()
returns the local-name part of an
xs:QName
value.
Signature
Argument | Type | Meaning |
value | xs:QName? | The xs:QName value whose local-name part is required. If the supplied value is an empty sequence, an empty sequence is returned. |
Result | xs:NCName? | The local-name part of the xs:QName . |
Effect
Given an expanded QName (that is, an instance of type
xs:QName
), this function returns the local-name part of the value.
The result is an
xs:NCName
. This is a type derived by restriction from
xs:string
, so the return value can be used anywhere that a string can be used. In a basic XSLT processor, which does not recognize the
xs:NCName
type directly, the value can still be used as a string.
Examples
Expression | Result |
local-name-from-QName(QName(‘http://mhk.me.uk/some.uri’, ‘my:invoice’)) | “invoice” |
local-name-from-QName(node-name(@xml:space)) | “space” |
The second example assumes that the context node has an attribute called
xml:space
.
See Also
QName()
on page 858
namespace-uri-from-QName()
on page 841
lower-case
The
lower-case()
function converts upper-case characters in a string to lower case.
For example,
lower-case(“McAndrew”)
returns
“mcandrew”
.
Signature
Argument | Type | Meaning |
value | xs:string? | The string to be converted |
Result | xs:string | The string with upper-case letters converted to lower case . |
Effect
For those whose only language is English, the matter of converting characters between upper case and lower case is straightforward: there is a direct one-to-one mapping between the 26 upper-case letters A–Z and the 26 lower-case letters a–z. In other languages, the relationship is not always so simple. In many Oriental scripts there is no concept of case at all. Even with Western languages there are many complications. To take a few examples: