[xsd-users] Attempting to access attribute xml:base.

Boris Kolpackov boris at codesynthesis.com
Fri Dec 22 04:35:48 EST 2006


Hi Jeroen,

Jeroen N. Witmond <jnw at xs4all.nl> writes:

> First of all, thanks for a great product. It really makes accessing
> XML from C++ much easier.

Thanks. We are glad you are enjoying it.


> Unfortunately, I'm having problems finding out how to access
> attributes with the xml prefix, specifically xml:base, from my
> application code.  Nowhere in the documentation or the generated code
> can I find a way to do so.

There is not much special about attributes from the XML namespace.
They are defined in the following schema by W3C:

http://www.w3.org/2001/xml.xsd

If you want to allow one or more attributes from this schema in your
type then you will first import the schema and then add a reference
to the attributes you need, for example:

<schema xmlns="http://www.w3.org/2001/XMLSchema"
        xmlns:t="test"
        targetNamespace="test">

  <import namespace="http://www.w3.org/XML/1998/namespace"
          schemaLocation="xml.xsd"/>

  <complexType name="type">
    <sequence>
      <element name="a" type="string"/>
    </sequence>
    <attribute ref="xml:base"/>
  </complexType>

  <element name="root" type="t:type"/>

</schema>

You will need to compile both your schema and xml.xsd. In the
generated code you will have a set of member functions to access
the value of 'base'.

The only special thing about the XML namespace is that you do not
need to define a namespace-prefix mapping for the 'xml' prefix.

If you want to allow say xml:base in all types in your schema then
you may want to create a base type which includes only that attribute
and then inherit all other types from it.


> I tried using
>
>   xsd cxx-tree --generate-xml-schema XmlSchema.xsd
>   xsd cxx-tree --extern-xml-schema   XmlSchema.xsd [other options] \
> toaccess.xsd
>
> But this only resulted in the typedefs in namespace xml_schema being
> declared in file XmlSchema.hxx instead of in toaccess.hxx.

That's expected. These options are useful when you want to factor
out those typedefs into a separate header because, for example,
you want to customize XML Schema built-in types of you have a lot
of schemas and want to reduce compilation time.


> On a related note: I came across an xsd file containing
>
>   <xs:import namespace="http://www.w3.org/XML/1998/namespace"/>
>
> Processing this xsd file results in an error about a missing
> schemaLocation attribute.  Specifying one (for instance
> http://www.w3.org/2001/XMLSchema.xsd as Google found) does not work.

Google made a mistake (is that at all possible? ;-)). The schema for
the http://www.w3.org/XML/1998/namespace namespace is xml.xsd.


> And I don't think downloading the schema for schemas (and all files it
> references) to a local disk and turning xsd loose on the result is a
> viable option. :-)

It may sound strange (it sure did to me when I first found out) but
Schema for XML Schema (XMLSchema.xsd) is not really a valid XML Schema
definition. It is there for illustration purposes only.


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/20061222/154a9dd8/attachment.pgp


More information about the xsd-users mailing list