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><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"><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."