xmlxml-parsingxquerysaxonflwor

How to rename node element names using a FLWOR XQuery?


How can I rename all foo tags as baz using an XQuery as below?

FLWOR:

declare namespace map = "http://www.w3.org/2005/xpath-functions/map";
declare namespace array = "http://www.w3.org/2005/xpath-functions/array";

declare namespace output = "http://www.w3.org/2010/xslt-xquery-serialization";

for $x in /root

return rename node $x/root/foo as "baz"

Sample doc:

  <root>
    <foo>bar</foo>
  </root>

output I'm looking for:

  <root>
    <baz>bar</baz>
  </root>

see also:

http://xqueryfiddle.liberty-development.net/nc4P6y8

and:

removing an element from xml using saxon xquery

which uses something not entirely dissimilar..


Solution

  • You should be able to use

    for $foo in doc('input.xml')//foo
    return rename node $foo as "baz"
    

    assuming you have support for XQuery update (like in Saxon 9 or 10 EE or in BaseX).