Kind of global attribute

This should cover W3C XML Schema, Relax NG and DTD related problems.
alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Kind of global attribute

Post by alk » Tue Feb 14, 2006 11:05 am

Hi,

I need the possibility to mark some attributes within an XML file as "unclear", meaning that the person who entered a value is not sure whether the value is correct or not (and no, it's not a matter of validation). So I thought it would be nice to have an additional attribute named unclear where the names of the unclear attributes could be listed. Seems to me as if I have to add this attribute to all elements of my XSD and that's what I want to avoid.
The question is if it is possible to define some kind of attribute at a single place that can be used in all my elements without having to define the usage over and over again. This may sound a little weird, but all ideas on solving the problem are truly appreciated.

Cheers
Alex

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Feb 14, 2006 11:28 am

Hi Alex,

The best approach I think will be to define a base type for all your elements types and there put the attribute you want to appear on all elements.
You can see a similar approach in the XML Schema for XSLT 2.0, look at generic-element-type and at versioned-element-type for intance:
http://www.w3.org/TR/xslt20/#schema-for-xslt

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Tue Feb 14, 2006 12:03 pm

Hi George,

thanks for your answer. I thought about something like that, but still all the complexTypes and elements would have to use this base type and that requires a lot of adaptation work in a rather longish schema definition file. I gues there is no way to extend the XMLSchema <element> definition with an attribute and that's it???

Regards,
Alex

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Feb 14, 2006 12:35 pm

If you have global types for your elements then you can use redefine to redefine those types to allow the desired attribute. See below a working sample.


testRedefine.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="test">
<xs:complexType>
<xs:sequence>
<xs:element maxOccurs="unbounded" ref="message"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:complexType name="MessageType">
<xs:sequence>
<xs:element ref="type"/>
</xs:sequence>
</xs:complexType>
<xs:element name="message" type="MessageType"/>
<xs:element name="type" type="xs:string"/>
</xs:schema>
test.xsd

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">

<xs:redefine schemaLocation="testRedefine.xsd">
<xs:complexType name="MessageType">
<xs:complexContent>
<xs:extension base="MessageType">
<xs:attribute name="version" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
</xs:redefine>
</xs:schema>
test.xml

Code: Select all


<?xml version="1.0"?>
<test>
<message version="1.0">
<type>test</type>
</message>
</test>
Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Tue Feb 14, 2006 2:32 pm

Yes, I know. Thanks for your efforts. What I would like to do is a redefine of <xs:element> to add the attribute there, but I fear that's not possible - or at least I don't understand how. That would mean add the attribute once and then use it everywhere. Am I nuts???

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Tue Feb 14, 2006 4:43 pm

Hi Alex,

You can define an attribute as a global component in a schema file and then you can just refer to that instead of defining it in elements. The caveat hear is that the attribute will belong to the schema target namespace while if you define it in each element (that is locally) it will belong by default to no namespace unless you set the form to qualified.

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Wed Feb 15, 2006 9:42 am

Hi George,

sorry to bother you again, but correct me if I'm wrong. Assumed I define a global attribute like this

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="myTargetNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="myTargetNamespace" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyValue">
<xs:complexType>
<xs:attribute name="Amount" type="xs:integer" use="required"/>
</xs:complexType>
</xs:element>
<xs:attribute name="unclear" type="xs:string"/>
</xs:schema>
In order to use the attribute in "MyValue" I still have to add

Code: Select all


    <xs:attribute ref="unclear"/>
to the element definition and. This is the step that I would somehow like to avoid. Therefore I thought about finding a way of defining the attribute only once and then it should automatically be available in all elements.

Cheers,
Alex

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Wed Feb 15, 2006 10:24 am

Hi Alex,

XML Schema does not provide a way to do that.

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Wed Feb 15, 2006 12:10 pm

Hi George,

that's what I feared...

I'm giving the global attribute a try, but now the next problem comes up. I've set elementFormDefault=qualified and attributeFormDefault=unqualified. This way the elements must be qualified and the attributes must not be qualified - unless the attribute is declared globally. Since my target/default namepspace does not use a prefix, but the global definition of the attribute requires me to use one, I'm stuck again :cry:

Cheers,
Alex

george
Site Admin
Posts: 2102
Joined: Thu Jan 09, 2003 2:58 pm

Post by george » Wed Feb 15, 2006 12:23 pm

Hi Alex,

The global attribute belons to the schema target namespace. The ref attribute value is a QName and it is resolved in the current namespace context where it appears. To use your example, if you have the target namespace also as default namespace then you do not need a prefix to refer to the attribute.

Code: Select all


<?xml version="1.0" encoding="UTF-8"?>
<xs:schema targetNamespace="myTargetNamespace" xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="myTargetNamespace" elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="MyValue">
<xs:complexType>
<xs:attribute name="Amount" type="xs:integer" use="required"/>
<xs:attribute ref="unclear"></xs:attribute>
</xs:complexType>
</xs:element>
<xs:attribute name="unclear" type="xs:string"/>
</xs:schema>
Anyway, as the attribute is defined in the target namespace when you will enter that attribute in an XML instance you always will need to qualify its name with a prefix bound to the schema target namespace (as attributes in instance documents do not use the default namespace).

Best Regards,
George

alk
Posts: 32
Joined: Wed Jan 25, 2006 5:43 pm

Post by alk » Wed Feb 15, 2006 3:08 pm

Hi George,

since my default name space does not use a prefix I had to put the global attribute into a seperate schema definition file with its own namespace. Then I just imported this namespace into my actual schema and used the prefix that I assigned to the new namespace with the global attribute. In other words: it works! Thanks for your help.

Best Regards,
Alex

Post Reply