[xsd-users] Creating elements in code

Attila atteeela at gmail.com
Tue May 6 14:42:44 EDT 2008


Hi Boris,

I greatly appreciate your response and it provides me with a good starting
point for further research.

Maybe I'll provide a little background as to why I am asking all this :)

We currently have an application that uses simple-line-based configuration
files (flat config) and would like to convert it over to XML.
Furthermore we have an automation interface that allows users to send
configuration entries over TCP to dynamically replace the contents of the
flat file and then have these settings become effective.

What I was thinking was to have the parsed CS XSD objects as the
"configuration collection" underneath and then be able to use webservices or
something to dynamically replace the CS XSD objects.

So if someone sends a command to replace element "foo", then I  could just
parse this element, and then use it to replace the foo element in my
configuration collection underneath.  Furthermore, will I be able to
potentially "build up" a "more complete" configuration file (defined by say
mytoolConfig.xsd) and then write it out to an instance document:
configsnapshot.xml?

Also: will the CS XSD parser restrict how many instances I can have of say
"foo" if my mytoolConfig.xsd says I can only have 1 instance?  Or will this
only be restricted if I choose to write it out to an XSD/validate it?

Thank you,


On Thu, May 1, 2008 at 4:04 AM, Boris Kolpackov <boris at codesynthesis.com>
wrote:

> 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
>



-- 
Attila
Software Developer
atteeela at gmail.com



More information about the xsd-users mailing list