[xsd-users] Creating elements in code

Boris Kolpackov boris at codesynthesis.com
Thu May 1 04:04:10 EDT 2008


Hi Attila,

Attila <atteeela at gmail.com> writes:

> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
> targetNamespace="http://www.w3schools.com"
> xmlns="http://www.w3schools.com"
> elementFormDefault="qualified">
>
> <xs:element name="note">
>     <xs:complexType>
>       <xs:sequence>
> 	<xs:element name="to" type="xs:string"/>
> 	<xs:element name="from" type="xs:string"/>
> 	<xs:element name="heading" type="xs:string"/>
> 	<xs:element name="body" type="xs:string"/>
>       </xs:sequence>
>     </xs:complexType>
> </xs:element>
> <xs:element name="foo">
>     <xs:complexType>
>       <xs:sequence>
> 	<xs:element name="bar" type="xs:string"/>
>       </xs:sequence>
>     </xs:complexType>
> </xs:element>
>
> </xs:schema>
>
>
> Can I provide an XML snippet instead of an entire XML document
> like this:
>
> <note>
>  <to>jon</to>
>  <from>rick</from>
>  <heading>hello world</heading>
>  <body>no text</body>
> </note>

First, it is helpful to distinguish between valid and invalid XML
fragments according to the schema. For example, the above fragment
is invalid because all the elements in this XML are unqualified
while the schema prescribes that they all should be in the
"http://www.w3schools.com" namespace. Here is a similar but valid
fragment:

<note xmlns="http://www.w3schools.com">
  <to>jon</to>
  <from>rick</from>
  <heading>hello world</heading>
  <body>no text</body>
</note>

Not surprisingly handling invalid XML documents with the generated
code is tricky. The missing namespace declaration problem pops up
from time to time and there are a few techniques that can be used
as described in the following recent discussion:

http://www.codesynthesis.com/pipermail/xsd-users/2008-March/001587.html
http://www.codesynthesis.com/pipermail/xsd-users/2008-March/001589.html
http://www.codesynthesis.com/pipermail/xsd-users/2008-March/001593.html

Note that from the two methods described in the last message, only
the second one will work for your case (the first won't work because
all your inner elements are also qualified). In the second method you
also don't need to search for the closing tag of the root element. All
you need to do is replace the beginning of the root element (<note)
with the same element plus the default namespace declaration
(<note xmlns="http://www.w3schools.com").

Now let's assume that you are trying to parse a valid XML document.
The XML Schema specification prescribes that only non-abstract
globally-defined elements can be XML document roots. In the above
schema that would be 'note' and 'foo'. For each such element, the
XSD compiler generates a set of parsing functions that can be used
to parser the corresponding XML into the object model. So in your
case, provided noteSnippet.xml contains a valid according to the
schema XML document, you can write:

auto_ptr<note> n = note_("noteSnippet.xml");


> Furthermore, could I then take these snippets and use it to construct
> a complete XML document?

I am not sure what you mean by a "complete XML document". Any valid
according to the schema XML document is complete.

Boris




More information about the xsd-users mailing list