Want to define an Attribute as either String or Byte

This should cover W3C XML Schema, Relax NG and DTD related problems.
mlcook
Posts: 67
Joined: Thu Jan 24, 2008 4:52 pm

Want to define an Attribute as either String or Byte

Post by mlcook »

After searching books and Internet, I can't see the solution to my problem. I'm hoping someone can provide a creative solution, or tell me it can't be done.

I'd like to define an element with a required attribute which is either a xs:string or xs:byte.

The attribute would have to have the same name, say "size", regardless of which type is used.

I'd like to be able to write, say,
<myarr size="17" />
or
<myarr size="variable" />

By allowing two types for size, there can be some early validity checking.

Additionally, my hope is that I could specify "variable" as the only value of type string allowed for the attribute.

Any ideas?

Thanks, Mike Cook
sorin_ristache
Posts: 4141
Joined: Fri Mar 28, 2003 2:12 pm

Re: Want to define an Attribute as either String or Byte

Post by sorin_ristache »

Hello,

This cannot be done in XML Schema. However you can define a different content model for the element myarr, in your case a different type for the size attribute but you have to use Relax NG. You can select the content model based on the value of a selector, for example the value of an attribute selector of the same myarr element.

Code: Select all

<?xml version="1.0" encoding="UTF-8"?>
<grammar
xmlns="http://relaxng.org/ns/structure/1.0"
xmlns:a="http://relaxng.org/ns/compatibility/annotations/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<start>
<choice>
<ref name="myarrIntegerType"/>
<ref name="myarrStringType"/>
</choice>
</start>


<define name="myarrIntegerType">
<element name="myarr">
<attribute name="selector">
<value>integer</value>
</attribute>
<attribute name="size">
<data type="positiveInteger"/>
</attribute>
</element>
</define>

<define name="myarrStringType">
<element name="myarr">
<attribute name="selector">
<value>string</value>
</attribute>
<attribute name="size">
<value>string</value>
</attribute>
</element>
</define>
</grammar>
Of course you can add other elements and attributes in the above schema where you need them. To create XML documents based on this Relax NG schema press the New button on the File toolbar or go to menu File -> New, select XML Document in the list of document types, in the Create an XML document dialog select the Relax NG tab and enter the location of the above schema in the URL field of the dialog.

You can read about oXygen's support for Relax NG schemas in the User Manual.


Regards,
Sorin
mlcook
Posts: 67
Joined: Thu Jan 24, 2008 4:52 pm

Re: Want to define an Attribute as either String or Byte

Post by mlcook »

Thanks for your suggestion and info on Relax NG.

Currently, our project is not using Relax NG, but that might be something to consider later on.

At least I know I wasn't overlooking something in schema definition, being somewhat new to all this.

Thanks again,
Mike Cook
Post Reply