Author |
Message
|
mateusz |
Posted: Tue Sep 07, 2010 10:20 am Post subject: xml:space attribute on string causes validation error |
|
|
Newbie
Joined: 23 Sep 2009 Posts: 5
|
Toolkit: V 6.1.0.4
Broker V 6.1
Hello,
I have some elements in an xml schema which will contain positional data (leading and trailing spaces must be preserved). Without setting any attributes, I can see that the xml coming out of broker contains the positional fields in tact.
A downstream system which consumes the xml drops leading/trailing spaces unless 'xml:space="preserve"' or some other directive indicating spaces is explicitly set. I am able to add this attribute to the elements in question using:
Code: |
SET OutputRoot.XMLNSC.ns:Output.payload.code.(XMLNSC.Attribute)xml:space = 'preserve'; |
but that causes a broker exception to be thrown by the MQOuput node during validation when the XML passes through it, claiming xml:space is an invalid attribute on the element.
Has anyone dealt with such a condition? Any thoughts for how to resolve this?
I have tried to explicitly add the xml:space attribute to my schema using the following with no luck:
Code: |
<xsd:attribute name="space" default="preserve">
<xsd:simpleType>
<xsd:restriction base="xsd:NCName">
<xsd:enumeration value="default"/>
<xsd:enumeration value="preserve"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
|
I also have considered encoding spaces with '_' or ' ' or wrapping the data with a CDATA, but would rather address this on the schema side or with the xml:space attribute, according to xml standards.
Any help would be appreciated.
Thanks. |
|
Back to top |
|
 |
Vitor |
Posted: Tue Sep 07, 2010 10:31 am Post subject: Re: xml:space attribute on string causes validation error |
|
|
 Grand High Poobah
Joined: 11 Nov 2005 Posts: 26093 Location: Texas, USA
|
mateusz wrote: |
A downstream system which consumes the xml drops leading/trailing spaces unless 'xml:space="preserve"' or some other directive indicating spaces is explicitly set. |
Can you just clarify your problem a little? Checking here, the space attribute is intended to preserve whitespace in a document so that:
Code: |
<Document>
<tag>hi</tag>
</Document>
|
does not become
Code: |
<Document><tag>hi</tag></Document> |
So it doesn't (or shouldn't) affect the value of any tag content, i.e
Code: |
<tag>the quick brown fox jumps over the lazy coder</tag> |
keeps the spaces between words in any event.
So what do you mean by "positional" data? How is this downsteam app parsing the XML? Any "normal" XML parser doesn't care about such things.[/code] _________________ Honesty is the best policy.
Insanity is the best defence. |
|
Back to top |
|
 |
kimbert |
Posted: Tue Sep 07, 2010 11:07 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
You're on the right track, but you need to get the namespace right on your declaration of the 'space' attribute. See http://www.w3.org/XML/1998/namespace
But there's an even easier way to do this. You need to add an <xs:import> statement to your xsd referencing http://www.w3.org/XML/1998/namespace. The XMLNSC parser should automatically pick up the xsd for that namespace from the catalog included with the toolkit, and deploy it along with the others.
Code: |
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="anything_you_like.xsd"/> |
I've tested this in the past, so I know it works. You may need to upgrade your toolkit to a later fix pack, though. |
|
Back to top |
|
 |
mateusz |
Posted: Tue Sep 07, 2010 11:39 am Post subject: |
|
|
Newbie
Joined: 23 Sep 2009 Posts: 5
|
kimbert wrote: |
You're on the right track, but you need to get the namespace right on your declaration of the 'space' attribute. See http://www.w3.org/XML/1998/namespace
But there's an even easier way to do this. You need to add an <xs:import> statement to your xsd referencing http://www.w3.org/XML/1998/namespace. The XMLNSC parser should automatically pick up the xsd for that namespace from the catalog included with the toolkit, and deploy it along with the others.
Code: |
<xs:import namespace="http://www.w3.org/XML/1998/namespace"
schemaLocation="anything_you_like.xsd"/> |
I've tested this in the past, so I know it works. You may need to upgrade your toolkit to a later fix pack, though. |
I see. I do that in my esql before I attach the xml:space="preserve" attribute to the element:
Code: |
DECLARE xml NAMESPACE 'http://www.w3.org/XML/1998/namespace'; |
Can you go into a little more detail on how the schema needs to be defined for the element I need this set on in order for it to pass validation?
I had this at one point:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://sample.com/common" xmlns="http://sample.com/common" >
<xsd:attribute name="space" default="preserve">
<xsd:simpleType>
<xsd:restriction base="xsd:NCName">
<xsd:enumeration value="default"/>
<xsd:enumeration value="preserve"/>
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:complexType name="payload">
<xsd:sequence>
<xsd:element name="code" type="xsd:string" minOccurs="0">
<!-- <xsd:complexType>
<xsd:attribute name="space" type="xsd:string" />
</xsd:complexType> -->
</xsd:sequence>
<!-- <xsd:attribute name="space" default="preserve"/> -->
</xsd:complexType>
</xsd:schema>
|
Also, what do you mean by 'schemaLocation="anything_you_like.xsd"'? Does this not matter because xml namespace will be picked up from internal schemas?
Thanks.[/code] |
|
Back to top |
|
 |
kimbert |
Posted: Tue Sep 07, 2010 12:20 pm Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
Quote: |
I see. I do that in my esql before I attach the xml:space="preserve" attribute to the element: |
You're getting confused between the message model and the message tree.
- The validation error is reporting that the message tree does not conform to the schema.
- Your message tree is good - it contains an xml:space attribute with the correct namespace.
- Your schema is wrong - it declares an attribute called 'space' but the attribute is not in the correct namespace.
The solution is to ensure that your schema includes an attribute with called 'space' in the namespace 'http://www.w3.org/XML/1998/namespace'
You do that in the way that I described, by importing the ready-made xsd.
Quote: |
Also, what do you mean by 'schemaLocation="anything_you_like.xsd"'? Does this not matter because xml namespace will be picked up from internal schemas? |
The 'schemaLocation' attribute on <xs:import> is a hint - no more than that. Applications are free to ignore it if they know better. It is also optional, so you may as well omit it entirely - the toolkit knows where to find the schema for that namespace.
Quote: |
Can you go into a little more detail on how the schema needs to be defined for the element I need this set on in order for it to pass validation? |
Just add an attribute reference to the element's complex type - referring to xml:space, of course. |
|
Back to top |
|
 |
mateusz |
Posted: Wed Sep 08, 2010 6:46 am Post subject: |
|
|
Newbie
Joined: 23 Sep 2009 Posts: 5
|
thanks for all your help, kimbert. i was very close and your pushing me over the edge.
i now have a working solution, but its not yet 100% clean. i think its a matter of my xsd creation skills. perhaps someone can chime in and comment:
here's my desired result:
Code: |
<payload>
<code xml:space="preserve"> Y </code>
</payload>
|
here's my schema:
Code: |
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://sample.com/common" xmlns="http://sample.com/common" >
<xsd:import namespace="http://www.w3.org/XML/1998/namespace"/>
<xsd:complexType name="payload">
<xsd:sequence>
<xsd:element name="code" minOccurs="0">
<xsd:complexType mixed="true">
<xsd:attribute ref="xml:space" default="preserve"/>
</xsd:complexType>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
|
When I generated a messageSet from the above schema, I had to do two things to get this working:
1. I was getting a null pointer on the import of the xml namespace. To fix this, I had to select "Use http://www.w3.org/2001/xml.xsd schema" on the properties of the mxsd
2. I was getting a mapping error saying I couldn't place data inside the <code> element since it had no type (it wasn't expecting any data inside the tag). To fix this, in the generated mxsd off the schema, I selected the Base Type on the complexType to be of string. This changed it from a Local ComplexType to an xsd:string +
Any way to do this cleaner to get the messageSet to generate properly? Or how to get those mxsd settings to reflect in the XSD?
Thanks for everyone's help thus far.
-Mateusz |
|
Back to top |
|
 |
kimbert |
Posted: Wed Sep 08, 2010 7:00 am Post subject: |
|
|
 Jedi Council
Joined: 29 Jul 2003 Posts: 5542 Location: Southampton
|
I know the answer to this one. If your element has both attributes and text content then it is, technically-speaking, a 'complex type with simple content'.
The XML schema structure that you need is:
Code: |
<xsd:complexType name="payload">
<xsd:sequence>
<xsd:element name="code" minOccurs="0">
<xsd:complexType name="payload">
<xsd:simpleContent>
<xsd:extension base="xsd:string">
<xsd:attribute ref="xml:space" default="preserve"/>
</xsd:extension>
</xsd:simpleContent>
</xsd:complexType>
</xsd:element>
</xsd:sequence>
</xsd:complexType> |
|
|
Back to top |
|
 |
mateusz |
Posted: Wed Sep 08, 2010 7:16 am Post subject: |
|
|
Newbie
Joined: 23 Sep 2009 Posts: 5
|
that's it. issue resolved.
thanks so much. |
|
Back to top |
|
 |
|