Coding With Fun
Home Docker Django Node.js Articles Python pip guide FAQ Policy

XML Schema property


May 28, 2021 XML Schema


Table of contents


The XSD property


All properties are declared as simple types.

That is, elements with properties are always defined as composite types.


What are properties?

Simple elements cannot have properties. I f an element has a property, it is treated as a composite type. But the property itself is always declared as a simple type.


How do I declare a property?

The syntax that defines the property is

<xs:attribute name="xxx" type="yyy"/>

Here, xxx refers to the property name, and yyy specifies the data type of the property. XML Schema has many built-in data types.

The most common types are:

  • xs:string
  • xs:decimal
  • xs:integer
  • xs:boolean
  • xs:date
  • xs:time

Instance

This is an XML element with properties:

<lastname lang="EN">Smith</lastname>

This is the corresponding property definition:

<xs:attribute name="lang" type="xs:string"/>


The default and fixed values of the property

The property can have a specified default or fixed value.

When no other values are specified, the default values are automatically assigned to the element.

In the following example, the default value is "EN":

<xs:attribute name="lang" type="xs:string" default="EN"/>

Fixed values are also automatically assigned to elements, and you cannot specify additional values.

In the following example, the fixed value is "EN":

<xs:attribute name="lang" type="xs:string" fixed="EN"/>


Optional and required properties

By default, properties are optional. To make the property mandatory, use the "use" property:

<xs:attribute name="lang" type="xs:string" use="required"/>


The qualification of the content

When an XML element or property has a defined data type, qualification is added to the contents of the element or property.

If the type of the XML element is "xs:date" and it contains a string similar to "Hello World," the element will not (pass) validation.

With XML schema, you can also add your own qualifications to your XML elements and properties. T hese qualifications are called facets (editor's note: meaning polyheddre faces, which can be translated as qualifiers). You'll learn more about facet in the next section.