This discussion is archived
2 Replies Latest reply: Oct 31, 2011 9:16 AM by user299932 RSS

Assign default namespace to XML fragment using XQuery

user299932 Newbie
Currently Being Moderated
Hi everybody!

I need to add a default namespace declaration to a XML fragment using XQuery. I wrote following statement to assign fragment to $body:

<soap-env:Body>{
fn-bea:inlinedXML($content/text())}
</soap-env:Body>

The problem is "$content/text()" has no namespace declaration so I need to assign a default namespace (xmlns="") to it in order to apply some XQuery transformations to its content.

I know this can be easily done with a XSLT but I would like use XQuery instead.

Could anyone tell me how I could perform this task?

Thank you in advance,

Daniel.
  • 1. Re: Assign default namespace to XML fragment using XQuery
    odie_63 Guru
    Currently Being Moderated
    Hi,

    I guess you have two options here :

    1) Adding the namespace directly on the textual XML contained in $content/text(), then call fn-bea:inlinedXML.
    Assuming BEA's XQuery implementation supports fn:replace function (according to the doc it does) - or an equivalent :
    fn-bea:inlinedXML( fn:replace($content/text(), '^<([^>]+)>', '<$1 xmlns="default_ns">') )
    For example, if $content/text() is
    <root id="1"><test>TEST!</test></root>
    The result will be :
    <root id="1" xmlns="default_ns"><test>TEST!</test></root>
    2) Adding the namespace on the generated instance (i.e. after calling fn-bea:inlinedXML).

    That's a little bit more complex and will require something like this :
    http://www.xqueryfunctions.com/xq/functx_change-element-ns-deep.html
  • 2. Re: Assign default namespace to XML fragment using XQuery
    user299932 Newbie
    Currently Being Moderated
    Thank you very much odie. First option worked like a charm!

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points