Renaming a Node
The
rename
expression allows you to replace the name property for a specific node. Attributes and descendants of the specified node are not affected by therename
operation.Example
The rename-nodes.xq XQuery changes an XML document by renaming
<UserID>
node to<ID>
and saving the result to a new document using thefn:put()
function.for $user in doc("../xml/users.xml")/table/users return rename node $user/UserId as QName("", "ID"), put(doc("../xml/users.xml"), "new_users.xml")Example – Using XQJ
You can execute XQuery programmatically using XQJ. In this example, a Java application is used to execute XQuery that uses the same rename and put XUF expressions to rename nodes in an XML document and create a new XML document with the renamed nodes:
// import the XQJ classes import javax.xml.xquery.*; import com.ddtek.xquery.xqj.DDXQDataSource; import com.ddtek.xquery.xqj.DDXQJDBCConnection; public class XUF { public static void main(String[] args) throws Exception { XQConnection xqconnection = null; XQPreparedExpression xqExpr = null; try { DDXQDataSource dataSource = new DDXQDataSource(); xqconnection = dataSource.getConnection(); // the query String xquery = "for $user in doc('../xml/users.xml')/table/users\n" + "return\n" + "rename node $user/UserId as QName('', 'ID'),\n" + "put(doc('../xml/users.xml'), 'new_users.xml')\n"; xqExpr = xqconnection.prepareExpression(xquery); // execute the query xqExpr.executeQuery(); } finally { if (xqExpr != null) xqExpr.close(); if (xqconnection != null) xqconnection.close(); } } }To learn more about DataDirect XQuery support for XQJ, see XQJ Support.