[xsd-users] Extracting text nodes from "any" type mapping

Boris Kolpackov boris at codesynthesis.com
Wed Sep 12 04:57:46 EDT 2007


Hi Matthew,

Wood, Matthew D <matthew.d.wood at intel.com> writes:

> <xs:complexType name="AttributeValueType" mixed="true">
>   <xs:complexContent mixed="true">
>     <xs:extension base="xacml:ExpressionType">
>       <xs:sequence>
>         <xs:any namespace="##any" processContents="lax" minOccurs="0"
> maxOccurs="unbounded"/>
>       </xs:sequence>
>       <xs:attribute name="DataType" type="xs:anyURI" use="required"/>
>       <xs:anyAttribute namespace="##any" processContents="lax"/>
>     </xs:extension>
>   </xs:complexContent>
> </xs:complexType>
>
> Valid document instances can contain either a Text node or an Element
> node.

To be precise, they can contain text fragments and any elements in any
order.


> XSD maps the "any" type to a collection of DOMElement nodes. When
> an instance containing only text is parsed, the mapping ignores the
> Text node completely.

The "any" particle only matches the elements in your type's content. The
text is allowed because of the mixed="true".


> What is the best way to work around this limitation? Do I need to parse
> with the xml_schema::flags::keep_dom flag to access the DOMNode objects
> and extract the text?

Yes, this is one of the ways to access text in the mixed content models.
This approach is used in the "mixed" example in the examples/cxx/tree/
directory.

Alternatively, if you don't want to have the complete DOM tree hanging
around just to access text in one element, you can customize the
AttributeValueType and extract the text in its parsing constructor.
This approach has an added advantage of easily supporting serializations
in addition to parsing. The "wildcard" example in the
examples/cxx/tree/custom/ directory shows how to do this for wildcard
(any and anyAttribute) but the idea is the same for mixed content (you
will be looking for DOMText instead of DOMElement or DOMAttr). For
more information to type customization see the C++/Tree Mapping
Customization Guide:


http://wiki.codesynthesis.com/Tree/Customization_guide


Boris




More information about the xsd-users mailing list