0
votes

I have an xquery file that transforms from a.xml to b.xml. Below is an example of my x-query:

<TestMessage>
    <Header>
      <MessageType>
           {for $x in doc("input.xml")//Test/NewHeader return 
        if ($x/MessageType/text() = "FIRST") then "FirstMessageType"
        else "SecondMessageType"
    }   
      </MessageType>
    </Header>
 </TestMessage>

This works fine and populates my MessageType properly. But I need to add a namespace as an attribute to the TestMessage element. So, when I do,

<TestMessage xmlns="http://www.testsource.com/TestMessage">
    <Header>
      <MessageType>
           {for $x in doc("input.xml")//Test/NewHeader return 
        if ($x/MessageType/text() = "FIRST") then "FirstMessageType"
        else "SecondMessageType"
    }   
      </MessageType>
    </Header>
 </TestMessage>

The xquery result has an empty MessageType element. I haven't changed anything except add the namespace in the x-query document. But I don't understand why this gives me a wrong resultant xml.

Any help would be greatly appreciated.

thanks a lot!

1

1 Answers

1
votes

When you add a default namespace to the TestMessage element, that namespace is also applied to the XPath you are executing underneath it: doc("input.xml")//Test/NewHeader and ($x/MessageType/text().

If that content doesn't have a namespace, then the easiest thing to do would be to declare a namespace prefix in the XML root: xmlns:test="http://www.testsource.com/TestMessage, and prefix all of the elements: <test:TestMessage xmlns:test="http://www.testsource.com/TestMessage>, <test:Header> and so on.

One way to get around this is to execute the no-namespace XQuery code outside of the default-namespaced XML:

declare function local:get-message-type()
{
  for $x in doc("input.xml")//Test/NewHeader 
  return 
    if ($x/MessageType/text() = "FIRST") 
    then "FirstMessageType"
    else "SecondMessageType"
};

<TestMessage xmlns="http://www.testsource.com/TestMessage">
    <Header>
      <MessageType>
           { local:get-message-type() }   
      </MessageType>
    </Header>
</TestMessage>