By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,838 Members | 1,668 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,838 IT Pros & Developers. It's quick & easy.

Schema: error with restriction in simpleContent

P: n/a
Hello!

In my schema I have the following:

<xs:complexType name="textareaType">
<xs:simpleContent>
<xs:restriction base="xs:string">
<xs:attribute ref="label" use="required" />
</xs:restriction>
</xs:simpleContent>
</xs:complexType>

But W3C Schema validator tells me there is an error (derivation of
complex type by restriction of simple type not allowed), even if
w3schools writes, that this should be valid code (http://
http://www.w3schools.com/schema/sche...plex_text.asp).
I also checked the original Schema reference (http://www.w3.org/TR/
xmlschema-1/), that points out: "Note that either <restrictionor
<extensionmust be chosen as the content of <simpleContent>."
When I replace restriction by extension everything works fine. But I
want to restrict the element type in a further step.
Any help?

Best regards,
Kai

Oct 31 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Kai Schlamp <st***********@gmx.dewrote in
<11*********************@50g2000hsm.googlegroups.c om>:
<xs:complexType name="textareaType">
<xs:simpleContent>
<xs:restriction base="xs:string">
<xs:attribute ref="label" use="required" />
</xs:restriction>
</xs:simpleContent>
</xs:complexType>

But W3C Schema validator tells me there is an error
(derivation of complex type by restriction of simple type
not allowed),
Guess what? They're right.
even if w3schools writes, that this should be valid code
(http://www.w3schools.com/schema/sche...plex_text.asp).
I'm of very low opinion of w3schools in general, but in this
case they don't assert anything of the sort. They just
didn't mention that a complex type may not be a restriction
of a simple type.
I also checked the original Schema reference
(http://www.w3.org/TR/ xmlschema-1/), that points out:
"Note that either <restrictionor <extensionmust be
chosen as the content of <simpleContent>."
You should've checked 2.2.1.3 as well.

Since you forgot to mention what it is that you're trying to
achieve, I'll refrain from giving any further advice.

--
"I can't help but wonder if you... don't know a hell of a
lot more about practically every subject than Solomon ever
did."
Nov 1 '07 #2

P: n/a
Hy Pavel,

thanks for the answer.
I just want to scheme a simple xml tag like this
<mytag myattr="10">foo</mytag>
and want to restrict "foo" to the length of 10 characters, thats all.
But I didn't find an example on the web for doing such an restriction
in this case (I am pretty new to XML, but I think you already know
that ;-))
As I understand the mytag must be a complex type, cause it has an
attribute ("myattr") and a content ("foo").
What's the best way to achieve this?

Nov 1 '07 #3

P: n/a

Please quote what you're replying to.

Kai Schlamp <st***********@gmx.dewrote in
<11**********************@o38g2000hse.googlegroups .com>:
I just want to scheme a simple xml tag like this
<mytag myattr="10">foo</mytag>
and want to restrict "foo" to the length of 10 characters,
thats all. But I didn't find an example on the web for
doing such an restriction in this case (I am pretty new to
XML, but I think you already know that ;-))
As I understand the mytag must be a complex type, cause it
has an attribute ("myattr") and a content ("foo").
What's the best way to achieve this?
Well, restrict then extend.

pavel@debian:~/dev/schema$ a schema.xsd
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="textarea" type="textareaType"/>
<xs:attribute name="label" type="xs:string"/>
<xs:complexType name="textareaType">
<xs:simpleContent>
<xs:extension base="str-10-chr">
<xs:attribute ref="label" use="required"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
<xs:simpleType name="str-10-chr">
<xs:restriction base="xs:string">
<xs:maxLength value="10"/>
</xs:restriction>
</xs:simpleType>
</xs:schema>
pavel@debian:~/dev/schema$ for F in sch*.xml ; do
xmllint --schema schema.xsd $F ; echo ; done
<?xml version="1.0"?>
<textarea>more than 10 characters</textarea>
sch1.xml:1: element textarea: Schemas validity error :
Element 'textarea': The attribute 'label' is required but
missing.
sch1.xml:1: element textarea: Schemas validity error :
Element 'textarea': [facet 'maxLength'] The value has a
length of '23'; this exceeds the allowed maximum length
of '10'.
sch1.xml:1: element textarea: Schemas validity error :
Element 'textarea': 'more than 10 characters' is not a
valid value of the atomic type 'str-10-chr'.
sch1.xml fails to validate

<?xml version="1.0"?>
<textarea label="aaa">more than 10 characters</textarea>
sch2.xml:1: element textarea: Schemas validity error :
Element 'textarea': [facet 'maxLength'] The value has a
length of '23'; this exceeds the allowed maximum length
of '10'.
sch2.xml:1: element textarea: Schemas validity error :
Element 'textarea': 'more than 10 characters' is not a
valid value of the atomic type 'str-10-chr'.
sch2.xml fails to validate

<?xml version="1.0"?>
<textarea>&lt;10 chars</textarea>
sch3.xml:1: element textarea: Schemas validity error :
Element 'textarea': The attribute 'label' is required but
missing.
sch3.xml fails to validate

<?xml version="1.0"?>
<textarea label="aaa">&lt;10 chars</textarea>
sch4.xml validates

pavel@debian:~/dev/schema$

I heartily recommend reading W3C's XML Schema Primer. It
might take you a day or two, but will give you a solid
understanding of the basics.

--
"I can't help but wonder if you... don't know a hell of a
lot more about practically every subject than Solomon ever
did."
Nov 1 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.