Creating Nodes: Element, Attribute, and Document Constructors

In the last section, we learned how to locate nodes in XML documents. Now we will learn how to create nodes. Elements, attributes, text nodes, processing instructions, and comments can all be created using the same syntax as XML. For instance, here is an element constructor that creates a book:

 <book year="1977">     <title>Harold and the Purple Crayon</title>     <author><last>Johnson</last><first>Crockett</first></author>     <publisher>HarperCollins Juvenile Books</publisher>     <price>14.95</price> </book> 

As we have mentioned previously, the document node does not have explicit syntax in XML, but XQuery provides an explicit document node constructor . The query document { } creates an empty document node. Let's use a document node constructor together with other constructors to create an entire document, including the document node, a processing instruction for stylesheet linking, and an XML comment:

 document {   <?xml-stylesheet type="text/xsl"                    href="c:\temp\double-slash.xslt"?>,   <!I love this book! >,   <book year="1977">     <title>Harold and the Purple Crayon</title>     <author><last>Johnson</last><first>Crockett</first></author>     <publisher>HarperCollins Juvenile Books</publisher>     <price>14.95</price>   </book> } 

Constructors can be combined with other XQuery expressions to generate content dynamically. In an element constructor, curly braces, { }, delimit enclosed expressions, which are evaluated to create open content. Enclosed expressions may occur in the content of an element or the value of an attribute. For instance, the following query might be used in an interactive XQuery tutorial to teach how element constructors work:

 <example>    <p> Here is a query. </p>    <eg> doc("books.xml")//book[1]/title </eg>    <p> Here is the result of the above query.</p>    <eg>{ doc("books.xml")//book[1]/title }</eg> </example> 

Here is the result of executing the above query for our sample data:

 <example>    <p> Here is a query. </p>    <eg> doc("books.xml")//book[1]/title </eg>    <p> Here is the result of the above query.</p>    <eg><title>TCP/IP Illustrated</title></eg> </example> 

Enclosed expressions in element constructors permit new XML values to be created by restructuring existing XML values. Here is a query that creates a list of book titles from the bibliography:

 <titles count="{ count(doc('books.xml')//title) }">  {   doc("books.xml")//title  } </titles> 

The output of this query follows :

 <titles count = "4">    <title>TCP/IP Illustrated</title>    <title>Advanced Programming in the Unix Environment</title>    <title>Data on the Web</title>    <title>The Economics of Technology and Content for    Digital TV</title> </titles> 

Namespace declaration attributes in element constructors have the same meaning they have in XML. We previously showed the following Dublin Core example as XML text ”but it is equally valid as an XQuery element constructor, and it treats the namespace declaration the same way:

 <book year="1994" xmlns:dcx="http://purl.org/dc/elements/1.1/">     <dcx:title>TCP/IP Illustrated</dcx:title>     <author><last>Stevens</last><first>W.</first></author> </book> 

Computed element and attribute constructors are an alternative syntax that can be used as the XML-style constructors are, but they offer additional functionality that is discussed in this section. Here is a computed element constructor that creates an element named title , with the content "Harold and the Purple Crayon" . Inside the curly braces, constants are represented using XQuery's native syntax, in which strings are delimited by double or single quotes.

 element title {    "Harold and the Purple Crayon" } 

Here is a slightly more complex constructor that creates nested elements and attributes using the computed constructor syntax:

 element book {     attribute year { 1977 },     element author     {         element first { "Crockett" },         element last { "Johnson" }     },         element publisher {"HarperCollins Juvenile Books"},         element price { 14.95 } } 

The preceding example uses literals for the names of elements. In a computed element or attribute constructor, the name can also be an enclosed expression that must have the type QName , which represents an element or attribute name. For instance, suppose the user has written a function that takes two parameters, an element name in English and a language, and returns a QName that has been translated to the desired language. This function could be used in a computed element constructor as follows:

 element { translate-element-name("publisher", "German") }         { "HarperCollins Juvenile Books" } 

The result of the above query is

 <Verlag>HarperCollins Juvenile Books</Verlag> 

In constructors, if sequences of whitespace characters occur in the boundaries between tags or enclosed expressions, with no intervening non-whitespace characters, then the whitespace is known as boundary whitespace. Implementations may discard boundary whitespace unless the query specifically declares that space must be preserved using the xmlspace declaration, a declaration that can occur in the prolog . The following query declares that all whitespace in element constructors must be preserved:

 declare xmlspace = preserve <author>    <last>Stevens</last>    <first>W.</first> </author> 

The output of the above query is

 <author>    <last>Stevens</last>    <first>W.</first> </author> 

If the xmlspace declaration is absent, or is set to strip , then boundary whitespace is stripped:

 <author><last>Stevens</last><first>W.</first></author> 


XQuery from the Experts(c) A Guide to the W3C XML Query Language
Beginning ASP.NET Databases Using VB.NET
ISBN: N/A
EAN: 2147483647
Year: 2005
Pages: 102

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net