Subject:XQuery problem with namespaces Author:Roel van der Hoeven Date:28 Feb 2012 03:43 AM
Hi,
I've got a problem to get namespaces working in a mapping I created.
I'll be getting a message which sometimes will contain the namespace ns0, and sometimes without any namespace. I tried to get this very simple QXuery to work with the namespace first, but I keep getting in the error part of the if construction.
We usually do not use namespaces and therefor I'm quite unfamiliar with them and their use. I imagine it could have something to do with the fact that we receive the input as a stream which is set to xs:untyped. If I set that to xs:anyType or xs:string I'll get the error:
Static type error. Types 'element(ns0:responseTypeCode, xs:anyType)*' and 'xs:string' are invalid argument types for binary operator '='.
declare variable $messagePreviousReference as xs:string external;
<TechnicalAcknowledgement ns0="http://bla.com/xsd">
{
if ($input/ns0:technicalResponse/ns0:responseTypeCode = "AK")
then
(
<emt:MessageInterchange
mct:codedAction="8"
mct:interchangeControlReference="{$messagePreviousReference}"/>,
<aty:ResponseMessageWithoutFunctionalGroups>
<aty:MessageResponse
mct:codedAction="8"
mct:messageReferenceNumber="{$messagePreviousReference}"/>
</aty:ResponseMessageWithoutFunctionalGroups>
)
else
if ($input/ns0:technicalResponse/ns0:responseTypeCode = "NA")
then
(
<emt:MessageInterchange
mct:codedAction="4"
mct:interchangeControlReference="{$messagePreviousReference}"/>,
<aty:ResponseMessageWithoutFunctionalGroups>
<aty:MessageResponse
mct:codedAction="4"
mct:messageReferenceNumber="{$messagePreviousReference}"/>
</aty:ResponseMessageWithoutFunctionalGroups>
)
else
error(xs:QName("Error"), concat(" Expected values for responseTypeCode are NA or AK. Current value is: ", $input/ns0:technicalResponse/ns0:responseTypeCode))
}
</TechnicalAcknowledgement>
Subject:XQuery problem with namespaces Author:Roel van der Hoeven Date:29 Feb 2012 03:32 AM
Ivan,
Thanks for your input. I had the namespace setup as you did, but still didn't work, then I saw that our parser strips the header of the file which contains meta data and only sends me the body, the namespace declaration wasn't passed on. I manually put it in the attached XML in the opening post, but after that I didn't set the namespace correct... stupid of me! I'll make sure our parser will pass on the declaration to the body.
The only small problem I have now is that it works fine when namespaces are declared, but XML files without namespaces will not work.
I can work around that by doing stuff like:
if ($input/ta13:technicalResponse/ta13:responseTypeCode = "AK" or $input/technicalResponse/responseTypeCode = "AK")
But that doesn't strike me as best practise, is there another way of dealing with this?
Subject:XQuery problem with namespaces Author:Ivan Pedruzzi Date:29 Feb 2012 11:56 AM
You could use other techniques like matching just the local name or local name and namespace URI, but what you are suggesting is the most efficient.
Watch out, trying to handle two types of document using the same query, may be a slippery sloop, usually namespaces are used to discriminate one dialect from another.