Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Having both an attribute and a restriction on an element in xml schema

Tags:

xsd

I'm trying to write a xml schema that will validate this piece of xml:

<date isodate="2007-03-14">14 march 2007</date>

The attribute isodate should have it's type set to xs:date and the content should be max 50 characters long.

I wonder if it's possible to write the xml schema definition in one block, something like this maybe:

<xs:element name="date" minOccurs="0" maxOccurs="1">  
  <xs:complexType>  
    <xs:simpleContent>  
      <xs:restriction base="xs:string">  
        <xs:minLength value="0"/>  
        <xs:maxLength value="50"/>  
      </xs:restriction>  
      <xs:attribute name="isodate" type="xs:date" use="required"/>  
    </xs:simpleContent>  
  </xs:complexType>  
</xs:element>

The code above doesn't work, and I can't really figure out why. Only workaround I have found is to break out the restriction part into a separate type, and link that like this:

<xs:simpleType name="reviewDate">  
    <xs:restriction base="xs:string">  
        <xs:minLength value="0"/>  
        <xs:maxLength value="50"/>  
    </xs:restriction>  
</xs:simpleType>

<xs:element name="date" minOccurs="0" maxOccurs="1">  
    <xs:complexType>  
        <xs:simpleContent>  
            <xs:extension base="reviewDate">  
                <xs:attribute name="isodate" type="xs:date" use="required"/>  
            </xs:extension>  
        </xs:simpleContent>  
    </xs:complexType>  
</xs:element>

The question I have is how to write the definition in one block so that the schema is a bit more readable, and doesn't reference types in other parts of the schema.

like image 564
Alexander Kjäll Avatar asked Aug 26 '09 16:08

Alexander Kjäll


People also ask

What XML Schema type can be used to contain other elements and attributes?

A complex element is an XML element that contains other elements and/or attributes.

Which one defines the type of XML data and restrictions?

Overview of XSDAn XSD defines the structure of an XML document. It specifies the elements and attributes that can appear in an XML document and the type of data these elements and attributes can contain.

What is qualified and unqualified in XML Schema?

Qualified or Unqualified. In XML Schema we can choose to specify whether the instance document must qualify all the elements and attributes, or must qualify only the globally declared elements and attributes. Regardless of what we choose, the entire instance would be validated.

What is an XML Schema?

The XML schema defines the shape, or structure, of an XML document, along with rules for data content and semantics such as what fields an element can contain, which sub elements it can contain and how many items can be present. It can also describe the type and values that can be placed into each element or attribute.

What is an attribute in XML?

Defining XML Attributes. The Type information describes the data the attribute can contain in the XML document, i.e. string, integer, date etc. Attributes can also be specified globally and then referenced (but more about this later).

What is a schema element?

The schema element defines the root element of a schema. Optional. Specifies a unique ID for the element Optional. The form for attributes declared in the target namespace of this schema.

What is the type property in XML?

The 'type' property provides the description of what type of data can be contained within the element when it appears in the XML document. There are a number of predefined simple types, such as xs:string, xs:integer, xs:boolean and xs:date (see XSD standard for a complete list).


2 Answers

You cannot merge both a restriction and an extension into one block of XSD. The solution that you have with the "ReviewDate" simple type is the best solution I know of.

Marc

like image 191
marc_s Avatar answered Oct 19 '22 09:10

marc_s


You can have a element with restriction and attribute(-s). The key is to define custom type with it's restrictions and then using it add attributes to it. Refer here: Content restriction and attribute validation on the same element in XSD

like image 2
Ignas Avatar answered Oct 19 '22 08:10

Ignas