<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE spec SYSTEM "../../xquery-semantics/xquery-semantics.dtd">
<spec xmlns:e="http://www.w3.org/1999/XSL/Spec/ElementSyntax" id="spec-top" w3c-doctype="wd">
<header>
<title>XQuery Update Facility 1.0</title>
<w3c-designation>WD-xquery-update-10</w3c-designation>
<w3c-doctype>W3C Working Draft</w3c-doctype>
<pubdate>
 <day>28</day>
 <month>August</month>
 <year>2007</year>
</pubdate>

<publoc>
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2007/WD-xquery-update-10-20070828/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/2007/WD-xquery-update-10-20070828/</loc>
</publoc>

<altlocs>
   <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2007/WD-xquery-update-10-20070828/xquery-update-10.xml" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML</loc>
</altlocs>

<latestloc>
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery-update-10/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/TR/xquery-update-10/</loc>
</latestloc>

<prevlocs>
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xqupdate-20060711/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xqupdate-20060508/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/2006/WD-xqupdate-20060127/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest"/>
<!--
<loc href="http://www.w3.org/TR/2006/WD-xqupdate-20060127/"/>
<loc href="http://www.w3.org/TR/2005/WD-xqupdate-20050404/"/>
<loc href="http://www.w3.org/TR/2004/WD-xqupdate-20040709/"/>
-->
</prevlocs>

<authlist>
  <author>
    <name>Don Chamberlin</name>
    <affiliation>IBM Almaden Research Center</affiliation>
    <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.almaden.ibm.com/cs/people/chamberlin/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.almaden.ibm.com/cs/people/chamberlin/</loc>
  </author>
  <author>
    <name>Daniela Florescu</name>
    <affiliation>Oracle</affiliation>
    <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:dana.florescu@oracle.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">dana.florescu@oracle.com</email>
  </author>
  <author>
    <name>Jim Melton</name>
    <affiliation>Oracle</affiliation>
    <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:jim.melton@oracle.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">jim.melton@oracle.com</email>
  </author>
  <author>
    <name>Jonathan Robie</name>
    <affiliation>Red Hat</affiliation>
    <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:jonathan.robie@redhat.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">jonathan.robie@redhat.com</email>
  </author>
  <author>
    <name>Jérôme Siméon</name>
    <affiliation>IBM T.J. Watson Research Center</affiliation>
    <email xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:simeon@us.ibm.com" xlink:type="simple" xlink:show="new" xlink:actuate="onRequest">simeon@us.ibm.com</email>
  </author>
</authlist>

<abstract>
<p>This document defines an update facility that extends the XML Query language, XQuery. 
The XQuery Update Facility provides expressions that can be used to make persistent changes
to instances of the XQuery 1.0 and XPath 2.0 Data Model.</p>
</abstract>

<status>
<p>
<emph>This section describes the status of this document at the time of its publication. 
Other documents may supersede this document. 
A list of current W3C publications and the latest revision of this technical report can be 
found in the <loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">W3C technical reports index</loc> at 
http://www.w3.org/TR/.</emph>
</p>

<p>
This is a
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2005/10/Process-20051014/tr.html#last-call" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Last Call Working Draft</loc>
for review by W3C Members and other interested parties.
This document was produced following the procedures set out for the W3C Process and
was defined by the 
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Query/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Query Working Group</loc>
(part of the 
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/Activity.html" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XML Activity</loc>).
It is designed to be read in conjunction with the following documents:
<bibref ref="xquery"/>, <bibref ref="update-req"/>, and <bibref ref="update-use-cases"/>. 
</p>

<p>
Publication as a Working Draft does not imply endorsement by the W3C Membership. 
This is a draft document and may be updated, replaced or obsoleted by other documents at 
any time. 
It is inappropriate to cite this document as other than work in progress.
</p>

<p>
This document defines a facility for updating instances of the XQuery 1.0 and XPath 2.0 
Data Model; the  language is specified in the form of extensions to
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/TR/xquery/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">XQuery 1.0</loc>. 
Organizations and individuals should review  this document to determine the degree
to which the facility fulfills requirements for updating XDM instances. 
The Working Group believes that this work is essentially complete 
and intends to advance it as soon as possible. 
</p>

<p>
Since the last version of this document, several significant changes have been made. 
Several new update primitives have been added, removing the need for the
compatibility table by introducing new mappings from updating expressions onto
update primitives. 
Considerable clarification of the handling of namespace bindings have been made. 
The semantics of node deletion and of parentless node value replacement have
been clarified. 
Finally, the XQueryX specification for the XQuery Update Facility has been added
in an appendix. 
Additional information about changes made since the last version of this document
can be found at <specref ref="id-log-28Aug2007"/>.</p>


<p>Of the XQuery Update Facility documents, only this document,
XQuery Update Facility 1.0, is a Last Call document. 
The XQuery Update Facility Requirements <bibref ref="update-req"/>,
although not on the Recommendation track, is being republished concurrently
with this document in order to demonstrate the degree to which this document
satisfies those Requirements. 
The XQuery Update Facility Use Cases <bibref ref="update-use-cases"/>,
although not on the Recommendation track, is being republished concurrently
with this document in order to illustrate various use cases that guided the design of the 
XQuery Update Facility specification. 
</p>

<p>
Public Last Call comments on this document and its open issues are invited.
Comments on this document are due by 31 October 2007.
Comments on this document should be made in W3C's
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Bugs/Public/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public Bugzilla system</loc>
for this specification (instructions can be found at
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/XML/2005/04/qt-bugzilla" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://www.w3.org/XML/2005/04/qt-bugzilla</loc>). 
When entering comments, select the Product named "XPath / XQuery / XSLT", the Component 
named "Update Facility", and the Version named "Last Call drafts". 
This repository includes open issues recorded by the Query Working Group as well as by 
members of the public. 
If access to the Bugzilla system is not feasible, you may send your comments to the W3C 
XSLT/XPath/XQuery mailing list,
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="mailto:public-qt-comments@w3.org" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public-qt-comments@w3.org</loc>.
It will be very helpful if you include the string [UPD] in the subject line of your 
comment, whether made in Bugzilla or in email. 
Each Bugzilla entry and email message should contain only one comment. 
Archives of the comments and responses are available at
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://lists.w3.org/Archives/Public/public-qt-comments/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">http://lists.w3.org/Archives/Public/public-qt-comments/</loc>.
</p>

<p>
This document was produced by a group operating under the
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">5 February 2004
W3C Patent Policy</loc>.
W3C maintains a 
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/2004/01/pp-impl/18797/status#disclosures" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">public list of any 
patent disclosures</loc>
made in connection with the deliverables of the group;
that page also includes instructions for disclosing a patent.
An individual who has actual knowledge of a patent which the individual believes contains
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#def-essential" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">Essential 
Claim(s)</loc>
must disclose the information in accordance with
<loc xmlns:xlink="http://www.w3.org/1999/xlink" href="http://www.w3.org/Consortium/Patent-Policy-20040205/#sec-Disclosure" xlink:type="simple" xlink:show="replace" xlink:actuate="onRequest">section 6 of the W3C 
Patent Policy</loc>. 
</p>

</status>

<langusage>
  <language id="EN">English</language>
  <language id="ebnf">EBNF</language>
</langusage>

<revisiondesc>
  <p>September 2005: First internal draft</p>
  <p>November 2005: Second internal draft</p>
  <p>27 January 2006: First public working draft</p>
  <p>8 May 2006: Second public working draft</p>
  <p>11 July 2006: Third public working draft</p>
  <p>28 August 2007: Last Call working draft</p>
</revisiondesc>
</header>
<body>

<div1 id="introduction">
  <head>Introduction</head>

<p>
This  document defines the syntax and semantics of an extension 
to <bibref ref="xquery"/> called the XQuery Update Facility 1.0.
This language extension is designed to meet the
requirements for updating instances of the  <bibref ref="datamodel"/>, as defined in <bibref ref="update-req"/>.</p>

<p>
The XQuery Update Facility 1.0 provides facilities to perform any or all
of the following operations on an
<termref def="dt-xdm-instance">XDM instance</termref>:
</p>
<ulist>
<item><p>Insertion of a node.</p></item>
<item><p>Deletion of a node.</p></item>
<item><p>Modification of a node by changing
some of its properties while preserving its <termref def="dt-node-identity">node identity</termref>.</p></item>
<item><p>Creation of a modified copy of a node with a new <termref def="dt-node-identity">node identity</termref>.</p></item>
</ulist>





  
<div2 id="id-terminology"><head>Terminology</head><p><termdef term="XQuery" id="dt-xquery">Within this document, the term <term>XQuery</term> refers to the language specified by <bibref ref="xquery"/>.</termdef>  <termdef term="data model" id="dt-data-model">The term <term>data model</term> refers to the data model specified by <bibref ref="datamodel"/>.</termdef> <termdef term="XDM instance" id="dt-xdm-instance">The term <term>XDM instance</term> denotes an unconstrained sequence of zero or more nodes and/or atomic values as defined by the <termref def="dt-data-model">data model</termref>.</termdef> <termdef term="node identity" id="dt-node-identity">The term <term>node identity</term> denotes the unique identity that is a property of every node in an <termref def="dt-xdm-instance">XDM instance</termref> (see <xspecref spec="DM" ref="node-identity"/>.)</termdef></p><p>As described in <xspecref spec="DM" ref="qnames-and-notations"/>, names in XQuery are qualified names (QNames) that consist of an optional namespace prefix, a local name, and an optional namespace URI. <termdef term="implied namespace binding" id="dt-implied-namespace-binding">The <term>implied namespace binding</term> of a QName is the association of its namespace prefix (or absence thereof) with its namespace URI (or absence thereof).</termdef> <termdef term="conflict" id="dt-conflict">Two namespace bindings are said to <term>conflict</term> if their namespace prefixes (or absence thereof) are the same but their namespace URI's (or absence thereof) are different.</termdef> Update operations that result in conflicting namespace bindings generally raise errors, as described in this document.</p></div2></div1>



<div1 id="id-extensions">
<head>Extensions to XQuery 1.0</head>

<p>The basic  building block of XQuery is the expression. XQuery 1.0 provides 
several kinds of expressions that can be composed with each other in 
arbitrary ways. An XQuery 1.0 expression takes one or more 
<termref def="dt-xdm-instance">XDM instances</termref> as input and returns an <termref def="dt-xdm-instance">XDM instance</termref> as 
a result. In XQuery 1.0, an expression never modifies the state of 
an existing node; however, constructor expressions create new nodes 
with new <termref def="dt-node-identity">node identities</termref>.</p>

<p>XQuery Update Facility 1.0 introduces a new category of expression called an <termref def="dt-updating-expression">updating expression</termref>, which can potentially modify the state of an existing node.</p>

<p>The extensions to XQuery 1.0 provided by XQuery Update Facility 1.0 may be characterized 
as follows:</p>

<olist>
<item><p>XQuery Update Facility 1.0 introduces five new kinds of expressions, called insert, 
delete, replace, rename, and transform expressions, and specifies the syntax and semantics of each new kind of expression.</p></item><item><p>XQuery Update Facility 1.0 classifies XQuery expressions into the following categories:<olist><item><p><termdef term="basic updating expression" id="dt-basic-updating-expression">A <term>basic updating expression</term> is an insert, delete, replace, or rename expression, or a call to an <termref def="dt-updating-function">updating function</termref>.</termdef></p></item><item><p><termdef term="updating expression" id="dt-updating-expression">An <term>updating expression</term> is a <termref def="dt-basic-updating-expression">basic updating expression</termref> or any
expression (other than a transform expression) that directly
contains an <termref def="dt-updating-expression">updating expression</termref>.</termdef></p><note><p>The definition of an <termref def="dt-updating-expression">updating expression</termref> is recursive.</p></note></item><item><p><termdef term="non-updating expression" id="dt-non-updating-expression">A <term>non-updating expression</term> is any XQuery expression that is not an <termref def="dt-updating-expression">updating expression</termref>.</termdef></p></item></olist></p> </item>

<item><p>XQuery Update Facility 1.0 defines  the places 
where updating and non-updating expressions can be used. In so doing, it makes small 
extensions to the syntax and semantics of certain existing expressions.</p></item>



</olist>

<div2 id="id-processing-model">
<head>Extensions to the Processing Model</head><p>XQuery Update Facility 1.0 defines the following extensions to the XQuery processing model:</p><olist><item><p>In XQuery 1.0, the result of each expression is an <termref def="dt-xdm-instance">XDM instance</termref>. XQuery Update Facility 1.0 extends the XQuery processing model so that the
result of an expression consists of both an <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> (either or both of which may be empty). <termdef id="dt-pending-update-list" term="pending update list">A <term>pending update list</term> is an unordered collection of <termref def="dt-update-primitive">update primitives</termref>, which represent node state changes that have not yet been applied.</termdef></p><note><p>In the current specification, no expression returns both a non-empty <termref def="dt-xdm-instance">XDM instance</termref> and a non-empty <termref def="dt-pending-update-list">pending update list</termref>.</p></note></item><item><p>XQuery Update Facility 1.0 also defines a set of <termref def="dt-update-operation">update operations</termref>. <termdef term="update operation" id="dt-update-operation"><term>Update operations</term> are used in defining the semantics of XQuery updates, but are not directly available to users. Update operations are defined in <specref ref="id-update-operations"/>.</termdef> Update operations fall into the following categories:</p><olist><item><p> <termdef term="update primitive" id="dt-update-primitive"><term>Update primitives</term> are the components of <termref def="dt-pending-update-list">pending update lists</termref>. Each <term>update primitive</term> represents a node state change that has not yet been applied.</termdef> <termdef term="target node" id="dt-target-node">The first argument of an update primitive, called its <term>target node</term>, is the principal node to be affected by the update primitive.</termdef> Update primitives are held on <termref def="dt-pending-update-list">pending update lists</termref> until they are made effective by a <code>upd:applyUpdates</code> operation.</p></item><item><p><termdef term="update routine" id="dt-update-routine"><term>Update routines</term> are sequences of actions that are used in the definition of XQuery semantics but do not appear on <termref def="dt-pending-update-list">pending update lists</termref>.</termdef> <code>upd:applyUpdates</code> is an example of an update routine.</p></item></olist></item><item><p>If the outermost expression in a query returns a <termref def="dt-pending-update-list">pending update list</termref>, <code>upd:applyUpdates</code> is implicitly applied to this pending update list. This invocation of <code>upd:applyUpdates</code> may raise an error (see <specref ref="id-upd-apply-updates"/> for possible error codes.)</p></item></olist><p><termdef id="dt-snapshot" term="snapshot">A <term>snapshot</term> is a scope within which expressions are evaluated
with respect to a fixed <termref def="dt-xdm-instance">XDM instance</termref> and updates are held pending.</termdef> A snapshot
is terminated by invocation of the <code>upd:applyUpdates</code> operation. XQuery Update Facility 1.0 currently defines an entire query as one snapshot.</p><p>This specification defines the semantics of updates to an <termref def="dt-xdm-instance">XDM instance</termref>.
Propagation of these updates to an underlying persistent store (if any)
is beyond the scope of this specification.</p></div2>

<div2 id="id-prolog-extensions"><head>Extensions to the Prolog</head><scrap headstyle="show"> 
<head/> 
<prod num="7" id="doc-xquery-Setter"><lhs>Setter</lhs><rhs><nt def="prod-xquery-BoundarySpaceDecl">BoundarySpaceDecl</nt>  |  <nt def="prod-xquery-DefaultCollationDecl">DefaultCollationDecl</nt>  |  <nt def="prod-xquery-BaseURIDecl">BaseURIDecl</nt>  |  <nt def="prod-xquery-ConstructionDecl">ConstructionDecl</nt>  |  <nt def="prod-xquery-OrderingModeDecl">OrderingModeDecl</nt>  |  <nt def="prod-xquery-EmptyOrderDecl">EmptyOrderDecl</nt>  |  <nt def="doc-xquery-RevalidationDecl">RevalidationDecl</nt>  |  <nt def="prod-xquery-CopyNamespacesDecl">CopyNamespacesDecl</nt></rhs></prod> 
<prod num="141" id="doc-xquery-RevalidationDecl"><lhs>RevalidationDecl</lhs><rhs>"declare"  "revalidation"  ("strict"  |  "lax"  |  "skip")</rhs></prod>
</scrap><p>The Prolog is extended by adding a new kind of <nt def="doc-xquery-Setter">Setter</nt>
called a revalidation declaration. 
<termdef term="revalidation declaration" id="dt-revalidation-decl">A <term>revalidation declaration</term>
sets the <termref def="dt-revalidation-mode">revalidation mode</termref> in the static context,
overriding any implementation-defined default.</termdef>
If a Prolog contains more than one revalidation declaration,
a static error is raised <errorref code="0003" class="ST"/>. 
<termref def="dt-revalidation-mode">Revalidation mode</termref> controls the process by which type information
is recovered for an updated document, as described in <specref ref="id-upd-revalidate"/></p>

<p>Support for each of the three revalidation modes is implementation-defined; however, an implementation must support at least one of the three revalidation modes. If a revalidation declaration specifies a revalidation mode that is not supported by the current implementation, a static error is raised <errorref code="0026" class="ST"/>.</p></div2>

<div2 id="id-static-context-extensions"><head>Extensions to the Static Context</head>

<olist>

<item><p>The following definition is added to the XQuery static context
(documented in <xspecref ref="static_context" spec="XQ"/>):</p>

<p><termdef term="revalidation mode" id="dt-revalidation-mode"><term>Revalidation mode</term>,
which may be <code>strict</code>, <code>lax</code>, or <code>skip</code>,
is a component of the static context that controls the behavior of the
<code>upd:revalidate</code> operation.</termdef></p>
</item>

<item>
<p>The following entry is added to the table of static context components
(documented in <xspecref ref="id-xq-static-context-components" spec="XQ"/>):</p>

<ulist>

<item>
<p>Component: Revalidation mode</p>
</item>

<item>
<p>Default initial value: <code>strict</code>.</p>
</item>

<item>
<p>Can be overwritten by an implementation: Yes (implementation defined.)</p>
</item>

<item>
<p>Can be overwritten by a query: Yes, overwritable by declaration in query prolog.</p>
</item>

<item>
<p>Scope: Global.</p>
</item>

<item>
<p>Consistency rules: Must be <code>strict</code>, <code>lax</code>, or <code>skip</code>.</p>
</item>

</ulist>

</item>

</olist>

</div2>

<div2 id="id-new-expressions"><head>New Kinds of Expressions</head>
<scrap headstyle="show"> 
<head/> 
<prod num="32" id="doc-xquery-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt def="prod-xquery-FLWORExpr">FLWORExpr</nt><br/>|  <nt def="prod-xquery-QuantifiedExpr">QuantifiedExpr</nt><br/>|  <nt def="prod-xquery-TypeswitchExpr">TypeswitchExpr</nt><br/>|  <nt def="prod-xquery-IfExpr">IfExpr</nt><br/>|  <nt def="doc-xquery-InsertExpr">InsertExpr</nt><br/>|  <nt def="doc-xquery-DeleteExpr">DeleteExpr</nt><br/>|  <nt def="doc-xquery-RenameExpr">RenameExpr</nt><br/>|  <nt def="doc-xquery-ReplaceExpr">ReplaceExpr</nt><br/>|  <nt def="doc-xquery-TransformExpr">TransformExpr</nt><br/>|  <nt def="prod-xquery-OrExpr">OrExpr</nt></rhs></prod> 

</scrap>



<p>XQuery Update Facility 1.0 extends the syntax of <nt def="doc-xquery-ExprSingle">ExprSingle</nt> by adding five new kinds of expressions, called insert, delete, replace, rename, and transform expressions.
The syntax and semantics of these expressions are described in the following sections.</p>

<note><p>In general, <termref def="dt-updating-expression">updating expressions</termref> cause a loss of type information from nodes that are affected by the update. Type information for these nodes may be recovered by a revalidation process at the end of the <termref def="dt-snapshot">snapshot</termref>. For more details on type loss and recovery, see the <termref def="dt-update-primitive">update primitives</termref> associated with each updating expression; see also <specref ref="id-upd-remove-type"/> and <specref ref="id-upd-revalidate"/>.</p></note><div3 id="id-insert"><head>Insert</head><scrap headstyle="show"><head/>
<prod num="143" id="doc-xquery-InsertExpr"><lhs>InsertExpr</lhs><rhs>"insert"  ("node"  |  "nodes")  <nt def="doc-xquery-SourceExpr">SourceExpr</nt>  <nt def="doc-xquery-InsertExprTargetChoice">InsertExprTargetChoice</nt>  <nt def="doc-xquery-TargetExpr">TargetExpr</nt></rhs></prod>
<prod num="142" id="doc-xquery-InsertExprTargetChoice"><lhs>InsertExprTargetChoice</lhs><rhs>(("as"  ("first"  |  "last"))?  "into")<br/>|  "after"<br/>|  "before"</rhs></prod>
<prod num="147" id="doc-xquery-SourceExpr"><lhs>SourceExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod><prod num="148" id="doc-xquery-TargetExpr"><lhs>TargetExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod></scrap>



<p>An insert expression is an <termref def="dt-updating-expression">updating expression</termref> that inserts copies of zero or more nodes into a 
designated position with respect to a target node. The keywords <code>node</code> and <code>nodes</code> may be used interchangeably, regardless of how many nodes are actually inserted. The position of the inserted nodes is determined as follows:</p><ulist><item><p>If <code>before</code> (or <code>after</code>) is specified:</p><ulist><item><p>The inserted nodes become the preceding (or following) siblings of the 
target node.</p></item><item><p>If multiple nodes are inserted by a single insert expression, the nodes 
remain adjacent and their order is preserved.</p></item><item><p>If multiple groups of nodes are inserted by multiple insert expressions 
in the same <termref def="dt-snapshot">snapshot</termref>, adjacency and ordering of nodes within each group is 
preserved but ordering among the groups is implementation-dependent.
</p></item></ulist></item><item><p>If <code>as first into</code> (or <code>as last into</code>) is specified:</p><ulist><item><p>The inserted nodes become the first (or last) children of the target 
node.</p></item><item><p>If multiple nodes are inserted by a single insert expression, the nodes 
remain adjacent and their order is preserved.</p></item><item><p>If multiple groups of nodes are inserted by multiple insert expressions 
in the same <termref def="dt-snapshot">snapshot</termref>, adjacency and ordering of nodes within each group is 
preserved but ordering among the groups is implementation-dependent.</p></item></ulist></item><item><p>If <code>into</code> is specified without <code>as first</code> or <code>as last</code>:</p><ulist><item><p>The inserted nodes become children of the target node.</p></item><item><p>If multiple nodes are inserted by a single insert expression, their 
order is preserved.</p></item><item><p>The positions of the inserted nodes are chosen so as not to  interfere with the intended 
position of nodes that are inserted with the specification <code>before</code>, <code>after</code>, 
<code>as first into</code>, or <code>as last into</code>. For example, If node B is inserted "after node A", 
no other node will be inserted between nodes A and B unless it is also 
inserted "after node A".</p></item><item><p>Subject to the above constraints, the positions of the inserted nodes 
among the children of the target node are implementation-dependent.</p></item></ulist></item></ulist><p>Examples:</p><ulist><item><p>Insert a <code>year</code> element after the publisher of the first book.</p><eg xml:space="preserve">insert node &lt;year&gt;2005&lt;/year&gt;
    after fn:doc("bib.xml")/books/book[1]/publisher</eg></item><item><p>Navigating by means of several bound variables, insert a new police report into the list of police reports for a particular accident.</p><eg xml:space="preserve">insert node $new-police-report
   as last into fn:doc("insurance.xml")/policies
      /policy[id = $pid]
      /driver[license = $license]
      /accident[date = $accdate]
      /police-reports</eg></item></ulist><p>The semantics of an insert 
expression are as follows:</p>

<olist>
<item><p><nt def="doc-xquery-SourceExpr">SourceExpr</nt> must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. <nt def="doc-xquery-SourceExpr">SourceExpr</nt> is evaluated as though it were an enclosed 
expression in an element constructor (see Rule 1e in <xspecref spec="XQ" ref="id-content"/>). The result of this step is either an error or a 
sequence of nodes to be inserted, called the <term>insertion sequence</term>. If the insertion sequence contains a document node, the document node is replaced in the insertion sequence by its children. If the insertion sequence contains an attribute node following a node that is not an attribute node, a type error is raised <errorref code="0004" class="TY"/>. Let <code>$alist</code> be the sequence of attribute nodes in the insertion sequence. Let <code>$clist</code> be the remainder of the insertion sequence, in its original order.</p><note><p>Either <code>$alist</code> or <code>$clist</code> or both may be empty.</p></note></item>

<item><p>The target expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The target expression is evaluated and checked as follows:</p><olist><item><p>If the result is an empty sequence, <errorref code="0027" class="DY"/> is raised.</p></item><item><p>If any form of <code>into</code> is specified, 
the result must be a single element or document node; any other non-empty result raises a type error <errorref code="0005" class="TY"/>.</p></item><item><p>If <code>before</code> or <code>after</code> 
is specified, the result must be a single element, text, comment, or processing instruction node; any other non-empty result raises a type error  <errorref code="0006" class="TY"/>.</p></item><item><p>If <code>before</code> or <code>after</code> 
is specified, the node returned by the target expression must have a non-empty <code>parent</code> property <errorref code="0029" class="DY"/>.</p></item></olist><p>  Let 
<code>$target</code> be the  node returned by the target expression.</p></item><item><p>If <code>$alist</code> is not empty and any form of <code>into</code> is specified, the following checks are performed:</p><olist><item><p><code>$target</code> must be an element node <errorref code="0022" class="TY"/>.</p></item><item><p>No attribute node in <code>$alist</code> may have a QName whose <termref def="dt-implied-namespace-binding">implied namespace binding</termref> <termref def="dt-conflict">conflicts</termref> with a namespace binding in the "namespaces" property of <code>$target</code> <errorref code="0023" class="DY"/>.</p></item></olist></item>

<item><p>If <code>$alist</code> is not empty and <code>before</code> or <code>after</code> is specified, the following checks are performed:</p><olist><item><p> <code>parent($target)</code> must be an element node <errorref code="0030" class="DY"/>.</p></item><item><p>No attribute node in <code>$alist</code> may have a QName whose <termref def="dt-implied-namespace-binding">implied namespace binding</termref> <termref def="dt-conflict">conflicts</termref> with a namespace binding in the "namespaces" property of <code>parent($target)</code> <errorref code="0023" class="DY"/>.</p></item></olist></item><item><p>The result of the insert expression is an empty <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> 
constructed as follows:</p>

<olist>
<item><p>If <code>as first into</code> is specified, the <termref def="dt-pending-update-list">pending update list</termref> consists of the following <termref def="dt-update-primitive">update primitives</termref>:</p><olist><item><p>If <code>$alist</code> is not empty, <code>upd:insertAttributes($target, $alist)</code></p></item><item><p>If <code>$clist</code> is not empty, <code>upd:insertIntoAsFirst($target, $clist)</code></p></item></olist></item>

<item><p>If <code>as last into</code> is specified, the <termref def="dt-pending-update-list">pending update list</termref> consists of the following <termref def="dt-update-primitive">update primitives</termref>:</p><olist><item><p>If <code>$alist</code> is not empty, <code>upd:insertAttributes($target, $alist)</code></p></item><item><p>If <code>$clist</code> is not empty, <code>upd:insertIntoAsLast($target, $clist)</code></p></item></olist></item>

<item><p>If <code>into</code> is specified with neither <code>as first</code> nor <code>as last</code>, the <termref def="dt-pending-update-list">pending update list</termref> consists of the following <termref def="dt-update-primitive">update primitives</termref>:</p><olist><item><p>If <code>$alist</code> is not empty, <code>upd:insertAttributes($target, $alist)</code></p></item><item><p>If <code>$clist</code> is not empty, <code>upd:insertInto($target, $clist)</code></p></item></olist></item>



<item><p>If <code>before</code> is specified, let <code>$parent</code> be the parent node of <code>$target</code>. The <termref def="dt-pending-update-list">pending update list</termref> consists of the following <termref def="dt-update-primitive">update primitives</termref>:</p><olist><item><p>If <code>$alist</code> is not empty, <code>upd:insertAttributes($parent, $alist)</code></p></item><item><p>If <code>$clist</code> is not empty, <code>upd:insertBefore($target, $clist)</code></p></item></olist></item>

<item><p>If <code>after</code> is specified, let <code>$parent</code> be the parent node of <code>$target</code>. The <termref def="dt-pending-update-list">pending update list</termref> consists of the following <termref def="dt-update-primitive">update primitives</termref>:</p><olist><item><p>If <code>$alist</code> is not empty, <code>upd:insertAttributes($parent, $alist)</code></p></item><item><p>If <code>$clist</code> is not empty, <code>upd:insertAfter($target, $clist)</code></p></item></olist></item>

</olist>
</item>
</olist></div3><div3 id="id-delete"><head>Delete</head><scrap headstyle="show"><head/>
<prod num="144" id="doc-xquery-DeleteExpr"><lhs>DeleteExpr</lhs><rhs>"delete"  ("node"  |  "nodes")  <nt def="doc-xquery-TargetExpr">TargetExpr</nt></rhs></prod>
<prod num="148" id="noid_N105D2.doc-xquery-TargetExpr"><lhs>TargetExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod></scrap>



<p>A delete expression deletes zero or more nodes from an <termref def="dt-xdm-instance">XDM instance</termref>. 
The keywords <code>node</code> and <code>nodes</code> may be used interchangeably, regardless of how many nodes are actually deleted. A delete expression is an <termref def="dt-updating-expression">updating expression</termref>.</p><p>Examples:</p><ulist><item><p>Delete the last author of the first book in a given bibliography.</p><eg xml:space="preserve">delete node fn:doc("bib.xml")/books/book[1]/author[last()]</eg></item><item><p>Delete all email messages that are more than 365 days old.</p><eg xml:space="preserve">delete nodes /email/message
     [fn:currentDate() - date &gt; xs:dayTimeDuration("P365D")]</eg></item></ulist><p>The semantics of a delete expression are as follows:
</p>

<olist>
<item><p>The target expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The target expression is evaluated. The result must be a 
sequence of zero or more nodes; otherwise a type error is raised <errorref code="0007" class="TY"/>. 
Let <code>$tlist</code> be the list of nodes returned by 
the target expression.</p></item>

<item><p>A new <termref def="dt-pending-update-list">pending update list</termref> is created. For each node 
<code>$tnode</code> in <code>$tlist</code>, the following <termref def="dt-update-primitive">update primitive</termref> is appended  to the pending update list: <code>upd:delete($tnode)</code>. The resulting 
pending update list (together with an empty <termref def="dt-xdm-instance">XDM instance</termref>)  is the result of the delete expression.</p></item>

</olist><notes><ulist><item><p>Since node deletions do not become effective until the end of a <termref def="dt-snapshot">snapshot</termref>, they have no effect on variable bindings or on the set of available documents or collections within the current query.</p></item><item><p>The <code>upd:delete($tnode)</code> primitive causes the given node to be detached from its parent after completion of the current query. If the given node has no parent, it is implementation-defined whether a dynamic error is raised <errorref code="0020" class="DY"/>.</p></item><item><p>The effects of a delete expression on persistent storage (if any) are beyond the scope of this specification.</p></item></ulist></notes></div3><div3 id="id-replace"><head>Replace</head><scrap headstyle="show"><head/>
<prod num="145" id="doc-xquery-ReplaceExpr"><lhs>ReplaceExpr</lhs><rhs>"replace"  ("value"  "of")?  "node"  <nt def="doc-xquery-TargetExpr">TargetExpr</nt>  "with"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod>
<prod num="148" id="noid_N10647.doc-xquery-TargetExpr"><lhs>TargetExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod></scrap>



<p>A replace expression is an <termref def="dt-updating-expression">updating expression</termref>. A replace expression has two forms, depending on whether <code>value of</code> 
is specified.</p>

<div4 id="id-replacing-node"><head>Replacing a Node</head><p>If <code>value of</code> is not specified, a replace expression 
 replaces one node with a new sequence of zero or more nodes. The replacement nodes occupy the position in the node hierarchy that was formerly occupied by the node that was replaced. For this reason, an attribute node can be replaced only by zero or more attribute nodes, and an element, text, comment, or processing instruction node can be replaced only by zero or more element, text, comment, or processing instruction nodes.  Example:</p><ulist><item><p>Replace the publisher of the first book with the publisher of the second book.</p><eg xml:space="preserve">replace node fn:doc("bib.xml")/books/book[1]/publisher
with fn:doc("bib.xml")/books/book[2]/publisher</eg></item></ulist><p> The semantics of this form of replace expression are as follows:
</p>

<olist><item><p>The expression following the keyword <code>with</code> must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. This expression is evaluated as though it were an enclosed 
expression in an element constructor (see Rule 1e in <xspecref spec="XQ" ref="id-content"/>). Let <code>$rlist</code> be the node sequence that results from this evaluation. If <code>$rlist</code> contains a document node, the document node is replaced in <code>$rlist</code> by its children.</p></item>



<item><p>The target expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The target expression is evaluated and checked as follows:</p><olist><item><p>If the result is an empty sequence, <errorref code="0027" class="DY"/> is raised.</p></item><item><p>If the result is non-empty and does not consist of a single element, attribute, text, comment, or processing instruction node, <errorref code="0008" class="TY"/> is raised.</p></item><item><p>If the result consists of a node whose parent property is empty, <errorref code="0009" class="DY"/> is raised.</p></item></olist><p>  Let 
<code>$target</code> be the  node returned by the target expression, and let <code>$parent</code> be its parent node.</p></item><item><p>If <code>$target</code> is an element, text, comment, or processing 
instruction node, then <code>$rlist</code> must consist exclusively of zero or more element, text, comment, or processing instruction nodes <errorref code="0010" class="TY"/>.</p>

   
</item><item><p>If <code>$target</code> is an attribute node, then:</p><olist><item><p> <code>$rlist</code> must consist exclusively of zero or more attribute nodes <errorref code="0011" class="TY"/>.</p></item><item><p>No attribute node in <code>$rlist</code> may have a QName whose <termref def="dt-implied-namespace-binding">implied namespace binding</termref> <termref def="dt-conflict">conflicts</termref> with a namespace binding in the "namespaces" property of <code>$parent</code> <errorref code="0023" class="DY"/>.</p></item></olist></item><item><p>The result of the replace expression is an empty <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> consisting of the following <termref def="dt-update-primitive">update primitive</termref>:
<code>upd:replaceNode($target, $rlist)</code></p></item>

</olist></div4><div4 id="id-replacing-node-value"><head>Replacing the Value of a Node</head><p>If <code>value of</code> is specified, 
a replace expression is used to modify the value of a node while preserving 
its <termref def="dt-node-identity">node identity</termref>. Example:</p><ulist><item><p>Increase the price of the first book by ten percent.</p><eg xml:space="preserve">replace value of node fn:doc("bib.xml")/books/book[1]/price
with fn:doc("bib.xml")/books/book[1]/price * 1.1</eg></item></ulist><p>The semantics of this form of replace expression are as 
follows:
</p>

<olist><item><p>The expression following the keyword <code>with</code> must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. This expression is evaluated as though it were the content 
expression of a text node constructor (see  Section 3.7.3.4 of 
<bibref ref="xquery"/>.) The result of this step, in the absence of errors, is 
either a single text node or an empty sequence. Let <code>$text</code> be the result of this step. 
</p></item>

<item><p>The target expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The target expression is evaluated and checked as follows:</p><olist><item><p>If the result is an empty sequence, <errorref code="0027" class="DY"/> is raised.</p></item><item><p>If the result is non-empty and does not consist of a single element, attribute, text, comment, or processing instruction node, <errorref code="0008" class="TY"/> is raised.</p></item></olist><p>  Let 
<code>$target</code> be the  node returned by the target expression.</p></item><item><p>If <code>$target</code> is an element node, the result of the replace 
expression is an empty <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> consisting of the following <termref def="dt-update-primitive">update primitive</termref>: <code>upd:replaceElementContent($target, $text)</code></p></item>



<item><p>If <code>$target</code> is an attribute, text, comment, or processing 
instruction node, let <code>$string</code> be the string value of the text node 
constructed in Step 1. If Step 1 did not construct 
a text node, let <code>$string</code> be a zero-length string.

Then:</p><olist><item><p>If <code>$target</code> is a comment node, and <code>$string</code> contains two adjacent hyphens or ends with a hyphen, a dynamic error is raised <errorref code="0072" class="DY"/>.</p></item><item><p>If <code>$target</code> is a processing instruction node, and <code>$string</code> contains the substring "<code>?&gt;</code>", a dynamic error is raised <errorref code="0026" class="DY"/>.</p></item><item><p>In the absence of errors, the result of a replace expression is an empty <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> containing the following <termref def="dt-update-primitive">update primitive</termref>: 
<code>upd:replaceValue($target, $string)</code>.</p></item></olist></item>

</olist></div4></div3><div3 id="id-rename"><head>Rename</head><scrap headstyle="show"><head/>
<prod num="146" id="doc-xquery-RenameExpr"><lhs>RenameExpr</lhs><rhs>"rename"  "node"  <nt def="doc-xquery-TargetExpr">TargetExpr</nt>  "as"  <nt def="doc-xquery-NewNameExpr">NewNameExpr</nt></rhs></prod>
<prod num="148" id="noid_N1079D.doc-xquery-TargetExpr"><lhs>TargetExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod><prod num="149" id="doc-xquery-NewNameExpr"><lhs>NewNameExpr</lhs><rhs><nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod></scrap>



<p>A rename expression replaces the <code>name</code> property of a <termref def="dt-data-model">data model</termref> node with a 
new QName. A rename expression is an <termref def="dt-updating-expression">updating expression</termref>.</p><p>Examples:</p><ulist><item><p>Rename the first <code>author</code> element of the first book to <code>principal-author</code>.</p><eg xml:space="preserve">rename node fn:doc("bib.xml")/books/book[1]/author[1]
as "principal-author"</eg></item><item><p>Rename the first <code>author</code> element of the first book to the QName that is the value of the variable <code>$newname</code>.</p><eg xml:space="preserve">rename node fn:doc("bib.xml")/books/book[1]/author[1]
as $newname</eg></item></ulist><p>The semantics of a rename expression are as follows:</p>

<olist>

<item><p>The target expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The target expression is evaluated and checked as follows:</p><olist><item><p>If the result is an empty sequence, <errorref code="0027" class="DY"/> is raised.</p></item><item><p>If the result is non-empty and does not consist of a single element, attribute, or processing instruction node, <errorref code="0012" class="TY"/> is raised.</p></item></olist><p>  Let 
<code>$target</code> be the  node returned by the target expression.</p></item><item><p><nt def="doc-xquery-NewNameExpr">NewNameExpr</nt> must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. <nt def="doc-xquery-NewNameExpr">NewNameExpr</nt> is 
evaluated as though it were the name expression of a computed element 
constructor (see Section 3.7.3.1 of <bibref ref="xquery"/>.) 
The result is either an error or an expanded QName. Let <code>$QName</code> 
be  this expanded QName.</p></item><item><p> The following checks are performed for error conditions:</p><olist><item><p>If <code>$target</code> is an element node, the "namespaces" property of <code>$target</code> must not include any namespace binding that <termref def="dt-conflict">conflicts</termref> with the <termref def="dt-implied-namespace-binding">implied namespace binding</termref> of <code>$QName</code> <errorref code="0023" class="DY"/>.</p></item><item><p>If <code>$target</code> is an attribute node that has a parent, the "namespaces" property of <code>parent($target)</code> must not include any namespace binding that <termref def="dt-conflict">conflicts</termref> with the <termref def="dt-implied-namespace-binding">implied namespace binding</termref> of <code>$QName</code> <errorref code="0023" class="DY"/>.</p></item><item><p>If <code>$target</code> is processing instruction node, <code>$QName</code> must not include a non-empty namespace prefix. <errorref code="0025" class="DY"/>.</p></item></olist></item>

<item><p>The result of the rename expression is an empty <termref def="dt-xdm-instance">XDM instance</termref> and a <termref def="dt-pending-update-list">pending update list</termref> 
containing the following <termref def="dt-update-primitive">update primitive</termref>: 
<code>upd:rename($target, $QName)</code>.</p></item>

</olist><note><p>The effects of a rename expression are limited to its target node. Attributes and descendants of the target node are not affected. If a global change of names or namespaces is intended, some form of explicit iteration must be used. The following example illustrates such a global change. The example operates on the node bound to variable <code>$root</code> and all its attributes and descendants, changing all QNames with the prefix <code>abc</code> to have a new prefix <code>xyz</code> and a new namespace URI <code>http://xyz/ns</code>.<eg xml:space="preserve">for $node in $root//abc:*
let $localName := fn:local-name($node),
    $newQName := fn:concat("xyz:", $localName)
return
   rename node $node as fn:QName("http://xyz/ns", $newQName),
   for $attr in $node/@abc:*
   let $attrLocalName := fn:local-name($attr),
       $attrNewQName := fn:concat("xyz:", $attrLocalName)
   return
      rename node $attr as fn:QName("http://xyz/ns", $attrNewQName)</eg></p></note></div3><div3 id="id-transform"><head>Transform</head><scrap headstyle="show"><head/>
<prod num="150" id="doc-xquery-TransformExpr"><lhs>TransformExpr</lhs><rhs>"copy"  "$"  <nt def="prod-xquery-VarName">VarName</nt>  ":="  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>  (","  "$"  <nt def="prod-xquery-VarName">VarName</nt>  ":="  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>)*  "modify"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt>  "return"  <nt def="doc-xquery-ExprSingle">ExprSingle</nt></rhs></prod>
</scrap>



<p>A transform expression can be used to create modified copies of existing nodes in an <termref def="dt-xdm-instance">XDM instance</termref>. Each node created by a transform expression has a new <termref def="dt-node-identity">node identity</termref>. The result of a transform expression is an <termref def="dt-xdm-instance">XDM instance</termref> that may include both nodes that were created by the transform expression and other, previously existing nodes. A transform expression is a <termref def="dt-non-updating-expression">non-updating expression</termref> because it does not modify the value of any existing nodes.</p><p>Examples:</p><ulist><item><p>Return a sequence consisting of all <code>employee</code> elements that have Java as a skill, excluding their <code>salary</code> child-elements:</p><eg xml:space="preserve">for $e in //employee[skill = "Java"]
return 
   copy $je := $e
   modify delete node $je/salary
   return $je</eg></item><item><p>The following example copies a node, modifies the copy, and returns both the original node and the modified copy:</p><eg xml:space="preserve">let $oldx := /a/b/x
return
   copy $newx := $oldx
   modify (rename node $newx as "newx", 
           replace value of node $newx by $newx * 2)
   return ($oldx, $newx)
</eg><note><p>No persistent changes to the underlying data result from this example.</p></note></item></ulist><p>A transform expression consists of three clauses, denoted by the keywords <code>copy</code>, <code>modify</code>, and <code>return</code>. The semantics of a transform expression are as follows:</p><olist><item><p>The <code>copy</code> clause contains one or more variable bindings, each of which consists of a variable name and an expression called the <term>source expression</term>. Each variable binding is processed as follows:</p><olist><item><p>The source expression must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The source expression is evaluated as though it were an enclosed 
expression in an element constructor (see Rule 1e in <xspecref spec="XQ" ref="id-content"/>.) The result of this step must be a single node <errorref code="0013" class="TY"/>.</p><note><p>The node returned by the source expression  is a new node whose <termref def="dt-node-identity">node identity</termref> is distinct from that of any previously existing node.</p></note></item><item><p>The variable name is bound to the node returned by the source expression. The scope of this variable binding includes all subexpressions of the containing transform expression that appear after the variable binding clause, including the source expressions of later variable bindings, but it does not include the source expression to which the current variable name is bound.</p></item></olist></item><item><p>The <code>modify</code> clause must contain either an <termref def="dt-updating-expression">updating expression</termref>, an empty expression <code>( )</code>, or a call to the <code>fn:error</code> function; otherwise a static error is raised <errorref code="0002" class="ST"/>. The  expression in the <code>modify</code> clause is evaluated, resulting in a <termref def="dt-pending-update-list">pending update list</termref>. If the <termref def="dt-target-node">target node</termref> of any <termref def="dt-update-primitive">update primitive</termref> on this pending update list is a node that was not newly created in Step 1, a dynamic error is raised <errorref code="0014" class="DY"/>. Let <code>$pul</code> be the pending update list generated by this step.</p></item><item><p>The following <termref def="dt-update-operation">update operation</termref> is invoked: <code>upd:applyUpdates($pul)</code>. The effect of this operation is to make the updates specified in the <code>modify</code> clause effective on the copied nodes.</p><note><p>In the event of incompatible updates, the <code>upd:applyUpdates</code> operation may raise an error, as described in <specref ref="id-upd-apply-updates"/>.</p></note></item><item><p>The <code>return</code> clause must contain a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>. The <code>return</code> clause is evaluated, and its result is the result of the transform expression. During evaluation of the <code>return</code> clause, changes applied to copied nodes by the preceding step are visible.</p></item></olist></div3><div3 id="id-compatibility"><head>Compatibility of Updating Expressions</head><p>The rules defining compatibility of <termref def="dt-updating-expression">updating expressions</termref> within a <termref def="dt-snapshot">snapshot</termref> are defined in <specref ref="id-upd-apply-updates"/>.</p><note><p>The effect of these rules is as follows:</p><olist><item><p>If any node is affected by more than one <code>rename</code> expression within a  <termref def="dt-snapshot">snapshot</termref>, a dynamic error is raised <errorref code="0015" class="DY"/>.</p></item><item><p>If any node is affected by more than one <code>replace</code> expression (without <code>value of</code> being specified) within a  <termref def="dt-snapshot">snapshot</termref>, a dynamic error is raised <errorref code="0016" class="DY"/>.</p></item><item><p>If any node is affected by more than one <code>replace value of</code> expression within a  <termref def="dt-snapshot">snapshot</termref>, a dynamic error is raised <errorref code="0017" class="DY"/>.</p></item><item><p>Within a given <termref def="dt-snapshot">snapshot</termref>, if an element node <code>E</code> is the target of a <code>replace value of</code> expression, and the children of <code>E</code> are also modified by other expressions, the final children of <code>E</code> are determined by the <code>replace value of</code> expression. For example:</p><ulist><item><p>Suppose that <code>$A</code> is bound to an element node that has a child element named <code>B</code>. Suppose that the following expressions are evaluated in the same <termref def="dt-snapshot">snapshot</termref>:</p><eg xml:space="preserve">replace node $A/B with &lt;C&gt;Hello&lt;/C&gt;,
replace value of node $A with &lt;D&gt;Goodbye&lt;/D&gt;</eg> <p>The expressions on the left and right side of the comma can be evaluated in any order. No error is raised. At the end of the <termref def="dt-snapshot">snapshot</termref>, the children of <code>$A</code> will consist of a single text node with the content <code>"Goodbye"</code>.</p></item></ulist></item></olist></note></div3></div2>
<div2 id="id-extensions-to-existing"><head>Extensions to Existing Expressions</head><p>XQuery Update Facility 1.0 provides extensions to the semantics of several existing kinds of XQuery expressions, as specified in this section.</p><div3 id="id-flwor-expr"><head>FLWOR Expression</head>

<p>The syntax of the FLWOR expression is not changed. Its semantics are extended as follows:</p>

<olist>

<item><p>If a <code>for</code>,
<code>let</code>, <code>where</code>, or <code>order by</code>
clause contains an <termref def="dt-updating-expression">updating expression</termref>, a static error is raised <errorref code="0001" class="ST"/>.</p></item><item><p>If the <code>return</code> clause contains a <termref def="dt-non-updating-expression">non-updating expression</termref>, the FLWOR expression is a <termref def="dt-non-updating-expression">non-updating expression</termref>, and its semantics are as specified in Section 3.8 of <bibref ref="xquery"/>.</p></item><item><p>If the <code>return</code> clause contains an <termref def="dt-updating-expression">updating expression</termref>, the FLWOR expression is an <termref def="dt-updating-expression">updating expression</termref>, and its semantics are as follows:</p><olist>

<item><p>The semantics of the <code>for</code>,
<code>let</code>, <code>where</code>, and <code>order by</code>
clauses are as specified in Section 3.8 of <bibref ref="xquery"/>. These clauses generate a stream of tuples of bound
variables.</p></item>

<item><p>For each tuple
generated by the previous step, the updating expression in the
<code>return</code> clause is evaluated, resulting in a <termref def="dt-pending-update-list">pending update list</termref>.</p></item>

<item><p>All the <termref def="dt-pending-update-list">pending update lists</termref> generated by the
previous step are merged by successive invocations of the <code>upd:mergeUpdates</code>
operation. The resulting merged pending update list is the
result of the FLWOR expression.</p><note><p>In the event of incompatible updates, the <code>upd:mergeUpdates</code> operation may raise an error, as described in <specref ref="id-upd-merge-updates"/>.</p></note></item>
</olist></item>

</olist><p>The following example illustrates the use of an updating expression in a FLWOR expression:</p><ulist><item><p>Update an inventory of parts according to a set of changes provided in the bound variable <code>$changes</code>. Both <code>/inventory</code> and <code>$changes</code> contain a set of <code>part</code> elements, each with a <code>partno</code> and a <code>quantity</code>.</p><eg xml:space="preserve">for $p in /inventory/part
let $deltap := $changes/part[partno eq $p/partno]
return 
    replace value of node $p/quantity
    with $p/quantity + $deltap/quantity</eg></item></ulist></div3><div3 id="id-typeswitch-expr"><head>Typeswitch Expression</head>

<p>The syntax of the typeswitch expression is not changed. Its semantics are extended as
follows (the term "branch" refers to any <code>case</code>  or <code>default</code> clause in the typeswitch expression):
</p>

<olist>

<item><p>If the operand expression of a typeswitch is an <termref def="dt-updating-expression">updating expression</termref>, a static error is raised <errorref code="0001" class="ST"/>.</p></item><item><p>If any branch contains an <termref def="dt-updating-expression">updating expression</termref>, the typeswitch expression is an <termref def="dt-updating-expression">updating expression</termref>; otherwise it is a <termref def="dt-non-updating-expression">non-updating expression</termref>.</p></item><item><p>If the typeswitch expression is a <termref def="dt-non-updating-expression">non-updating expression</termref>, its semantics are as specified in Section 3.12.2 of
<bibref ref="xquery"/>.</p></item><item><p>If the typeswitch expression is an <termref def="dt-updating-expression">updating expression</termref>, then all of its branches must contain either an <termref def="dt-updating-expression">updating expression</termref>, an empty expression <code>( )</code>, or a call to the <code>fn:error</code> function; otherwise a static error is raised <errorref code="0001" class="ST"/>. In this case, selection of the effective case and
binding of variables is performed as specified in Section 3.12.2 of
<bibref ref="xquery"/>. The  expression in the <code>return</code>
clause of the effective case (or default) is then evaluated, resulting
in a <termref def="dt-pending-update-list">pending update list</termref>, which serves as the result of
the typeswitch expression.
</p></item>



</olist></div3><div3 id="id-conditional-expr"><head>Conditional Expression</head><p>The semantics of
conditional expressions are extended as follows (the term "branch" refers to the <code>then</code> and <code>else</code> clauses in the conditional expression):
</p>

<olist>

<item><p>If the if-clause contains an <termref def="dt-updating-expression">updating expression</termref>, a static error is raised <errorref code="0001" class="ST"/>.</p></item><item><p>If either branch contains an <termref def="dt-updating-expression">updating expression</termref>, the conditional expression is an <termref def="dt-updating-expression">updating expression</termref>; otherwise it is a <termref def="dt-non-updating-expression">non-updating expression</termref>.</p></item><item><p>If the conditional expression is a <termref def="dt-non-updating-expression">non-updating expression</termref>, its semantics are as specified in Section 3.10 of
<bibref ref="xquery"/>.</p></item><item><p>If the conditional expression is an <termref def="dt-updating-expression">updating expression</termref>, then both of its branches must contain either an <termref def="dt-updating-expression">updating expression</termref>, an empty expression <code>( )</code>, or a call to the <code>fn:error</code> function; otherwise a static error is raised <errorref code="0001" class="ST"/>. In this case, the test
expression is evaluated and the <code>then</code> or <code>else</code>
clause is selected and evaluated as specified in Section 3.10 of
<bibref ref="xquery"/>. The result of the conditional expression is
the <termref def="dt-pending-update-list">pending update list</termref> returned by the
selected branch.</p></item>



</olist><p>The following example illustrates the use of updating expressions in a conditional expression:</p><ulist><item><p>If the element bound to variable <code>$e</code> has a <code>last-updated</code> attribute, update its value to the current date; otherwise insert such an attribute.</p><eg xml:space="preserve">if ($e/@last-updated)
then replace value of node 
        $e/last-updated with fn:currentDate()
else insert node 
        attribute last-updated {fn:currentDate()} into $e</eg></item></ulist></div3><div3 id="id-comma-expr"><head>Comma Expression</head><p>The semantics of
comma expressions (composed of one or more expressions concatenated by the comma operator, as described in Section 3.3.1 of  <bibref ref="xquery"/>) are extended as follows:
</p>

<olist>

<item><p>If any operand of the comma expression is an <termref def="dt-updating-expression">updating expression</termref>, the comma expression is an <termref def="dt-updating-expression">updating expression</termref>; otherwise it is a <termref def="dt-non-updating-expression">non-updating expression</termref>.</p></item><item><p>If the comma expression is a <termref def="dt-non-updating-expression">non-updating expression</termref>, its semantics are as specified in Section 3.3.1 of
<bibref ref="xquery"/>.</p></item><item><p>If the comma expression is an <termref def="dt-updating-expression">updating expression</termref>, then each of its operands must be either an <termref def="dt-updating-expression">updating expression</termref>, an empty expression <code>( )</code>, or a call to the <code>fn:error</code> function; otherwise a static error is raised <errorref code="0001" class="ST"/>. In this case, the operand expressions are evaluated (in any order), and the <termref def="dt-pending-update-list">pending update lists</termref>
returned by the operand expressions are merged by the <code>upd:mergeUpdates</code>
operation. The resulting merged <termref def="dt-pending-update-list">pending update list</termref> is the result of the comma expression.</p></item>



</olist><p>The following example illustrates the use of an updating comma expression:</p><ulist><item><p>This example makes the value of an element empty and gives the element an <code>xsi:nil="true"</code> attribute. Both of these operations may be necessary in order to preserve the validity of the element.</p><eg xml:space="preserve">let $q := /inventory/item[serialno = "123456"]/quantity
return
   ( replace value of node $q with ( ),
     insert node attribute xsi:nil {"true"} into $q )</eg></item></ulist></div3><div3 id="id-parenthesized-expr"><head>Parenthesized Expression</head><p>The semantics of a parenthesized expression (any XQuery expression enclosed in parentheses) are extended as follows:</p><p>The category of a parenthesized expression is the same as the category of its operand expression, which may be an <termref def="dt-updating-expression">updating expression</termref> or a <termref def="dt-non-updating-expression">non-updating expression</termref>. The result of a parenthesized expression is also the same as the result of its operand expression. An empty parenthesized expression <code>( )</code> is a <termref def="dt-non-updating-expression">non-updating expression</termref>, and its result is an empty sequence.</p></div3><div3 id="id-function-declaration"><head>Function Declaration</head><scrap headstyle="show"><head/>
<prod num="26" id="doc-xquery-FunctionDecl"><lhs>FunctionDecl</lhs><rhs>"declare"  "updating"?  "function"  <nt def="prod-xquery-QName">QName</nt>  "("  <nt def="prod-xquery-ParamList">ParamList</nt>?  ")"  ("as"  <nt def="prod-xquery-SequenceType">SequenceType</nt>)?  (<nt def="prod-xquery-EnclosedExpr">EnclosedExpr</nt>  |  "external")</rhs></prod>
</scrap>

<p>The syntax of a function declaration is extended to include an optional keyword: <code>updating</code>. <termdef term="updating function" id="dt-updating-function">A function whose declaration contains the keyword updating is called an <term>updating function</term>.</termdef> The semantics of a function declaration, described in Section 4.15 of <bibref ref="xquery"/>,  are extended as follows:</p><olist><item><p>If <code>updating</code> is not specified:</p><olist><item><p>If <code>external</code> is not specified, the EnclosedExpr in the function declaration must be a <termref def="dt-non-updating-expression">non-updating expression</termref>; otherwise a static error is raised <errorref code="0001" class="ST"/>.</p></item><item><p>If <code>external</code> is specified, the external function  must not return a non-empty <termref def="dt-pending-update-list">pending update list</termref>; otherwise a dynamic error is raised <errorref code="0018" class="DY"/>.</p></item></olist></item><item><p>If <code>updating</code> is specified:</p><olist><item><p>A return type must not  be specified <errorref code="0028" class="ST"/>. The return type of an updating function is assumed to be the empty
sequence.</p></item><item><p>If <code>external</code> is not specified, the EnclosedExpr in the function declaration must be an <termref def="dt-updating-expression">updating expression</termref>, an empty expression <code>( )</code>, or a call to the <code>fn:error</code> function; otherwise a static error is raised <errorref code="0002" class="ST"/>.</p></item><item><p>If <code>external</code> is specified, the external function may return a non-empty <termref def="dt-pending-update-list">pending update list</termref> but it must not return a non-empty <termref def="dt-xdm-instance">XDM instance</termref>; otherwise a dynamic error is raised <errorref code="0019" class="DY"/>.</p></item></olist></item></olist><p>The means by which an external function returns an <termref def="dt-xdm-instance">XDM instance</termref> or a <termref def="dt-pending-update-list">pending update list</termref> is implementation-defined.</p><p>The following example illustrates a declaration of an updating function.</p><ulist><item><p>This function takes an element, a QName, and an atomic value. If the given element has an attribute with the given QName, the function updates the attribute with the given value; otherwise it inserts a new attribute with the given name and value.</p><eg xml:space="preserve">declare updating function 
   upsert($e as element(), 
          $an as xs:QName, 
          $av as xs:anyAtomicType) 
   {
   let $ea := $e/attribute()[fn:node-name(.) = $an]
   return
      if (fn:empty($ea))
      then insert node attribute {$an} {$av} into $e
      else replace value of node $ea with $av
   }</eg></item></ulist></div3><div3 id="id-function-call"><head>Function Call</head><p>The semantics of a function call are extended as follows:</p><p>The function call is evaluated as specified in Section 3.1.5 of <bibref ref="xquery"/>. If any input parameter of the function  call is an <termref def="dt-updating-expression">updating expression</termref>, a static error is raised <errorref code="0001" class="ST"/>. If the called function was declared with the <code>updating</code> keyword, the function call is an <termref def="dt-updating-expression">updating expression</termref>. If the called function was declared without the <code>updating</code> keyword, the function call is a <termref def="dt-updating-expression">non-updating expression</termref>.</p></div3><div3 id="id-other-expr"><head>Other Expressions</head><p>The semantics of all XQuery expressions other than FLWOR expressions, 
typeswitch expressions, conditional expressions, comma expressions, 
parenthesized expressions, and function calls are extended as follows:</p><p>If any operand of this expression is an <termref def="dt-updating-expression">updating expression</termref>, a static error is raised <errorref code="0001" class="ST"/>.</p><p>In addition, the initializing expression of a variable declaration in a Prolog may not be an <termref def="dt-updating-expression">updating expression</termref> <errorref code="0001" class="ST"/>.</p></div3></div2>

<div2 id="id-new-functions">
  <head>Extensions to Built-in Function Library</head>
  
  <p>XQuery Update Facility 1.0 provides extensions to XQuery built-in function library, as specified in this
  section.</p> 

  <div3 id="id-func-put">
    <head>fn:put</head>
      



<proto name="put" isSchema="no" isDatatype="no" isSpecial="no" isOp="no" return-type="empty-sequence()" isStd="yes" returnEmptyOk="no" returnSeq="no" returnVaries="no">
<arg name="node" type="node()" emptyOk="no"/>
<arg name="uri" type="xs:string" emptyOk="no"/>
</proto>

<p>Summary: Stores  a document or element to the location specified by <code>$uri</code>.
This function is normally invoked to create
a resource on an external storage system such as a file system or a
database.</p>



<p>If <code>$node</code> is not a document node or an element node, and the implementation does not support <code>fn:put</code> on the given node kind, a dynamic error is
raised <errorref code="0001" class="UP"/>.</p><p>If
<code>$uri</code> is not a valid lexical representation of the <code>xs:anyURI</code> type, a dynamic error is
raised <errorref code="0002" class="UP"/>. If <code>$uri</code> is a relative URI Reference, it is resolved
relative to the value of the base URI property in the static
context.</p>

<p>The semantics of <code>fn:put</code> are implementation-defined,
since they occur completely outside the domain of XQuery. The intent is that, if <code>fn:put</code> is invoked on a document node and no error is raised, a
subsequent query can access the stored document by invoking
<code>fn:doc</code> with the same URI.</p><p>The results of <code>fn:put</code> do not become effective until after completion of the query containing the <code>fn:put</code> function--that is, until after completion of the <code>upd:applyUpdates</code> operation invoked by the outermost expression in the current query. The <code>fn:put</code> function has no effect on the current query. (For example, it does not change the set of available documents or collections seen by the current query.)</p>










  </div3>

</div2>



</div1>


<div1 id="id-update-operations">
<head>Update Operations</head>
<p>This section describes the update operations defined by XQuery Update Facility 1.0. Although these update operations are described using a functional notation, they are not true functions because many of them have no return value. These update operations are used in defining the semantics of XQuery expressions, but they are not directly available to users.</p><p>Update operations consist of <termref def="dt-update-primitive">update primitives</termref>, which are the components of <termref def="dt-pending-update-list">pending update lists</termref>, and <termref def="dt-update-routine">update routines</termref>, which are used in defining XQuery semantics but do not appear on <termref def="dt-pending-update-list">pending update lists</termref>.</p>
<div2 id="id-update-primitives"><head>Update Primitives</head><p>The update primitives described in this section may be held on <termref def="dt-pending-update-list">pending update lists</termref>. When an update primitive is held on a pending update list, its node operands are represented by their <termref def="dt-node-identity">node identities</termref>. The semantics of an update primitive do not become effective until their pending update list is processed by the  <code>upd:applyUpdates</code> routine.</p><div3 id="id-upd-insert-before"><head>upd:insertBefore</head>
					<glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertBefore(
   $target as node(),
   $content as node()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> immediately before <code>$target</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element, text, processing
									instruction, or comment node with a non-empty <code>parent</code> property. <code>$content</code> must be a sequence containing only element, text, processing instruction,
									and comment nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>Effects on nodes in <code>$content</code>:</p><olist><item><p>For each node in <code>$content</code>, the <code>parent</code> property is set to <code>parent($target)</code>.</p></item><item><p>If the <code>type-name</code> property of <code>parent($target)</code> is <code>xs:untyped</code>, then <code>upd:setToUntyped()</code> is invoked on each element or attribute node in <code>$content</code>.</p></item></olist></item><item><p>Effects on <code>parent($target)</code>:</p><olist><item><p>The <code>children</code> property of <code>parent($target)</code> is modified to add the nodes in <code>$content</code> just before <code>$target</code>, preserving their order.</p></item><item><p>If at least one of the nodes in <code>$content</code> is an element or text node, <code>upd:removeType(parent($target))</code> is invoked.</p></item></olist></item></olist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-insert-after"><head>upd:insertAfter</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertAfter(
   $target as node(),
   $content as node()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> immediately after <code>$target</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element, text, processing
									instruction, or comment node with a non-empty <code>parent</code> property. <code>$content</code> must be a sequence containing only element, text, processing instruction,
									and comment nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><p>The semantics of <code>upd:insertAfter</code> are identical to the semantics of <code>upd:insertBefore</code>, except that Rule 2a is changed as follows:</p><ulist><item><p>The <code>children</code> property of <code>parent($target)</code> is modified to add the nodes in <code>$content</code> just after <code>$target</code>, preserving their order.</p></item></ulist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-insert-into"><head>upd:insertInto</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertInto(
   $target as node(),
   $content as node()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> as the children of <code>$target</code>, in an implementation-dependent position.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element or document node. <code>$content</code> must be a sequence containing only element, text, processing instruction,
									and comment nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><p>The semantics of <code>upd:insertInto</code> are identical to the semantics of <code>upd:insertBefore</code>, except that <code>$target</code> is substituted everywhere for <code>parent($target)</code>, and Rule 2a is changed as follows:</p><ulist><item><p>The <code>children</code> property of <code>$target</code> is changed to include the nodes in <code>$content</code> in implementation-dependent positions, preserving their relative order.</p></item></ulist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-insert-into-as-first"><head>upd:insertIntoAsFirst</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertIntoAsFirst(
   $target as node(),
   $content as node()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> as the first children of <code>$target</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element or document node. <code>$content</code> must be a sequence containing only element, text, processing instruction,
									and comment nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><p>The semantics of <code>upd:insertIntoAsFirst</code> are identical to the semantics of <code>upd:insertBefore</code>, except that <code>$target</code> is substituted everywhere for <code>parent($target)</code>, and Rule 2a is changed as follows:</p><ulist><item><p>The <code>children</code> property of <code>$target</code> is changed to include the nodes in <code>$content</code> as the first children, preserving their order.</p></item></ulist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-insert-into-as-last"><head>upd:insertIntoAsLast</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertIntoAsLast(
   $target as node(),
   $content as node()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> as the last children of <code>$target</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element or document node. <code>$content</code> must be a sequence containing only element, text, processing instruction,
									and comment nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><p>The semantics of <code>upd:insertIntoAsLast</code> are identical to the semantics of <code>upd:insertBefore</code>, except that <code>$target</code> is substituted everywhere for <code>parent($target)</code>, and Rule 2a is changed as follows:</p><ulist><item><p>The <code>children</code> property of <code>$target</code> is changed to include the nodes in <code>$content</code> as the last children, preserving their order.</p></item></ulist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-insert-attributes"><head>upd:insertAttributes</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:insertAttributes(
   $target as element(),
   $content as attribute()+)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Inserts <code>$content</code> as attributes of <code>$target</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p>None</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>Error checks:</p><olist><item><p>If the QNames of any two attribute nodes in <code>$content</code> have <termref def="dt-implied-namespace-binding">implied namespace bindings</termref> that <termref def="dt-conflict">conflict</termref> with each other, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item><item><p>If the QName of any attribute node in <code>$content</code> has an <termref def="dt-implied-namespace-binding">implied namespace binding</termref> that <termref def="dt-conflict">conflicts</termref> with a namespace binding in the "namespaces" property of <code>$target</code>, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item></olist></item><item><p>For each node <code>$A</code> in <code>$content</code>:</p><olist><item><p>The <code>parent</code> property of <code>$A</code> is set to <code>$target</code>.</p></item><item><p>If the <code>type-name</code> property of <code>$target</code> is <code>xs:untyped</code>, then <code>upd:setToUntyped($A)</code> is invoked.</p></item></olist></item><item><p>The following properties of <code>$target</code> are changed:</p><olist><item><p><code>attributes</code>: Modified to include the nodes in <code>$content</code>.</p></item><item><p><code>namespaces:</code> Modified to include namespace bindings for any attribute namespace prefixes in <code>$content</code> that did not already have bindings.</p></item><item><p><code>upd:removeType($target)</code> is invoked.</p></item></olist></item></olist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-delete"><head id="id-dm-delete">upd:delete</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:delete(
   $target as node())</eg></def></gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p>None</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>If <code>$target</code> has no parent, the <termref def="dt-xdm-instance">XDM instance</termref> is unchanged. An implementation may (but is not required to) raise a dynamic error if <code>$target</code> had no parent before execution of the query began. <errorref code="0020" class="DY"/>.</p></item><item><p>If <code>$target</code> has a parent node <code>$P</code>, then:</p><olist><item><p>The <code>parent</code> property of <code>$target</code> is set to empty.</p></item><item><p>If <code>$target</code> is an attribute node, the <code>attributes</code> property of <code>$P</code> is modified to remove <code>$target</code>.</p></item><item><p>If <code>$target</code> is a non-attribute node, the <code>children</code> property of <code>$P</code> is modified to remove <code>$target</code>.</p></item><item><p>If <code>$target</code> is an element, attribute, or text node, and <code>$P</code> is an element node, then <code>upd:removeType($P)</code>  is invoked.</p></item></olist></item></olist><note><p>Deleted nodes are detached from their parent nodes; however, a node deletion has no effect on variable bindings or on the set of available documents or collections during processing of the current query.</p></note><note><p>Multiple <code>upd:delete</code> operations may be applied to the same node during execution of a query; this is not an error.</p></note></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-replacenode"><head>upd:replaceNode</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:replaceNode(
   $target as node(),
   $replacement as node()*)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Replaces <code>$target</code> with <code>$replacement</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be a node that has a parent. If <code>$target</code> is an attribute node, <code>$replacement</code> must consist of zero or more attribute nodes. If <code>$target</code> is an element, text, comment, or processing instruction node, <code>$replacement</code> must be consist of zero or more element, text, comment, or processing instruction nodes.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>Error checks:</p><olist><item><p>If the QNames of any two attribute nodes in <code>$replacement</code> have <termref def="dt-implied-namespace-binding">implied namespace bindings</termref> that <termref def="dt-conflict">conflict</termref> with each other, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item><item><p>If the QName of any attribute node in <code>$replacement</code> has an <termref def="dt-implied-namespace-binding">implied namespace binding</termref> that <termref def="dt-conflict">conflicts</termref> with a namespace binding in the "namespaces" property of <code>parent($target)</code>, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item></olist></item><item><p>Effects on nodes in <code>$replacement</code>:</p><olist><item><p>For each node in <code>$replacement</code>, the <code>parent</code> property is set to <code>parent($target)</code>.</p></item><item><p>If the <code>type-name</code> property of <code>parent($target)</code> is <code>xs:untyped</code>, then <code>upd:setToUntyped()</code> is invoked on each element node in <code>$replacement</code>.</p></item></olist></item><item><p>Effect on <code>$target</code>:</p><olist><item><p>The <code>parent</code> property of <code>$target</code> is set to empty.</p></item></olist></item><item><p>Effects on <code>parent($target)</code>:</p><olist><item><p>If <code>$target</code> is an attribute node, the <code>attributes</code> property of <code>parent($target)</code> is modified by removing <code>$target</code> and adding the nodes in <code>$replacement</code> (if any).</p></item><item><p>If <code>$target</code> is an attribute node, the <code>namespaces</code> property of <code>parent($target)</code> is modified to include namespace bindings for any attribute namespace prefixes in <code>$replacement</code> that did not already have bindings.</p></item><item><p>If <code>$target</code> is an element, text, comment, or processing instruction node, the <code>children</code> property of <code>parent($target)</code> is modified by removing <code>$target</code> and adding the nodes in <code>$replacement</code> (if any) in the former position of <code>$target</code>, preserving their order.</p></item><item><p><code>upd:removeType(parent($target))</code> is invoked.</p></item></olist></item></olist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-replace-value"><head>upd:replaceValue</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:replaceValue(
   $target as node(),
   $string-value as xs:string)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Replaces the string value of <code>$target</code> with <code>$string-value</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an attribute, text, comment, or processing instruction node.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>If <code>$target</code> is an attribute node:</p><olist><item><p><code>string-value</code> of <code>$target</code> is set to <code>$string-value</code>.</p></item><item><p><code>upd:removeType($target)</code> is invoked.</p></item></olist></item><item><p>If <code>$target</code> is a text, comment, or processing instruction node: <code>content</code> of <code>$target</code> is set to <code>$string-value</code>.</p></item><item><p>If  <code>$target</code> is a text node that has a parent, <code>upd:removeType(parent($target))</code> is invoked.</p></item></olist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-replace-element-content"><head>upd:replaceElementContent</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:replaceElementContent(
   $target as element(),
   $text as text()?)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Replaces the existing children of the element node <code>$target</code> by the optional text node <code>$text</code>. The attributes of <code>$target</code> are not affected.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p>None.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>For each node <code>$C</code> that is a child of <code>$target</code>, the <code>parent</code> property of <code>$C</code> is set to empty.</p></item><item><p>The <code>parent</code> property of <code>$text</code> is set to <code>$target</code>.</p></item><item><p>Effects on <code>$target</code>:</p><olist><item><p><code>children</code> is set to consist exclusively of <code>$text</code>. If <code>$text</code> is an empty sequence, then <code>$target</code> has no children.</p></item><item><p><code>typed-value</code> and <code>string-value</code> are set to the <code>content</code> property of <code>$text</code>. If <code>$text</code> is an empty sequence, then <code>typed-value</code> is an empty sequence and <code>string-value</code> is an empty string.</p></item><item><p><code>upd:removeType($target)</code> is invoked.</p></item></olist></item></olist></def></gitem>
						
						
					</glist></div3><div3 id="id-upd-rename"><head>upd:rename</head><glist>
						<gitem><label>Parameters</label><def><eg xml:space="preserve">upd:rename(
   $target as node(),
   $newName as xs:QName)</eg></def></gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Changes the node-name of <code>$target</code> to <code>$newName</code>.</p>
							</def>
						</gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$target</code> must be an element, attribute, or processing instruction node.</p>
							</def>
						</gitem><gitem><label>Semantics</label><def><olist><item><p>If <code>$target</code> is an element node:</p><olist><item><p><code>node-name</code> of <code>$target</code> is set to <code>$newName</code>.</p></item><item><p><code>upd:removeType($target)</code> is invoked.</p> </item><item><p>If  <code>$newName</code> has an <termref def="dt-implied-namespace-binding">implied namespace binding</termref> that <termref def="dt-conflict">conflicts</termref> with an existing namespace binding in the <code>namespaces</code> property of <code>$target</code>, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item><item><p>The <code>namespaces</code> property of <code>$target</code> is modified to include a namespace binding derived from <code>$newName</code>, if this binding did not already exist.</p></item></olist></item><item><p>If <code>$target</code> is an attribute node:</p><olist><item><p><code>node-name</code> of <code>$target</code> is set to <code>$newName</code>.</p></item><item><p><code>upd:removeType($target)</code> is invoked.</p> </item><item><p>If <code>$newName</code> is <code>xml:id</code>, the <code>is-id</code> property of <code>$target</code> is set to <code>true</code>.</p></item><item><p>If <code>$target</code> has a parent, and <code>$newName</code> has an <termref def="dt-implied-namespace-binding">implied namespace binding</termref> that <termref def="dt-conflict">conflicts</termref> with a namespace binding in the <code>namespaces</code> property of <code>parent($target)</code>, a dynamic error is raised <errorref code="0024" class="DY"/>.</p></item><item><p>If <code>$target</code> has a parent, the <code>namespaces</code> property of <code>parent($target)</code> is modified to include a namespace binding derived from <code>$newName</code>, if this binding did not already exist.</p></item></olist></item><item><p>If <code>$target</code> is a processing instruction node, its <code>target</code> property is set to the local part of  <code>$newName</code>.</p></item></olist><note><p>At the end of a <termref def="dt-snapshot">snapshot</termref>, if multiple attribute nodes with the same parent have the same qualified name, an error will be raised by <code>upd:applyUpdates</code>.</p></note></def></gitem>
						
						
					</glist></div3></div2><div2 id="id-update-routines"><head>Update Routines</head><div3 id="id-upd-merge-updates"><head>upd:mergeUpdates</head><glist><gitem><label>Parameters</label>
<def><eg xml:space="preserve">upd:mergeUpdates(
   $pul1 as pending-update-list,
   $pul2 as pending-update-list)</eg>  
</def>
						</gitem>
						<gitem>
							<label>Summary</label>
							<def>
								<p>Merges two <termref def="dt-pending-update-list">pending update lists</termref>.</p>
							</def>
						</gitem><gitem>
							<label>Constraints</label>
							<def>
								<p>None.</p>
							</def>
						</gitem><gitem>
							<label>Semantics</label>
							<def>
								<olist><item><p>The two <termref def="dt-pending-update-list">pending update lists</termref> are merged and a single pending update list containing all the update primitives from both lists is returned.</p></item><item><p>Optionally, <code>upd:mergeUpdates</code> may raise a dynamic error if any of the following conditions are detected:</p><olist><item><p>Two or more <code>upd:rename</code> primitives on the merged list have the same target node <errorref code="0015" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceNode</code> primitives on the merged list have the same target node <errorref code="0016" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceValue</code> primitives on the merged list have the same target node <errorref code="0017" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceElementContent</code> primitives on the merged list have the same target node <errorref code="0017" class="DY"/>.</p></item></olist></item></olist>



	



							</def>
						</gitem></glist></div3><div3 id="id-upd-apply-updates"><head>upd:applyUpdates</head><glist><gitem><label>Parameters</label><def><eg xml:space="preserve">upd:applyUpdates(
   $pul as pending-update-list)</eg></def></gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p>None.</p>
							</def>
						</gitem><gitem>
							<label>Summary</label>
							<def>
								<p>This routine ends a <termref def="dt-snapshot">snapshot</termref> by making effective  the semantics of all the update primitives on a <termref def="dt-pending-update-list">pending update list</termref> and by revalidating the resulting <termref def="dt-xdm-instance">XDM instance</termref>.</p>
							</def>
						</gitem><gitem>
							<label>Semantics</label>
							<def>
								
							<olist><item><p>Checks the update primitives on <code>$pul</code> for compatibility. Raises a dynamic error if any of the following conditions are detected:</p><olist><item><p>Two or more <code>upd:rename</code> primitives on <code>$pul</code> have the same target node <errorref code="0015" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceNode</code> primitives on <code>$pul</code> have the same target node <errorref code="0016" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceValue</code> primitives on <code>$pul</code> have the same target node <errorref code="0017" class="DY"/>.</p></item><item><p>Two or more <code>upd:replaceElementContent</code> primitives on <code>$pul</code> have the same target node <errorref code="0017" class="DY"/>.</p></item></olist></item><item><p>The semantics of all the <termref def="dt-update-primitive">update primitives</termref> on <code>$pul</code> are made effective, in the following order:</p><olist><item><p>First, all <code>upd:insertInto</code>, <code>upd:insertAttributes</code>, <code>upd:replaceValue</code>, and <code>upd:rename</code>  primitives are applied.</p></item><item><p>Next, all <code>upd:insertBefore</code>, <code>upd:insertAfter</code>, <code>upd:insertIntoAsFirst</code>, and <code>upd:insertIntoAsLast</code> primitives are applied.</p></item><item><p>Next, all <code>upd:replaceNode</code> primitives are applied.</p></item><item><p>Next, all <code>upd:replaceElementContent</code> primitives are applied.</p></item><item><p>Finally, all <code>upd:delete</code> primitives are applied.</p></item></olist></item><item><p>If, as a net result of the above steps, the <code>children</code> property of some node contains adjacent text nodes, these adjacent text nodes are merged into a single text node. The string-value of the resulting text node is the concatenated string-values of the adjacent text nodes, with no intervening space added. The <termref def="dt-node-identity">node identity</termref> of the resulting text node is implementation-dependent.</p></item><item><p>If, as a net result of the above steps, the <code>children</code> property of some node contains an empty text node, that empty text node is deleted from the <code>children</code> property.</p></item><item><p>For each document or element node <code>$top</code> that was <termref def="dt-mark">marked for revalidation</termref> by one of the earlier steps, <code>upd:revalidate($top)</code> is invoked.</p></item><item><p>If the resulting <termref def="dt-xdm-instance">XDM instance</termref> violates any constraint specified in <bibref ref="datamodel"/>, a dynamic error is raised <errorref code="0021" class="DY"/>.</p><note><p>For example, a data model constraint violation might occur if multiple attributes with the same parent have the same qualified name (see <xspecref spec="DM" ref="ElementNodeOverview"/>.)</p></note></item><item><p>The  <code>upd:applyUpdates</code> operation is atomic with respect to the data model. In other words, if <code>upd:applyUpdates</code> terminates normally, the resulting <termref def="dt-xdm-instance">XDM instance</termref> reflects the result of all update primitives; but if <code>upd:applyUpdates</code> raises an error, the resulting <termref def="dt-xdm-instance">XDM instance</termref>  reflects no changes. Atomicity is guaranteed only with respect to operations on XDM instances, 
and only with respect to error conditions specified in this document.</p><note><p>The results of implementation-dependent error conditions such as exceeding 
resource limits are beyond the scope of this specification.</p></note></item><item><p>Propagation
of XDM changes to an underlying persistent store is beyond the
scope of this specification. For example, the effect on persistent storage of deleting a node that has no parent is beyond the scope of this specification.</p></item></olist></def>
						</gitem></glist></div3><div3 id="id-upd-revalidate"><head>upd:revalidate</head><glist><gitem><label>Parameters</label><def><eg xml:space="preserve">upd:revalidate(
   $top as node())</eg></def></gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$top</code> must be a document node or an element node.</p>
							</def>
						</gitem><gitem>
							<label>Summary</label>
							<def>
								<p>Schema validation is applied to the subtree rooted at <code>$top</code> in order to recover the types of updated nodes while preserving their <termref def="dt-node-identity">node identities</termref>.</p>
							</def>
						</gitem><gitem>
							<label>Semantics</label>
							<def>
								
							<p>If <termref def="dt-revalidation-mode">revalidation mode</termref> in the static context is <code>skip</code>, <code>upd:revalidate</code> performs no action. Otherwise:</p><olist><item><p>If <termref def="dt-revalidation-mode">revalidation mode</termref> in the static context is <code>lax</code>, define <code>$topV</code> as the result of the XQuery expression <code>validate lax {$top}</code>. If <termref def="dt-revalidation-mode">revalidation mode</termref> is <code>strict</code>, define <code>$topV</code> as the result of the XQuery expression <code>validate strict {$top}</code>. During computation of <code>$topV</code>, it is necessary to maintain a mapping between each node in <code>$topV</code> and the corresponding node (if any) in the subtree rooted at <code>$top</code> (this mapping is maintained in an implementation-dependent way.)</p><note><ulist><item><p>This step may raise an error <xerrorref spec="XQ" code="0027" class="DY"/> if <code>$top</code> is found to be invalid.</p></item><item><p>Some of the nodes in <code>$topV</code> (for example, default attributes generated by the validation process) may have no corresponding nodes in <code>$top</code>.</p></item></ulist></note></item><item><p>For each node <code>$nV</code> in <code>$topV</code> that has a corresponding node <code>$n</code> in <code>$top</code>, replace the following properties of <code>$n</code> with the corresponding properties of <code>$nV</code>: <code>type-name</code>, <code>typed-value</code>, <code>string-value</code>, <code>is-id</code>, <code>is-idrefs</code>, <code>namespace-bindings</code>, <code>nilled</code>.</p></item><item><p>For each node <code>$nV</code> in <code>$topV</code> that does not have a corresponding node in <code>$top</code>, insert the node <code>$nV</code> into the subtree rooted at <code>$top</code> as a child or attribute of the node corresponding to the parent of <code>$nV</code>.</p></item><item><p>The result of <code>upd:revalidate</code> is to modify the properties of the nodes rooted at <code>$top</code> and possibly to add some new nodes to this subtree. When the revalidation process is complete, <code>$topV</code> can be discarded.</p></item></olist><note><p>After revalidation, the type annotations of the nodes in the validated subtree are consistent with their content. It is expected that implementations will optimize the revalidation process by taking into account which nodes have been modified since they were last validated.</p></note></def>
						</gitem></glist></div3><div3 id="id-upd-remove-type"><head>upd:removeType</head><glist><gitem><label>Parameters</label><def><eg xml:space="preserve">upd:removeType(
   $N as node())</eg></def></gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$N</code> must be an element or attribute node</p>
							</def>
						</gitem><gitem>
							<label>Summary</label>
							<def>
								<p> This routine is applied to a node whose name or content has been modified, in order to remove specific type information from the node and its ancestors, pending revalidation.</p>
							</def>
						</gitem><gitem>
							<label>Semantics</label>
							<def>
								
							<olist><item><p>If <code>$N</code> is an element node, its properties are changed as follows:</p><olist><item><p> If <code>type-name</code> is not equal to <code>xs:untyped</code>, then</p><p><olist><item><p><code>type-name</code> is set to <code>xs:anyType</code></p></item><item><p>If the parent of <code>N</code> is an element node, then <code>upd:removeType(parent($N))</code> is invoked.</p></item></olist> </p></item><item><p><code>string-value</code> is set equal to the concatenated contents of the text node descendants, in document order.</p></item><item><p><code>typed-value</code> is set equal to the <code>string-value</code> property, as an instance of <code>xs:untypedAtomic</code>.</p><note><p>The <termref def="dt-data-model">data model</termref> allows some flexibility to implementations regarding whether <code>string-value</code> and/or <code>typed-value</code> are stored or computed dynamically.</p></note></item><item><p><code>nilled</code>, <code>is-id</code>, and <code>is-idrefs</code> are set to <code>false</code>.</p></item></olist></item><item><p>If <code>$N</code> is an attribute node, its properties are changed as follows:</p><olist><item><p><code>type-name</code> is set to <code>xs:untypedAtomic</code>.</p> </item><item><p><code>typed-value</code> is set equal to the <code>string-value</code> property, as an instance of <code>xs:untypedAtomic</code>.</p></item><item><p> <code>is-id</code> and <code>is-idrefs</code> are set to <code>false</code>.</p></item><item><p>If <code>$N</code> has a parent, <code>upd:removeType(parent($N))</code> is invoked.</p></item></olist></item><item><p>The topmost ancestor of <code>$N</code> is <termref def="dt-mark">marked</termref> for revalidation.</p><p><termdef term="mark" id="dt-mark">To <term>mark</term> a node means to identify the node as participating in a later operation.</termdef> Marking of nodes is accomplished in an implementation-dependent way--for example, an implementation might maintain a list of marked nodes.</p></item></olist></def>
						</gitem></glist></div3><div3 id="id-upd-set-to-untyped"><head>upd:setToUntyped</head><glist><gitem><label>Parameters</label><def><eg xml:space="preserve">upd:setToUntyped(
   $N as node())</eg></def></gitem>
						<gitem>
							<label>Constraints</label>
							<def>
								<p><code>$N</code> must be an element or attribute node</p>
							</def>
						</gitem><gitem>
							<label>Summary</label>
							<def>
								<p> This routine is applied to a node that has been inserted into an untyped context, which requires that the node and its descendants be untyped as well.</p>
							</def>
						</gitem><gitem>
							<label>Semantics</label>
							<def>
								
							<olist><item><p>If <code>$N</code> is an element node, its properties are changed as follows:</p><olist><item><p><code>type-name</code> is set to <code>xs:untyped</code>.</p></item><item><p><code>typed-value</code> is set equal to the <code>string-value</code> property, as an instance of <code>xs:untypedAtomic</code>.</p><note><p>The <termref def="dt-data-model">data model</termref> allows some flexibility to implementations regarding whether <code>string-value</code> and/or <code>typed-value</code> are stored or computed dynamically.</p></note></item><item><p><code>nilled</code>, <code>is-id</code>, and <code>is-idrefs</code> are set to <code>false</code>.</p></item><item><p><code>upd:setToUntyped()</code> is invoked on the attributes and child element nodes of <code>$N</code>.</p></item></olist></item><item><p>If <code>$N</code> is an attribute node, its properties are changed as follows:</p><olist><item><p><code>type-name</code> is set to <code>xs:untypedAtomic</code>.</p> </item><item><p><code>typed-value</code> is set equal to the <code>string-value</code> property, as an instance of <code>xs:untypedAtomic</code>.</p></item><item><p> <code>is-id</code> and <code>is-idrefs</code> are set to <code>false</code>.</p></item></olist></item></olist></def>
						</gitem></glist></div3></div2></div1>


<div1 id="id-update-static">
<head>Static Typing Feature</head>

<p>XQuery 1.0 includes an optional static typing feature. This section
describes the static typing feature for the
XQuery Update Facility 1.0. Implementations of this specification that support
optional static typing must implement the following static typing
rules, along with the XQuery 1.0 static typing rules defined in
<bibref ref="XQueryFormalSemantics"/>.</p>

<div2 id="id-update-overview"><head>Overview and notations</head>

<p>The concepts and notations used in this specification are the same
as those used to define the static typing feature for XQuery 1.0. For
convenience, we recall here some of the main concepts and
notations. We refer the reader to <bibref ref="XQueryFormalSemantics"/> for a more detailed introduction.</p>

<p>The static typing feature for the XQuery Update Facility 1.0 is defined based on a
processing model similar to the one of XQuery 1.0. We first define the
XQuery Update Facility 1.0 <emph>Core</emph>, as a subset of the XQuery Update Facility 1.0. Then for
each kind of updating expression, we give the normalization rules from
the XQuery Update Facility 1.0 into that Core, as well as the inference rules that
compute the type for each Core expression.</p>

    <smnotation>
      <p id="jd_map_expr">Normalization rules map expressions in the
      XQuery Update Facility 1.0 into expressions in the XQuery Update Facility 1.0 Core. They are
      written as follows.</p>

      	<mapping>
      	  <xquery>
      	    <expression>
      	      <map><emph>Expr</emph></map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
      	    </expression>
      	  </xquery>
      	  <core>
      	    <expression>
      	      <emph>CoreExpr</emph>
      	    </expression>
      	  </core>
      	</mapping>
    </smnotation>

    <smnotation>
      <p>The static semantics is specified through a
      <term>judgment</term> which gives for each expression its
      corresponding static type. The meaning of that judgment is
      defined through <term>inference rules</term> as in <bibref ref="XQueryFormalSemantics"/>.</p>

      <p id="jd_has_type">The judgment</p>

      <display>
      <clause>
        <environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
        <expression><emph>Expr</emph> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a></expression>
      </clause>
      </display>

      <p>holds when, in the static environment <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a>, the
      expression <emph>Expr</emph> has type <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a>.</p>
    </smnotation>

    <smnotation>
      <p> The static environment is the same as the one defined in
      <bibref ref="XQueryFormalSemantics"/>, extended with an entry
      corresponding to the revalidation mode, as follows.</p>

      <table summary="" role="figure" cellpadding="5">
      <col width="10%" span="1"/><!-- empty column forces left indentation -->
      <col width="15%" span="1"/>
      <col width="65%" align="justify" span="1"/>
      <col width="10%" span="1"/><!-- empty column forces right indentation -->
      <tbody>
      <tr><td rowspan="1" colspan="1"/>
          <td id="xq_revalidation_mode_env" valign="top" rowspan="1" colspan="1"><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a>.<a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_revalidation_mode_env" class="env">revalidationMode</a></td>
          <td rowspan="1" colspan="1">
            <table>
              <tbody>
                <tr><td rowspan="1" colspan="1">The <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a>.<a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_revalidation_mode_env" class="env">revalidationMode</a> environment
                component designates the <term>revalidation mode</term> in
                the XQuery Update Facility 1.0 static context.</td></tr>

                <tr><td rowspan="1" colspan="1">The <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a>.<a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_revalidation_mode_env" class="env">revalidationMode</a> environment
                component is one of <term>strict</term>,
                <term>lax</term>, or <term>skip</term>.</td></tr>
                </tbody>
            </table>
          </td>
          <td rowspan="1" colspan="1"/>
      </tr>

      </tbody>
      </table>
   </smnotation>
</div2>

<div2 id="id-update-core"><head>Core Grammar</head><p>We extend the
XQuery Core defined in <bibref ref="XQueryFormalSemantics"/> with five
expressions needed to support the XQuery Update Facility 1.0: insert, delete, rename,
replace, and transform.</p>

<scrap headstyle="show"> 
<head/> 
<prod num="23 (Core)" id="doc-core-ExprSingle"><lhs>ExprSingle</lhs><rhs><nt def="prod-core-FLWORExpr">FLWORExpr</nt><br/>|  <nt def="prod-core-QuantifiedExpr">QuantifiedExpr</nt><br/>|  <nt def="prod-core-TypeswitchExpr">TypeswitchExpr</nt><br/>|  <nt def="prod-core-IfExpr">IfExpr</nt><br/>|  <nt def="doc-core-InsertExpr">InsertExpr</nt><br/>|  <nt def="doc-core-DeleteExpr">DeleteExpr</nt><br/>|  <nt def="doc-core-RenameExpr">RenameExpr</nt><br/>|  <nt def="doc-core-ReplaceExpr">ReplaceExpr</nt><br/>|  <nt def="doc-core-TransformExpr">TransformExpr</nt><br/>|  <nt def="prod-core-OrExpr">OrExpr</nt></rhs></prod> 
<prod num="99 (Core)" id="doc-core-InsertExpr"><lhs>InsertExpr</lhs><rhs>"insert"  "nodes"  <nt def="doc-core-SourceExpr">SourceExpr</nt>  <nt def="doc-core-InsertExprTargetChoice">InsertExprTargetChoice</nt>  <nt def="doc-core-TargetExpr">TargetExpr</nt></rhs></prod> 
<prod num="98 (Core)" id="doc-core-InsertExprTargetChoice"><lhs>InsertExprTargetChoice</lhs><rhs>(("as"  ("first"  |  "last"))?  "into")<br/>|  "after"<br/>|  "before"</rhs></prod> 
<prod num="103 (Core)" id="doc-core-SourceExpr"><lhs>SourceExpr</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod> 
<prod num="104 (Core)" id="doc-core-TargetExpr"><lhs>TargetExpr</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod> 
<prod num="100 (Core)" id="doc-core-DeleteExpr"><lhs>DeleteExpr</lhs><rhs>"delete"  "nodes"  <nt def="doc-core-TargetExpr">TargetExpr</nt></rhs></prod> 
<prod num="101 (Core)" id="doc-core-ReplaceExpr"><lhs>ReplaceExpr</lhs><rhs>"replace"  ("value"  "of")?  "node"  <nt def="doc-core-TargetExpr">TargetExpr</nt>  "with"  <nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod> 
<prod num="102 (Core)" id="doc-core-RenameExpr"><lhs>RenameExpr</lhs><rhs>"rename"  "node"  <nt def="doc-core-TargetExpr">TargetExpr</nt>  "as"  <nt def="doc-core-NewNameExpr">NewNameExpr</nt></rhs></prod> 
<prod num="105 (Core)" id="doc-core-NewNameExpr"><lhs>NewNameExpr</lhs><rhs><nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod> 
<prod num="106 (Core)" id="doc-core-TransformExpr"><lhs>TransformExpr</lhs><rhs>"copy"  "$"  <nt def="prod-core-VarName">VarName</nt>  ":="  <nt def="doc-core-ExprSingle">ExprSingle</nt>  (","  "$"  <nt def="prod-core-VarName">VarName</nt>  ":="  <nt def="doc-core-ExprSingle">ExprSingle</nt>)*  "modify"  <nt def="doc-core-ExprSingle">ExprSingle</nt>  "return"  <nt def="doc-core-ExprSingle">ExprSingle</nt></rhs></prod> 
</scrap>

<ulist>
  <item><p id="prod-core-IfExpr">The core grammar production for <code>IfExpr</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
  <item><p id="prod-core-OrExpr">The core grammar production for <code>OrExpr</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
  <item><p id="prod-core-FLWORExpr">The core grammar production for <code>FLWORExpr</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
  <item><p id="prod-core-QuantifiedExpr">The core grammar production for <code>QuantifiedExpr</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
  <item><p id="prod-core-TypeswitchExpr">The core grammar production for <code>TypeswitchExpr</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
  <item><p id="prod-core-VarName">The core grammar production for <code>VarName</code> can be found in <bibref ref="XQueryFormalSemantics"/></p></item>
</ulist>
</div2>


<div2 id="id-update-norm-prolog">
  <head>XQuery Update Facility 1.0 Prolog</head>

  <p>In addition to the normalization rules defined in <bibref ref="XQueryFormalSemantics"/> for the XQuery 1.0 prolog, the
  following normalization rules apply to the revalidation mode
  setter.</p>

  <smnorm>
    <p>Revalidation declarations are left unchanged through
    normalization.</p>

    <mapping>
     <xquery>
      <expression>
       <map><emph>RevalidationDecl</emph></map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_prolog" class="judgment"><subscript>PrologDecl</subscript></a>
      </expression>
     </xquery>
     <core>
      <expression>
        <emph>RevalidationDecl</emph>
      </expression>
     </core>
    </mapping>
  </smnorm>

  <smcontext>
    <p>The revalidation declaration modifies the revalidation mode
    in the static context.</p>

    <infergr>
	<infer>
	  <prejudge>
	      <clause>
              <expression>
                <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript> =
                  <update>
                    <environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a>.<a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_revalidation_mode_env" class="env">revalidationMode</a></environment>
                    <expression>
                    <emph>RevalidationMode</emph></expression>
                  </update>
              </expression>
	      </clause>
	  </prejudge>
	  <postjudge>
	    <multiclause>
	      <clause>
              <environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
              <expression>declare revalidation <emph>RevalidationMode</emph>
              <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_yields_context" class="judgment"><term>=&gt;</term><subscript>stat</subscript></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a><subscript>1</subscript></expression> 
	      </clause>
	    </multiclause>
	  </postjudge>
	</infer>
    </infergr>
  </smcontext>
</div2>

<div2 id="id-update-norm-expr">
  <head>XQuery Update Facility 1.0 Expressions</head>

    <p>In addition to the normalization and static typing rules
    defined in <bibref ref="XQueryFormalSemantics"/> for XQuery 1.0
    expressions, the normalization and static typing rules described in
    this specification apply to the expressions in the XQuery Update Facility 1.0. For
    each expression except the transform expression, the final type is
    the empty type which corresponds to the value being returned. In
    each case, the static typing rules check that every sub-expression
    is well-typed, and that the resulting value is consistent with the
    constraints specific to each expression. For instance, the
    expression used in a delete must return a sequence of nodes.</p>

    <div3 id="id-update-typing-insert">
     <head>Insert</head>

    <smnorm>
      <p>The following normalization rules applies to insert
      expressions. Note that whether the expression is written using
      the 'nodes' or 'node' modifier, the normalized expression always
      uses the 'nodes' modifier.</p>

      <mapping>
       <xquery>
      	<expression>
         <map>insert (node|nodes) <emph>Expr</emph><subscript>1</subscript> <emph>InsertExprTargetChoice</emph> <emph>Expr</emph><subscript>2</subscript></map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </xquery>
       <core>
        <expression>
         insert nodes <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a>(<map><emph>Expr</emph><subscript>1</subscript></map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>)
      	 <emph>InsertExprTargetChoice</emph>
         <map><emph>Expr</emph><subscript>2</subscript></map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_expr" class="judgment"><subscript>Expr</subscript></a>
        </expression>
       </core>
      </mapping>

      <p>Where <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#sec_items_to_nodes"><emph>fs:</emph><code>item-sequence-to-node-sequence</code></a> is the special function used
      for computing element content and is defined in <bibref ref="XQueryFormalSemantics"/>.</p>

    </smnorm>

    <smtype>
      <p>The static typing rule for insert depends on whether it is an
      insert <code>into</code>, or an insert <code>before</code> or
      <code>after</code>.</p>

      <p>In the case of an insert <code>into</code>, the inference
      depends on whether the target node is an element or a document
      node (in which case it does not allow attribute nodes to be
      inserted). This is expressed by the two following rules.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                (attribute *)*, (element * | text | comment | processing-instruction)*</expression>
	        </clause>
	      </multiclause>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_subtype_of" class="judgment"><term>&lt;:</term></a> (element *)?</expression>
	        </clause>
	      </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                        <expression>insert nodes <emph>Expr</emph><subscript>1</subscript> ((as (first | last))? into) <emph>Expr</emph><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <code>empty</code></expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                (element * | text | comment | processing-instruction)*</expression>
	        </clause>
	      </multiclause>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_subtype_of" class="judgment"><term>&lt;:</term></a> <map>document-node()?</map><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_map_sequencetype" class="judgment"><subscript>sequencetype</subscript></a></expression>
	        </clause>
	      </multiclause>
            </prejudge>
            <postjudge>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                        <expression>insert nodes <emph>Expr</emph><subscript>1</subscript> ((as (first | last))?
    	 into) <emph>Expr</emph><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <code>empty</code></expression>
                </clause>
              </multiclause>
            </postjudge>
          </infer>
        </infergr>

        <p>In the case of an insert <code>before</code> or
        <code>after</code>, the type of the target node must be either
        an element, text, comment or processing-instruction node. This
        is expressed by the following rule.</p>

        <infergr>
          <infer>
            <prejudge>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>1</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_subtype_of" class="judgment"><term>&lt;:</term></a>
                (attribute *), (element * | text | comment | processing-instruction)*</expression>
	        </clause>
	      </multiclause>
              <multiclause>
                <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a> </environment>
                  <expression><emph>Expr</emph><subscript>2</subscript> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#jd_has_type" class="judgment"><term>:</term></a> <a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><emph>Type</emph></a><subscript>2</subscript></expression>
                </clause>
              </multiclause>
	      <multiclause>
	        <clause><environment><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#xq_stat_env_def" class="env">statEnv</a></environment>
                <expression><a href="http://www.w3.org/TR/2007/REC-xquery-semantics-20070123/#doc-fs-Type"><em