Schema: error with restriction in simpleContent

Discussion in 'XML' started by Kai Schlamp, Oct 31, 2007.

  1. Kai Schlamp

    Kai Schlamp Guest

    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://
    www.w3schools.com/schema/schema_complex_text.asp).
    I also checked the original Schema reference (http://www.w3.org/TR/
    xmlschema-1/), that points out: "Note that either <restriction> or
    <extension> must 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
     
    Kai Schlamp, Oct 31, 2007
    #1
    1. Advertising

  2. Kai Schlamp

    Pavel Lepin Guest

    Kai Schlamp <> wrote in
    <>:
    > <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/schema_complex_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 <restriction> or <extension> must 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."
     
    Pavel Lepin, Nov 1, 2007
    #2
    1. Advertising

  3. Kai Schlamp

    Kai Schlamp Guest

    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?
     
    Kai Schlamp, Nov 1, 2007
    #3
  4. Kai Schlamp

    Pavel Lepin Guest

    Please quote what you're replying to.

    Kai Schlamp <> wrote in
    <>:
    > 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."
     
    Pavel Lepin, Nov 1, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Random
    Replies:
    1
    Views:
    4,727
    Random
    Nov 19, 2004
  2. Markus
    Replies:
    1
    Views:
    1,660
    Markus
    Nov 23, 2005
  3. David Norman
    Replies:
    3
    Views:
    769
    Peter Flynn
    Aug 4, 2004
  4. Eric D
    Replies:
    2
    Views:
    453
    George Bina
    Jan 31, 2007
  5. Thijs
    Replies:
    1
    Views:
    490
    Martin Honnen
    May 28, 2008
Loading...

Share This Page