Functions on nodes
The name of a node can be accessed with the XQuery functions fn:node-name() and fn:name(). If the node has no name (document nodes, comment nodes or text nodes), an empty character string is outputted, otherwise the name. The name in processing instructions consists of the target of the processing instruction. For attributes and elements, the name may originate from a namespace.
fn:node-name() outputs the name as expanded QName, so that a namespace prefix is not required. Whereas fn:name() outputs a string in the syntactical form of a QName, so that a namespace prefix must be used. If such a prefix is defined in the context of the element node or attribute node, it is used, otherwise the XQuery implementation generates an unique prefix.
However, it is often useful to access separately the two components of a name (the namespace in the form of an URI and the "local" name). For this purpose, the functions fn:local-name() and fn:namespace-uri() are used. The following table shows some examples for the interaction of these functions:
$k | node-name($k) | name($k) | local-name($k) | namespace-uri($k) |
---|---|---|---|---|
<A/> | A | A | A | |
<y:A xmlns:y="http://x"/> | "http://x:A" | y:A | A | "http://x" |
<A xmlns="http://x"/> | "http://x:A" | A | A | "http://x" |
attribute a {1} | a | a | a | |
<!-- comment --> | ||||
<?do nix?> | do | do | do |
Table: Examples for the results of the name functions
The root of the node tree assigned to a node can be addressed with the fn:root() function. If the node which is passed on as argument comes from a XML document, the function call outputs a document node. However, in the following example the function outputs the node itself because it represents the root of the node tree:
fn:root(<Doctor/>)
The textual value of a node can be accessed with the fn:string() function, the typed value by means of the fn:data() function. Depending on the node type, this function outputs also a sequence of more than one value:
fn:string(<Name>
<FirstName>Daniela</FirstName>
<LastName>Baumann</LastName>
</Name>)
results in DanielaBaumann as value of xs:string type.
fn:data(<DateOfBirth>1982-07-23</DateOfBirth>)
results in, provided that an appropriate Schema definition is given, a value of xs:date type.
A special function for the treatment of numerical data is fn:number(). This function generates from a node a value of xs:double type. It only differs from the constructor function xs:double in border cases. While xs:double() reports an error if the argument is an empty sequence or does not originate from the region of representation of xs:double, fn:number outputs in these cases the value NaN. Therefore, it applies:
fn:number(<A><B>1</B><C>2</C></A>) = 12.0
because the textual value of <A><B>1</B><C>2</C></A> is 12, whereas the following expression:
fn:number(<A><B>1.0</B><C>2.0</C></A>)
results in the value NaN.
Source: "XQuery – Grundlagen und fortgeschrittene Methoden", dpunkt-Verlag, Heidelberg (2004)
<< back | next >> |