[xsd-users] reading and writing non root elements
Greg Carter
greg at permedia.ca
Wed Nov 29 19:16:25 EST 2006
Hello Boris,
One last problem. I changed my schema so that the element was global
and added a ref to it. I used the code that you gave in the last post
plus the helper functions in the FAQ. I also added a call to
setAttributeNS and added an entry for the prefix for my namespace (tns
in the example). In the generated XML I am not getting my root element
tagged with the prefix, if I manually add the prefix to the root element
it validates.
This is what is generated:
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<elOne xmlns:tns="http://www.blah.com/MySchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=http://www.blah.com/MySchema MySchema.xsd">
<content1 value="1">
</content1>
<content2 value2="2">
</content2>
</elOne>
If I change it to
<?xml version="1.0" encoding="UTF-8" standalone="no" ?>
<tns:elOne xmlns:tns="http://www.blah.com/MySchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation=http://www.blah.com/MySchema MySchema.xsd">
<content1 value="1">
</content1>
<content2 value2="2">
</content2>
</tns:elOne>
(add the tns: prefix to elOne)
it works. This is also how the XML looks (tns namespace prefix) when
generated by the XSD generated code for the global element I chose as
the root element.
I did step through the XSD generated code to see where the prefix was
added but it wasn't immediately clear.
I do have a comment on multiple global elements, but I'll put it in a
new message to the list.
Thanks.
Greg.
Boris Kolpackov wrote:
> Hi Greg,
>
>
> I am glad you've figured it out. I think your problem with setProperty
> comes from the fact that Xerces-C++ treats the location as a relative
> path unless it is in the form "file:///absolute/path/to/your/schema.xsd".
> You can read more about this in Q 2.4 of the "official" C++/Tree Mapping
> FAQ:
>
> http://codesynthesis.com/projects/xsd/documentation/cxx/tree/faq/#2.4
>
>
> Also note that in order for your XML fragment to be successfully
> validated against the schema you will need to declare the fragment's
> root element as global in the schema. Only global elements are allowed
> to be document roots in XML Schema.
>
>
> hth,
> -boris
>
More information about the xsd-users
mailing list