Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax to define the maxLength facet in XML Schema?

I'm trying to validate an XML schema with several tools, but I'm not getting a consistent message, depending on which tool I use. The following syntax seems to be the issue:

<xs:element name="Name" 
            minOccurs="1" 
            type ="xs:string" 
            maxLength = "125"/>

XML-Spy triggers an error whereas Notepad ++ (windows) and XML Copy Editor (Ubuntu) validate it. So is that syntax correct, or should I use this:

<xs:element name="name">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:minOccurs="1"/>
      <xs:maxLength = "125"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
like image 855
magdmartin Avatar asked Feb 19 '13 20:02

magdmartin


People also ask

Which one of the following options defines the type of XML data and restrictions?

An 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 enumeration in XML Schema?

Enumerations are a base simple type in the XSD specification containing a list of possible values. Single-valued enumerations are shown as restrictions of the base simple type xs:token , as illustrated below: ? < xs:simpleType name=”GraduationPlanTypeMapType”>

How is length defined in XSD?

you can always define the maximal length of a string in xsd. Just add the attribute maxLength resp. minLength .


2 Answers

This is what the syntax could look like:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xs:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="SomeContainer">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="Name" minOccurs="0">
                    <xs:simpleType>
                        <xs:restriction base="xs:string">
                            <xs:maxLength value="125"/>
                        </xs:restriction>
                    </xs:simpleType>
                </xs:element>                   
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
  • minOccurs only goes to elements in a content model. It is not expected for global level elements.
  • A minOccurs="1" is superfluous. 1 is the default value, so you shouldn't have to specify it.
  • maxLength is a constraining facet associated with simple type restrictions.
like image 182
Petru Gardea Avatar answered Nov 22 '22 23:11

Petru Gardea


You ask "So is my syntax correct [in example 1] or should I write [example 2]?"

Neither.

In your first example, you use the undeclared maxLength attribute on your xs:element element. (The minOccurs attribute may or may not be allowed, depending on context; as Petru Gardea has already pointed out, it's not legal on top-level element declarations.) The editors which don't raise errors on this are not doing a full job of checking conformance to the XSD schema for schemas (let alone the full constraints of XSD). If you want reliable validation of XSD schema documents, Xerces, Saxon, MSV, or some other conforming XSD implementation is your friend.

In your second example, minOccurs has ceased to be an attribute on an element declaration (which it can be in some contexts) and become an element (no, wrong) inside xs:restriction (no, wrong). The maxLength facet is correctly represented as an element child of xs:restriction, but the element in your example is not well formed; it seems to be trying to use the element type name as an attribute name. If you delete the erroneous minOccurs element and correct the ill-formed maxLength element, the remaining is a syntactically correct top-level element declaration for Name:

<xs:element name="name">
  <xs:simpleType>
    <xs:restriction base="xs:string">
      <xs:maxLength value = "125"/>
    </xs:restriction>
  </xs:simpleType>
</xs:element>
like image 22
C. M. Sperberg-McQueen Avatar answered Nov 23 '22 00:11

C. M. Sperberg-McQueen