[xsd-users] XSD and XPath

Boris Kolpackov boris at codesynthesis.com
Fri May 9 08:17:28 EDT 2008


Hi Attila,

Attila <atteeela at gmail.com> writes:

> I am wondering if it is possible to use XPath expressions against an XSD
> object model to obtain a particular node.
> I know that I could do XPath queries against an instance document and then
> convert to object model, but that is too expensive.

XPath can be used together with the DOM document association feature.
This feature allows you to maintain bidirectional association with
DOM and object model nodes. You can then execute XPath queries on
the underlying DOM document, obtain resulting DOM node(s), resolve
associated object model nodes and handle the result using the more
convenient object model. In fact, in the next release of XSD we will
include an example on how to to this. For the current release you
can download this example via this link:

http://codesynthesis.com/~boris/tmp/xpath.tar.gz

Note also that the above approach has a number of limitations: the
DOM association is created only when you parse an XML document to
the object model and underlying DOM nodes do not automatically
reflect changes made to the object model and vice versa. For more
information on DOM association see Section 5.1, "DOM Association"
in the C++/Tree Mapping User Manual:

http://codesynthesis.com/projects/xsd/documentation/cxx/tree/manual/#5.1


> Another question is:  Will the object model be populated with the defaults
> specified in the schema if the elements/attributes are not present in an
> instance document?

If XML Schema validation is enabled in Xerces-C++ then default values for
both attributes and elements are automatically assigned in DOM and in the
object model.

If XML Schema validation is disabled then the generated code assigns
default values for attributes (elements with default values are quite
inconvenient and therefore are not widely used, I don't remember ever
seeing a practical schema using them). Note also that there will be no
default attributes or elements in DOM with this setup.

Boris




More information about the xsd-users mailing list