Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

[XML Schema] Element with restriction and attribute

Hello, I have the following problem and I really hope someone here can help me.

I am trying to write an XML Schema to validate some XML and the problem is that I can't seem to get an attribute on an element that has a restriction.

I want to validate this:
<email public="no">example@host.com</email>

But I want to make sure that the adress is actually an email adress by checking it with a regular expression:
<xs:element name="email">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:pattern value="regex"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
But I also want the attribute. Problem is, I can't add it to a simpleType, so I need to give "email" a complexType. But doing that, restricts me from setting a restriction on the content of the element, because the parser says I can't use the xs:string-base for a restriction in a complexType.

I have searched for the answer to my question for a couple of days now and I still haven't found it. I really can't believe that nobody knows a solution, because I would think that it is kind of common to have something similar to what I want. Anyway, I hope someone will be able to help. Thanks!
[1322 byte] By [CyberByte] at [2007-11-11 6:45:36]
# 1 Re: [XML Schema] Element with restriction and attribute
I had a similar issue... I ended up defining a type for the restriction, and then used the type in a simpleContent extension:

<!-- Type definition -->
<xs:simpleType name="emailString">
<xs:restriction base="xs:string">
<xs:pattern value="regex" />
</xs:restriction>
</xs:simpleType>

<!-- Element -->
<xs:element name="email">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="emailString">
<xs:attribute name="public" type="xs:boolean" />
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
Archistrategos at 2007-11-11 23:28:54 >