XML Editor
Sign up for a WebBoard account Sign Up Keyword Search Search More Options... Options
Chat Rooms Chat Help Help News News Log in to WebBoard Log in Not Logged in
Show tree view Topic
Go to previous topicPrev TopicGo to next topicNext Topic
Postnext
Roel van der HoevenSubject: 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 '='.

Code:

declare namespace emt="http://pcs.portinfolink.nl/consignment_types";
declare namespace mct="http://pcs.portinfolink.nl/consignment_type";
declare namespace aty="http://pcs.portinfolink.nl/em_message_component_types";
declare namespace ns0="http://pcs.portinfolink.nl/schema";

declare variable $input as document-node(element(*, xs:untyped)) external;

declare option ddtek:xml-streaming 'no';
declare option ddtek:serialize 'indent=yes,omit-xml-declaration=no';

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>


DocumentXML_CONTRL_IN_TA12_var.xml
XML input

Postnext
Ivan PedruzziSubject: XQuery problem with namespaces
Author: Ivan Pedruzzi
Date: 28 Feb 2012 11:47 AM
Originally Posted: 28 Feb 2012 10:59 AM
Hi Roel,

We tested the query using DataDirect XQuery version 5.0 and we had to change the namespace declaration to make it work

declare namespace emt="http://pcs.portinfolink.nl/consignment_types";
declare namespace mct="http://pcs.portinfolink.nl/consignment_type";
declare namespace aty="http://pcs.portinfolink.nl/em_message_component_types";
(:declare namespace ns0="http://pcs.portinfolink.nl/schema";:)
declare namespace ns0="http://www.portbase.com/schema";

When binding the document programmatically we did the following

expr.bindDocument(qName, new StreamSource(paramValue), xqItemType);

Given the sample we did not encounter the error, never the less such type mismatch can be fixed wrapping the expression in a string like the following

if (string($input/ns0:technicalResponse/ns0:responseTypeCode) = "AK")

Does it help?


Ivan Pedruzzi
Stylus Studio Team

Postnext
Roel van der HoevenSubject: 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?

Posttop
Ivan PedruzziSubject: 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.

Hope this helps
Ivan Pedruzzi
Stylus Studio Team

 
Go to previous topicPrev TopicGo to next topicNext Topic
Download A Free Trial of Stylus Studio 6 XML Professional Edition Today! Powered by Stylus Studio, the world's leading XML IDE for XML, XSLT, XQuery, XML Schema, DTD, XPath, WSDL, XHTML, SQL/XML, and XML Mapping!  
go

Log In Options

Site Map | Privacy Policy | Terms of Use | Trademarks
Stylus Scoop XML Newsletter:
W3C Member
Stylus Studio® and DataDirect XQuery ™are from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2016 All Rights Reserved.