Click to See Complete Forum and Search --> : [XML Schema] Element with restriction and attribute


Jordi
08-31-2005, 06:05 PM
I am trying to write a XML Schema and I want it to validate an example XML element. But there's one thing that I can't really get to work: I'm trying to write something that will validate the following:
<email public="yes">email@host.com</email>

I want to place restrictions on both the attribute and the value of the field, but I can't seem to get it to work. The attribute should only be able to take the values "yes" and "no" and the email adress should be verified through a regular expression. I know how to make an element that contains just text and an attribute: (the attribute restriction is taken care of by the publicType)

<xs:element name="email">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute name="public" type="publicType" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</element>

I'd like to add something like this to that:
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$"/>
</xs:restriction>

The problem is that I need a restriction for the email field. But I need an extension to attach the attribute. And I can't have both, because that's appearantly illegal in XML Schema.
Can anyone help me with this please?

theuedimaster
09-04-2005, 12:52 PM
Use the format

<email>
<address>blah@gmail.com</address>
<status>verified</status>
</email>

sheila
09-06-2005, 02:55 AM
Personally, I wouldn't change my XML source code as theuedimaster suggests. You're recording the public status of that specific email address (you might want to store more than one address as a child of email) so it should be kept in context (as a child of address rather than a sibling) and it's meta-data rather then content, hence the appropriateness of an attribute.

As to your schema problem, why do you need an extension to attatch the attribute? Wouldn't this work?


<xs:element name="email">
<xs:complexType>
<xs:attribute name="public" type="confirmation" />
<xs:restriction base="xs:string">
<xs:pattern value="^[0-9a-z]([-_.~]?[0-9a-z])*@[0-9a-z]([-.]?[0-9a-z])*\\.[a-z]{2,4}$"/>
</xs:restriction>
</xs:complexType>
</element>

<xs:simpleType name="confirmation">
<xs:restriction base="xs:string">
<xs:enumeration value="yes" />
<xs:enumeration value="no" />
</xs:restriction>
</xs:simpleType>