SLIDE 60 60
119
Converting Attributes to Elements with XQuery
- An XQuery reformulation of a previous XSLT query – much more
straightforward (but ignores text nodes)
DECLARE FUNCTION convertAttributes($a AS attribute()) AS element() { RETURN element {name($a)} {data($a)}
}
DECLARE FUNCTION convertElement($e AS node()) AS element() { RETURN
element {name($e)} {
{ FOR $a IN $e/@* RETURN convertAttribute ($a) } , IF empty($e/*) THEN $e/text( ) ELSE { FOR $n IN $e/* RETURN convertElement($n) }
} }
RETURN convertElement(doc(“my-document” )/*)
The actual query: Just a RETURN statement!! Computed element
Concatenate Concatenate results results 120
Integration with XML Schema and Namespaces
- Let type sometype be defined in http://types.r.us/types.xsd:
IMPORT SCHEMA IMPORT SCHEMA namespace trs = “http://types.r.us” AT AT “http://types.r.us/types.xsd”; DECLARE DECLARE NAMESPACE NAMESPACE foo foo = “http://foo.org/foo”; DECLARE FUNCTION local local:doSomething($x AS trs trs:sometype) AS xs xs:string { FOR $i IN fn fn: :doc(… ); … … … RETURN <foo foo:something> … … … </foo foo:something> }
Namespace Location Another namespace Local functions have predefined namespace Built-in functions have predefined namespaces Predefined namespace