Rename expression

A rename expression replaces the name property of a data model node with a new QName.

Syntax

Read syntax diagramSkip visual syntax diagramdo renameTargetExpressionasNewNameExpression
do rename
The keywords that begin a rename expression.
TargetExpression
An XQuery expression that is not an updating expression. The result of TargetExpression must be a single element, attribute, or processing instruction node. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.

The rename expression affects only the TargetExpression node. If the TargetExpression node is an element node, the expression has no effect on any attributes or children of the target node. If the TargetExpression node is an attribute node, the expression has no effect on other attributes or descendants of the parent node of the TargetExpression node.

as
The keyword that begins the new name expression.
NewNameExpression
An XQuery expression that is not an updating expression. The result of NewNameExpression must be a value of type xs:string, xs:QName, xs:untypedAtomic, or a node from which such a value can be extracted by the atomization process. If the expression includes a top-level comma operator, then the expression must be enclosed in parentheses.

The resulting value is converted to a QName, resolving its namespace prefix, if any, according to the statically known namespaces. The result is either an error or an expanded QName. The expanded QName replaces the name of the TargetExpression node.

If the new QName contains the same prefix but a different URI than an in-scope namespace of the TargetExpression node, Db2 XQuery returns an error.

Examples

The following examples use the CUSTOMER table from the Db2 SAMPLE database. In the CUSTOMER table, the XML column INFO contains customer address and phone information.

In the following example, the copy clause of the transform expression creates a copy of the XML document from column INFO. The rename expression changes the name property of the addr element to shipto:

xquery 
transform
copy $mycust := db2-fn:sqlquery('select info from customer where cid = 1000')
modify
  do rename $mycust/customerinfo/addr as "shipto"
return $mycust

When run against the SAMPLE database, the expression returns the following result:

<customerinfo Cid="1000">
  <name>Kathy Smith</name>
  <shipto country="Canada">
    <street>5 Rosewood</street>
    <city>Toronto</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>M6W 1E6</pcode-zip>
  </shipto>
  <phone type="work">416-555-1358</phone>
</customerinfo>

In the following example, the modify clause of the transform expression contains a FLWOR expression and a rename expression that changes the name property of all instances of the element phone to phonenumber:

xquery 
transform 
copy $mycust :=  db2-fn:sqlquery('select info from customer where cid = 1003')
modify 
  for $phone in $mycust/customerinfo/phone
  return
    do rename $phone as "phonenumber"
return $mycust

When run against the SAMPLE database, the expression returns the following result:

<customerinfo Cid="1003">
  <name>Robert Shoemaker</name>
  <addr country="Canada">
    <street>1596 Baseline</street>
    <city>Aurora</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>N8X 7F8</pcode-zip>
  </addr>
  <phonenumber type="work">905-555-7258</phonenumber>
  <phonenumber type="home">416-555-2937</phonenumber>
  <phonenumber type="cell">905-555-8743</phonenumber>
  <phonenumber type="cottage">613-555-3278</phonenumber>
</customerinfo>

In the following example, the rename expression changes the name of the addr element's attribute from country to geography:

xquery 
transform
copy $mycust := db2-fn:sqlquery('select info from customer where cid = 1000') 
modify 
  do rename $mycust/customerinfo/addr/@country as "geography"
return $mycust

When run against the SAMPLE database, the expression returns the following result:

<customerinfo Cid="1000">
  <name>Kathy Smith</name>
  <addr geography="Canada">
    <street>5 Rosewood</street>
    <city>Toronto</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>M6W 1E6</pcode-zip>
  </addr>
  <phone type="work">416-555-1358</phone>
</customerinfo>

The following example uses the rename expression and the fn:QName function to add the namespace prefix other to the names of the customer's non-work phone number elements and attributes. The prefix other is bound to the URI http://otherphone.com:

xquery
transform 
copy $mycust := db2-fn:sqlquery('select info from customer where cid = 1004') 
modify 
  for $elem in $mycust/customerinfo/phone[@type != "work"]
  let $elemLocalName := fn:local-name($elem) 
  let $newElemQName := fn:QName("http://otherphone.com", fn:concat("other:", 
     $elemLocalName))
  return
    ( do rename $elem as $newElemQName,
    for $a in $elem/@* let $attrlocalname := fn:local-name($a)
    let $newAttrName := fn:QName("http://otherphone.com", fn:concat("other:", 
       $attrlocalname))
    return 
      do rename $a as $newAttrName )
return $mycust

When run against the SAMPLE database, the expression returns the following result:

<customerinfo Cid="1004">
  <name>Matt Foreman</name>
  <addr country="Canada">
    <street>1596 Baseline</street>
    <city>Toronto</city>
    <prov-state>Ontario</prov-state>
    <pcode-zip>M3Z 5H9</pcode-zip>
  </addr>
  <phone type="work">905-555-4789</phone>
  <other:phone xmlns:other="http://otherphone.com" other:type="home">
    416-555-3376</other:phone>
  <assistant>
    <name>Gopher Runner</name>
    <phone type="home">416-555-3426</phone>
  </assistant>
</customerinfo>

If you use the following expression in the transform expression's return clause, the phone element nodes that use the default element namespace appear as child nodes of the primary node, and the other:phone element node appears as the child node of the secondary node.

<phonenumbers xmlns:other="http://otherphone.com">
  <primary>
    { $mycust//phone }
  </primary>
  <secondary>
    { $mycust//other:phone }
  </secondary>
</phonenumbers>

When run against the SAMPLE database, the transform expression returns the following result:

<phonenumbers xmlns:other="http://otherphone.com"
  <primary>
    <phone type="work">905-555-4789</phone>
    <phone type="home">416-555-3426</phone>
  </primary>
  <secondary>
    <other:phone other:type="home">416-555-3376</other:phone>
  </secondary>
</phonenumbers>