1

I have a question regarding namespaces for attributes, and how to declare such attributes in an XML schema.

The reason that I want to use namespaces for an attribute is that I use XML DSig, and the signature parts should be specified with an "ID" attribute.

The name of the attribute that specify the identification could have any name, and some possible choices are "ID", "Id" or "id".

However, there seems to be a recommendation to use "xml:id", rather than anything else such as "ID".

This is for example described in the following page: http://www.w3.org/TR/xml-id/

This works fine for DSig signatures. I am using "xmlsec1" for signing/verification in my C++ application, and I am able to sign and verify the signature.

In the XML files, the id is specified like this:

<elm:MyElement xml:id="MyName" OtherAttribute="SomeValue">

Note that "xml:id" is an attribute in a namespace, which is rarely used although legal XML. However, the XML files should also be validated against a schema with Xerces, and it is there I have some problems.

The problem is probably that I do not specify the schema correctly, rather than a problem in Xerces-C.

I have tried to specify the attribute name as "xml:id" in the scheme like this:

                    <xs:attribute name="xml:id" use="required">
                            <xs:simpleType>
                               <xs:restriction base="xs:string">
                                  <xs:enumeration value="MyName"/>
                               </xs:restriction>
                            </xs:simpleType>
                    </xs:attribute>

However, when I try to validate the document, I get the following error message:

Error at file XMLParserInput, line 1, char 441
  Message: attribute '{http://www.w3.org/XML/1998/namespace}id' is not
declared for element 'Routing'

My XML document has an "xml:id" attribute, but the Xerces validator does not seem to think that this attribute is declared according to the schema.

My question now is how I should write the XML schema to make it accept "xml:id"?

I have searched on the net, and I have got some hints, for example this:

http://xsd.stylusstudio.com/2004Oct/post01004.htm

But my XML schema is more complicated, and consists of several xsd files and multiple namespaces, so I have not been able to make it validate with Xerces.

My XML file declares something similar to the following:

<soap:Envelope xmlns:soap="http://www.w3.org/2001/12/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:qwerty="
http://www.mycompany.se/qwert" xmlns:abcd="http://www.mycompany.se/abcd">

The schema file(s) declares information similar to this:

<xs:schema xmlns:abcd="http://www.mycompany.se/abcd" xmlns:xs="
http://www.w3.org/2001/XMLSchema"  targetNamespace="http://www.mycompany.se/abcd"

elementFormDefault="qualified" attributeFormDefault="unqualified">

Can you give some hints how I should declare the XML schema to validate the XML file with xml:id?

Regards /Mikael

1 Answer 1

0

One easy approach would be to add

<xs:anyAttribute namespace="http://www.w3.org/XML/1998/namespace"
                 processContents="skip" />

to the relevant elements. This should allow any xml: attributes. If you want to actually validate the ID attributes then you would need to define a separate schema for the xml namespace

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.w3.org/1998/XML/Namespace">
    <xs:attribute name="id" type="xs:ID"/>
</xs:schema>

<xs:import> this schema into your main schema and then declare

<xs:attribute ref="xml:id" use="required" />

at the appropriate place.

2
  • Thanks for this suggestion, this could be a possible solution. However, this will not validate the contents of the attribute, and we would like validation to be done also for this attribute. Is there any other possible method to add xml:id to the schema which also does validation? Jan 8, 2013 at 14:36
  • @MikaelSvenning I've added an alternative if you want to properly validate that the xml:id is a valid ID. Jan 8, 2013 at 14:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.