[XQuery Talk Mailing List Archive Home] [By Date] [By Thread] [By Subject] [By Author] [Recent Entries] [Reply To This Message]

adding or modifing an attribute

Michael Kay mike at saxonica.com
Fri Jun 6 09:46:35 PDT 2008

  adding or modifing an attribute
In the absence of XQuery Updates, this kind of problem is rather tedious to
solve in XQuery, at least in comparison with the XSLT solution. 

You basically need to write a recursive function that is called to process
each node in the tree, and that calls itself to process its children.
Something like this:

declare function local:copy-and-change($nodes as node()*) as node()* {
for $in in $nodes return
  typeswitch ($in) {
  case element(name):
    <name> {
         @* except @average,
         attribute average {90},
    } </name>
  case element():
    element {node-name($in)} {


Note that this code will drop any namespaces that aren't used in element and
attribute names - don't use it if your document has namespace-sensitive
content such as xsi:type attributes. (I don't know of any general answer to
that problem.)

Michael Kay

> -----Original Message-----
> From: http://x-query.com/mailman/listinfo/talk 
> [mailto:http://x-query.com/mailman/listinfo/talk] On Behalf Of Sam Carleton
> Sent: 04 June 2008 17:03
> To: http://x-query.com/mailman/listinfo/talk
> Subject:  adding or modifing an attribute
> I have worked with DOM, SAX, XSLT, and XPath in the past and 
> am now working with a framework that has XQuery.  The task at 
> hand is very simple, change (or add if not present) an 
> attribute to one element in an XML document:
> Example: If the name element has an average attribute, change 
> it to 93, otherwise create it and set it to 93
> input:
> <root>
>  <students>
>    <name value="scott"/>
>  </students>
> </root>
> or
> <root>
>  <students>
>    <name value="scott" average="90"/>
>  </students>
> </root>
> output:
> <root>
>  <students>
>    <name value="scott" average="93"/>
>  </students>
> </root>
> How might I do this with XQuery?  The framework I am using is 
> Trolltech's Qt which is a C++ framework.  I am under the 
> impression I am going to have to load the XML document into 
> memory, execute the XQuery query on it that will result in a 
> new XML document that I will then save over the original XML 
> file, is that correct?  I can figure out the Qt stuff, the 
> real question is how would I put together a query to make 
> this change in XQuery?
> Sam
> _______________________________________________
> http://x-query.com/mailman/listinfo/talk
> http://x-query.com/mailman/listinfo/talk


Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
First Name
Last Name
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.