Schema: error with restriction in simpleContent

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Kai Schlamp

    #1

    Schema: error with restriction in simpleContent

    Hello!

    In my schema I have the following:

    <xs:complexTy pe name="textareaT ype">
    <xs:simpleConte nt>
    <xs:restricti on base="xs:string ">
    <xs:attribute ref="label" use="required" />
    </xs:restriction>
    </xs:simpleConten t>
    </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://
    https://www.w3schools.com/schema/sch...plex_text.asp).
    I also checked the original Schema reference (http://www.w3.org/TR/
    xmlschema-1/), that points out: "Note that either <restrictiono r
    <extensionmus t 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

  • Pavel Lepin

    #2
    Re: Schema: error with restriction in simpleContent


    Kai Schlamp <stroncococcus@ gmx.dewrote in
    <1193862601.833 480.71940@50g20 00hsm.googlegro ups.com>:
    <xs:complexTy pe name="textareaT ype">
    <xs:simpleConte nt>
    <xs:restricti on base="xs:string ">
    <xs:attribute ref="label" use="required" />
    </xs:restriction>
    </xs:simpleConten t>
    </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 <restrictiono r <extensionmus t 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."

    Comment

    • Kai Schlamp

      #3
      Re: Schema: error with restriction in simpleContent

      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?

      Comment

      • Pavel Lepin

        #4
        Re: Schema: error with restriction in simpleContent


        Please quote what you're replying to.

        Kai Schlamp <stroncococcus@ gmx.dewrote in
        <1193913218.460 782.168360@o38g 2000hse.googleg roups.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="textareaT ype"/>
        <xs:attribute name="label" type="xs:string "/>
        <xs:complexTy pe name="textareaT ype">
        <xs:simpleConte nt>
        <xs:extension base="str-10-chr">
        <xs:attribute ref="label" use="required"/>
        </xs:extension>
        </xs:simpleConten t>
        </xs:complexType>
        <xs:simpleTyp e name="str-10-chr">
        <xs:restricti on 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>mor e 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">mor e 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;1 0 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."

        Comment

        Working...