XML:DB XUpdate Use Cases

Author: Kimbro Staken

http://www.xmldatabases.org/

This document outlines a basic set of uses cases for the XUpdate specification from the XML:DB Initiative.

XUpdate is a simple XML update language. You can use it to modify XML content by simply declaring what changes should be made in an XML syntax. This document intends to provide a solid set of XUpdate examples showing how the language can be used. The initial set of use cases covers the basic functionality and it would be great to see this list grow over time. If you have a use case or example that you would like to contribute, please contact me kstaken@xmldatabases.org.

All use cases operate on the following XML document.

<addresses>
   
   <address id="1">
      <!--This is the users name-->
      <name>
         <first>John</first>
         <last>Smith</last>
      </name>      
      <city>Houston</city>
      <state>Texas</state>
      <country>United States</country>
      <phone type="home">333-300-0300</phone>
      <phone type="work">333-500-9080</phone>
      <note><![CDATA[This is a new user]]></note>
   </address>
   
</addresses>

All use cases were tested against dbXML 1.0 beta 4, but should also work with Lexus. dbXML uses Lexus to provide XUpdate support.

Use Case Index

  1. Insert Element Before
  2. Insert Element After
  3. Append Element
  4. Insert attribute
  5. Insert Text Content
  6. Insert XML Block
  7. Insert a Processing Instruction
  8. Insert a Comment
  9. Insert CDATA Content
  10. Update Element
  11. Update Attribute
  12. Update Comment
  13. Update CDATA Content
  14. Rename Element
  15. Rename Attribute
  16. Delete Element
  17. Delete Attribute
  18. Delete Text Content of an Element
  19. Delete Comment
  20. Delete CDATA Content
  21. Copying a Node
  22. Moving a Node

1. Insert Element Before

Add a middle name element before the last name element

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">
   
   <xupdate:insert-before select="/addresses/address[@id = 1]/name/last" >   
      <xupdate:element name="middle">Lennox</xupdate:element>      
   </xupdate:insert-before>
</xupdate:modifications>

2. Insert Element After

Add a cell phone element after the home phone element

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:insert-after select="/addresses/address[@id = 1]/phone[@type='home']" >   
      <xupdate:element name="phone"><xupdate:attribute name="type">cell</xupdate:attribute>490-494-4904</xupdate:element>      
   </xupdate:insert-after>
</xupdate:modifications>

3. Append Element

Append a zip code element to the address record.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:append select="/addresses/address[@id = 1]" >   
      <xupdate:element name="zip">90200</xupdate:element>      
   </xupdate:append>
</xupdate:modifications>

4. Insert attribute

Add an extension attribute to the work phone element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:append select="/addresses/address[@id = 1]/phone[@type='work']" >   
      <xupdate:attribute name="extension">223</xupdate:attribute>      
   </xupdate:append>
</xupdate:modifications>

5. Insert Text Content

Append the "of America" clause to the country element

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:append select="/addresses/address[@id = 1]/country" >
      <xupdate:text> of America</xupdate:text>
   </xupdate:append>
</xupdate:modifications>

6. Insert XML Block

Add a new address record to the top level addresses element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:append select="/addresses" >
      <xupdate:element name="address">
         <xupdate:attribute name="id">2</xupdate:attribute>
         <name>
            <first>Susan</first>
            <last>Long</last>
         </name>
         <city>Tucson</city>
         <state>Arizona</state>
         <country>United States</country>
         <phone type="home">430-304-3040</phone>
      </xupdate:element>      
   </xupdate:append>
</xupdate:modifications>

7. Insert a Processing Instruction

Add a simple XML processing instruction to the root level of the document.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:insert-before select="/addresses">
      <xupdate:processing-instruction name="cocoon-process">type="xsp"</xupdate:processing-instruction>
   </xupdate:insert-before>
</xupdate:modifications>

8. Insert a Comment

Add another comment about the name element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:insert-before select="/addresses/address[@id = 1]/name">
      <xupdate:comment>Another comment about the name</xupdate:comment>
   </xupdate:insert-before>
</xupdate:modifications>

9. Insert CDATA Content

Add a notes element with CDATA content with some HTML tags.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:append select="/addresses/address[@id = 1]" >
      <xupdate:element name="note"><xupdate:cdata><![CDATA[A simple <b>note</b>]]></xupdate:cdata></xupdate:element>
   </xupdate:append>
</xupdate:modifications>

10. Update Element

Change the first name of address with id = 1 to be Johnathan.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:update select="/addresses/address[@id = 1]/name/first">Johnathan</xupdate:update>
</xupdate:modifications>

11. Update Attribute

Change the type of the phone number 333-300-0300 to be a cell.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:update select="/addresses/address[@id = 1]/phone[.='333-300-0300']/@type" >cell</xupdate:update>
</xupdate:modifications>

12. Update Comment

Change the comment right before the name element that starts with "This".

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:update select="/addresses/address[@id = 1]/comment()[starts-with(., 'This')]" >This user has a name.</xupdate:update>
</xupdate:modifications>

13. Update CDATA Content

Change the CDATA content of the note element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:update select="/addresses/address[@id = 1]/note/text()" ><![CDATA[Just some <i>modified</i> content]]></xupdate:update>
</xupdate:modifications>


14. Rename Element

Rename the note element to comment.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:rename select="/addresses/address[@id = 1]/note" >comment</xupdate:rename>
</xupdate:modifications>

15. Rename Attribute

Rename the type attribute on all phone elements to location.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:rename select="/addresses/address[@id = 1]/phone/@type">location</xupdate:rename>   
</xupdate:modifications>

16. Delete Element

Remove all phone elements.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:remove select="/addresses/address[@id = 1]/phone"/>
</xupdate:modifications>


17. Delete Attribute

Delete all type attributes on phone elements.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:remove select="/addresses/address[@id = 1]/phone/@type" />

</xupdate:modifications>

18. Delete Text Content of an Element

Clear the content of the country element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:remove select="/addresses/address[@id = 1]/country/text()" />
</xupdate:modifications>

19. Delete Comment

Remove the first comment before the name element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

    <xupdate:remove select="/addresses/address[@id = 1]/comment()[1]" />

</xupdate:modifications>

20. Delete CDATA Content

Remove the CDATA content of the note element.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:remove select="/addresses/address[@id = 1]/note/text()" />

</xupdate:modifications>

21. Copying a Node

Copy the state node and place the copy after the country node.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">
   
   <xupdate:variable name="state" select="/addresses/address[@id = 1]/state"/>
   <xupdate:insert-after select="/addresses/address[@id = 1]/country">
      <xupdate:value-of select="$state"/>
   </xupdate:insert-after>
</xupdate:modifications>

22. Moving a Node

Move the country node before the state node.

<xupdate:modifications version="1.0"
       xmlns:xupdate="http://www.xmldb.org/xupdate">

   <xupdate:variable name="country" select="/addresses/address[@id = 1]/country"/>
   <xupdate:remove select="/addresses/address[@id = 1]/country"/>
   <xupdate:insert-before select="/addresses/address[@id = 1]/state">
      <xupdate:value-of select="$country"/>
   </xupdate:insert-before>   
   
</xupdate:modifications>