What is the syntax for defining a maxLength face in an XML schema?

I am trying to validate an XML schema with multiple tools, but I am not getting a consistent message, depending on which tool I am using. The following syntax seems to be:

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

XML-Spy triggers the error, while Notepad ++ (Windows) and the XML Copy editor (Ubuntu) test it. Is this 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> 
+4
source share
2 answers

You ask: "Is my syntax correct [in example 1], or should I write [example 2]?"

None.

In the first example, you use the unqulared maxLength attribute on an xs: element element. (The minOccurs attribute may or may not be allowed, depending on the context, as Peter Gardea has already pointed out, it is not legal for declarations of top-level elements.) Editors who do not create errors on this do not do the full job of checking the conformity of the XSD scheme for schemes (not to mention the full limitations of XSD). If you need reliable verification of XSD, Xerces, Saxon, MSV schema documents or some other appropriate implementation of XSD, your friend.

In the second example, minOccurs ceases to be an element declaration attribute (which may be in some contexts) and becomes an element (no, not so) inside xs: constraint (no, incorrect). The maxLength fragment is correctly represented as a child of the xs: restriction element, but the element in your example is poorly formed; seems to be trying to use the element type name as the attribute name. If you remove the erroneous minOccurs element and correct the invalid maxLength element, the rest is the 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> 
+3
source

Here is what the syntax might 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 applies only to elements in the content model. This is not expected for global items.
  • Not enough minOccurs = "1". 1 is the default value, so you do not need to specify it.
  • maxLength is a related facet associated with simple type constraints.
+6
source

All Articles