[xquery-talk] Re: replacing a node in in-memory XML

Hans-Juergen Rennau hrennau at yahoo.de
Tue Nov 6 22:13:39 PST 2007

Hello Wolfgang,

let me add a remark about the situation when the nodes to be modified cannot be identified by type *ALONE*. For example, the document might contain many community groups, and the services element is to be replaced only in the groups (2, 31, 32, 40). In such situations, I suggest to combine the "typeswitch recursion pattern" with a "node marking pattern": a) before starting the recursion, "mark" the modification targets by collecting them in a variable, b) within the relevant typeswitch branch use a node identity-based if/else. This approach scales rather well when things get more complicated (several element types are to be partially and heterogeneously modified).

declare variable $targetNodes_services := 
   ($doc//communitygroup)[position() = (2, 31, 32, 40)]/group/services

typeswitch ($node)
   case $elem as element(services) return
      if ($elem intersect $targetNodes_services) then <services>...</services>

		         element {node-name($elem)} {$elem/@*, $elem/node()/local:replace(.)}

The approach helped me when I had to modify WordML documents, highly repetitive as they are in terms of element names!

With kind regards,

       __________________________________ Ihr erstes Fernweh? Wo gibt es den schönsten Strand? www.yahoo.de/clever

More information about the talk mailing list