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 ...

Get XQuery from the Experts: A Guide to the W3C XML Query Language now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.