[xsd-users] CoT schema and xsd followup

Boris Kolpackov boris at codesynthesis.com
Wed Jun 28 11:53:30 EDT 2006


Hi Matt,

Matt Burnham <mwburn at mhpcc.hpc.mil> writes:

> I've updated my version of xsd to the latest and using some of the added
> options, things are now working.

I am glad it is working for you.


> I have a "core" schema that has a placeholder element called "detail".
> I then have multiple sub-schemas for data that can go inside of the
> "detail" section.  My actual xml file that I'm parsing get's loaded into
> the cxx-tree, but I can only access the "detail" element.  Is there any
> way to get to the "embedded data?  And then pass this data to the
> parsers I have for it?
>
> [...]
>
> <?xml version="1.0" encoding="us-ascii" standalone="yes"?>
> <event>
>    <detail>
>        <embedded-data>
>        </embedded-data/>
>    </detail>
> </event>

You can do this but it will require a bit of work. The basic idea is
as follows:

(1) Parse your top-level XML with the keep_dom option (see the mixed
    example in examples/cxx/tree).

(2) Obtain DOMElement corresponding to embedded-data node:

    using namespace xercesc;

    detail& d = ...

    DOMElement* de (static_cast<DOMElement*> (d._node ()));
    DOMNodeList* nl (de->getElementsByTagName ("embedded-data");
    DOMElement* ede (nl->item (0));

(3) Construct the type corresponding to 'embedded-data' using
    DOMElement:

    embedded_data ed (ede);


There is, however, a better, more elegant way of doing this if you
can change your schemas. The idea is to use XML Schema polymorphism
to define open-ended content. Your 'detail' element can be defined
as follows:

<xs:element name="content" type="xs:anyType"/>

<xs:element name="detail">
  <xs:complexType>
    <xs:sequence>
      <xs:element ref="content" minOccurs="0" maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

anyType is an XML Schema built-in type that matches any content.

Now you can use XML Schema substitution groups to define concrete
instances of 'content':

<xs:complexType name="embedded_data">
  <xs:complexContent>
    <xs:restriction base="xs:anyType">

      ...

    </xs:restriction>
  </xs:complexContent>
</xs:complexType>

<xs:element name="embedded-data" type="embedded_data" substitutionGroup="content"/>


Your instance still looks exactly the same. But the generated code
handles instantiation of 'embedded_data' automatically:

detail& d = ...

xml_schema::type& c (d.content ()); // xs:anyType maps to xml_schema::type

if (embedded_data* ed = dynamic_cast<embedded_data*> (&c))
{
  // content is embedded_data
}

Individual schemas that define various extension data (like
'embedded_data') can be developed separately from the root
schema which defines the 'content' element (they will need
to include the root schema and be linked into the application,
though). There is an example called polymorphism in
examples/cxx/tree that shows how this works.


hth,
-boris

-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://codesynthesis.com/pipermail/xsd-users/attachments/20060628/6645a021/attachment.pgp


More information about the xsd-users mailing list