http www w3 org 2001 xmlschema instance Exploring the Schema

Author

Reads 721

A blacksmith working meticulously with tools in a dimly lit workshop with various equipment around.
Credit: pexels.com, A blacksmith working meticulously with tools in a dimly lit workshop with various equipment around.

Exploring the Schema is a crucial part of understanding XML Schema. The XML Schema instance document is a self-describing document that provides information about the structure and constraints of an XML document.

The XML Schema instance document is a root element named "schema" that contains the definitions of the elements and attributes used in the XML document. This root element is the starting point for understanding the schema.

The "targetNamespace" attribute is used to identify the target namespace of the schema. This attribute is used to define the scope of the schema and to prevent conflicts with other schemas. The target namespace is a unique identifier for the schema.

The "xmlns" attribute is used to define the namespace prefix for the schema. This attribute is used to provide a shorthand way of referencing the namespace.

For another approach, see: Well-formed Element

Complex Type Definitions

A complex type definition is a way to create a new type based on an existing type. This can be done by deriving a new type from an existing complex-simple type by restriction, which reduces the child elements allowed or the type of a child element.

Here's an interesting read: Golang Http New Request

Credit: youtube.com, Must specify a {http://www.w3.org/2001/XMLSchema-instance}type during creating Approval process...

A complex type can also be derived by restriction from an existing complex-complex type, which restricts the character data and attributes allowed for the new type. This can be done by using elements like group, all, choice, or sequence, which must be the same or a subset of the ones appearing in the base type.

To create a new complex type, you can use the complexType element to start the definition of a new type, and then use the simpleContent element to indicate that the content model of the new type contains only character data and no elements. You can also derive the new type by extending the simple decimal type, as shown in Example 4.

Restriction of complex types is conceptually the same as restriction of simple types, except that it involves a type's declarations rather than the acceptable range of a simple type's values. A complex type derived by restriction is very similar to its base type, except that its declarations are more limited than the corresponding declarations in the base type.

Complex Type Definitions

Credit: youtube.com, XML Schema Complex Types

A complex type definition is a fundamental concept in XML schema design. It's a type that can contain character data, other elements, or both, and can also have attributes.

To define a complex type, you use the complexType element. This element is the starting point for defining a new type that can contain other elements.

One way to derive a new complex type is by extension. This involves adding attributes or elements to an existing simple type. For example, you can derive a new complex type from the simple type decimal by adding a currency attribute.

A complex type can also be derived by restriction. This means that you take an existing complex type and limit its content model. For instance, you can restrict a complex type to only allow a certain number of child elements or to require a specific type of child element.

To restrict a complex type, you use the restriction element, which can contain group, all, choice, or sequence elements. These elements must be the same or a subset of the ones appearing in the base type.

Curious to learn more? Check out: How to Use Wordpress Org

Credit: youtube.com, Complex | Meaning of complex

Here are some key points to keep in mind when restricting a complex type:

  • The restriction may contain group, all, choice, or sequence elements.
  • These elements must be the same or a subset of the ones appearing in the base type.
  • The type assigned to each element must be the same or a restriction of the type assigned to that element in the base type.
  • An element of the base type may be excluded from the restricted type.

For example, if you have a complex type that allows a minimum of 0 to a maximum of 1 comment elements, you can restrict it to require a minimum of 1 comment element.

Here's a summary of the key differences between simple and complex types:

I hope this helps you understand complex type definitions!

2.5.4 AnyType

The anyType is a fundamental concept in complex type definitions. It represents the base type from which all simple and complex types are derived.

The anyType type does not constrain its content in any way, making it a versatile option for elements that require unconstrained content. This means the element value can be a number, a sequence of characters, or even a mixture of characters and elements.

In fact, anyType is the default type when none is specified, so you can use it as a fallback option. For example, an element declared with anyType can contain a value like 423.46, a sequence of characters, or even embedded markup to support internationalization.

Credit: youtube.com, Anytype Foundations: Understand the Basic Anytype Functions

Unconstrained element content is often needed in elements containing prose that requires embedded markup. In such cases, the default declaration or a slightly restricted form of anyType may be suitable. The text type described in Any Element, Any Attribute is an example of a type that can be used for such purposes.

2.8 Groups

Groups are a great way to organize and reuse attribute declarations in complex type definitions. They can contain multiple attribute declarations and even other attribute groups.

You can define an attribute group by listing all the desired attributes in a single place, making it easy to update and maintain. This is similar to parameter entities in XML 1.0.

Attribute groups can be referenced by name in complex type definitions, improving readability and reducing clutter. For example, you can create a named attribute group for an item element and reference it in the item element declaration.

Attribute group references must appear at the end of complex type definitions, along with attribute declarations. This ensures that all the necessary information is included in one place.

Global Definitions

Credit: youtube.com, XML Schema Definition Tutorial

Global definitions can be named and defined at the top level, and then referenced by name elsewhere.

These definitions can contain attributes like default, which gives the default value of the attribute, and fixed, which gives a single value the attribute may be given. The type of the attribute's value is given either by a type attribute of the definition or by a simpleType child element of the definition.

A global element or attribute can be referenced in one or more declarations using the ref attribute. This allows the referenced element to appear in the instance document in the context of the referencing declaration.

Here's a quick rundown of the attributes that can be defined in a global attribute:

  • default: the default value of the attribute
  • fixed: a single value the attribute may be given
  • name: the name of the defined attribute
  • type: the type of the defined attribute's value

Note that global declarations cannot contain references, they must identify simple and complex types directly.

Xsd File Location

You can specify the location of an xsd file using the xsi:schemaLocation attribute. This attribute provides hints to a processor regarding the location of schema documents.

Credit: youtube.com, XML Schema Definition(XSD)

The xsi:schemaLocation attribute value consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member of the pair is a hint describing where to find an appropriate schema document for that namespace.

For example, you can indicate the location of the Report schema to a processor of the Quarterly Report using the xsi:schemaLocation attribute. The attribute value would contain a pair of URI references, one for the namespace and the other for the schema document.

Here are the three circumstances where the xsi:schemaLocation attribute is used:

  • In an instance document, the attribute provides hints from the author to a processor regarding the location of schema documents.
  • In a schema, the include element has a required schemaLocation attribute, and it contains a URI reference which must identify a schema document.
  • Also in a schema, the import element has optional namespace and schemaLocation attributes.

Global Definition

Global definitions can be named and defined at the top level, and then referenced by name elsewhere. This allows for a more organized and reusable approach to defining attributes and elements.

A global definition of an attribute can contain several key elements, including the default value, fixed value, name, and type. The default value is an xs:string that is used for any element in which the attribute can appear but does not.

Two Women in Elegant Blazers Discussing a Business Diagram
Credit: pexels.com, Two Women in Elegant Blazers Discussing a Business Diagram

The fixed value is a single xs:string that is the only value the attribute may be given; the attribute must either appear with that value or not appear. Fixed and default are mutually exclusive.

The name of the defined attribute is also specified, and this is the name by which the definition is referenced. The type of the attribute's value is given either by a type attribute of the definition or by a simpleType child element of the definition.

Here are the key elements of a global attribute definition:

  • default: the default value of the attribute (an xs:string)
  • fixed: a single value (an xs:string) that is the only value the attribute may be given
  • name: the name of the defined attribute
  • type: the type of the attribute's value (given by a type attribute or a simpleType child element)

To reference a global definition of an attribute, you use the ref attribute, which specifies the attribute definition. You can also specify the form of the attribute name, which can be either qualified or unqualified. The default form is set by the schema element's attributeFormDefault attribute.

The form attribute can have one of two values: qualified or unqualified. If the attribute name must be qualified with the namespace when appearing in the element or type, you use the value "qualified". If not, you use the value "unqualified".

Global Definition Reference

Black and White Geometric Representation of Data
Credit: pexels.com, Black and White Geometric Representation of Data

A global definition in XML Schema is a declaration that appears as a child of the schema element, making it available for reference in other declarations. This is done using the ref attribute, which specifies the attribute definition and is required.

The use of the ref attribute can have one of two values: form=qualified or form=unqualified. The former requires the attribute name to be qualified with a namespace when appearing in the element or type, while the latter does not. The default value is set by the schema element's attributeFormDefault attribute.

To reference a global element or attribute, you simply use the ref attribute in the declaration. For example, a declaration that references a global element enables the referenced element to appear in the instance document in the context of the referencing declaration.

Here are some key things to keep in mind when working with global definitions:

  • Global declarations cannot contain references; they must identify simple and complex types directly.
  • Global declarations cannot contain cardinality constraints such as minOccurs, maxOccurs, or use.
  • Global elements and attributes can be referenced in one or more declarations using the ref attribute.
  • The default value for the form attribute is set by the schema element's attributeFormDefault attribute.

By following these guidelines, you can effectively use global definitions in your XML Schema to create reusable and modular code.

Element Content

Credit: youtube.com, Find an XSD Element from an XML Node using Liquid XML Studio 2012

Element content can be defined in various ways. An element can contain other elements, attributes, or a simple type of value.

An element with simple content can have either a default value or a fixed value. This is done using the default or fixed attributes.

An element can also have its contents restricted using key, keyref, or unique elements. These elements can be used to define the allowed content of an element.

An element can have attributes and contain other elements, or it can have attributes and contain only a simple type of value. In the latter case, the attribute type is simply a definition of the set of values that attribute can be given.

Here are some examples of element content:

  • Elements with simple content can have a default value or a fixed value.
  • Elements can have their contents restricted using key, keyref, or unique elements.
  • Elements can have attributes and contain other elements, or they can have attributes and contain only a simple type of value.

Any Element, Any

The any element is a flexible mechanism that enables content models to be extended by any elements and attributes belonging to specified namespaces. This is useful for creating complex types that can contain a wide range of elements and attributes.

A Black Tablet with Diagram on Screen
Credit: pexels.com, A Black Tablet with Diagram on Screen

For example, the text type can contain an unrestricted mixture of character content and element content from any namespace, such as Ruby annotations. It also allows an optional xml:lang attribute.

The lax value of the processContents attribute instructs an XML processor to validate the element content on a can-do basis. This means it will validate elements and attributes for which it can obtain schema information, but not signal errors for those it cannot obtain any schema information.

The any element can be specified with a namespace attribute, which can be set to any of the values listed in Table 4. This allows you to constrain the number of elements that may appear in an element.

In addition to the any element, there is a corresponding anyAttribute element that enables attributes to appear in elements. This is useful for permitting attributes from external namespaces to appear in an element.

For example, adding anyAttribute to the declaration of an element, such as htmlExample, permits any XHTML attribute to appear in that element. This means an attribute like href can appear in the htmlExample element.

The anyAttribute element cannot constrain the number of attributes that may appear in an element, unlike the any element.

Curious to learn more? Check out: Http 302 Redirect Example

Mixed Content

Credit: youtube.com, How to Replace Element Value with Mixed Content Using Python's lxml Library

Mixed content is a unique aspect of XML Schema that allows character data to appear alongside subelements. This is in contrast to the deepest subelements, where character data is confined.

To illustrate this, consider the customer letter snippet from the article, where text appears between elements and their child elements. This is made possible by setting the mixed attribute on the type definition to true.

The XML Schema mixed model differs fundamentally from the mixed model in XML 1.0. Under the XML Schema mixed model, the order and number of child elements appearing in an instance must agree with the order and number of child elements specified in the model.

This provides full validation of mixed models, in contrast to the partial schema validation provided by XML 1.0.

Here's a summary of the differences between simple and complex types:

This table highlights the key differences between simple and complex types, which is essential to understand when working with mixed content.

Simple Facets

An artist’s illustration of artificial intelligence (AI). This image visualises an artificial neural network as physical objects. The complex structure represents a network of information ...
Credit: pexels.com, An artist’s illustration of artificial intelligence (AI). This image visualises an artificial neural network as physical objects. The complex structure represents a network of information ...

Element content can be broken down into simple facets. A single element can have multiple facets, making it a versatile and dynamic part of your design.

A facet is essentially a characteristic or property of an element. For example, color is a facet of an element, as it can be red, blue, green, or any other color.

C Using Entities

Using entities can be a powerful way to simplify instance documents and improve schema validation.

Entities are named fragments of content that can be used in the construction of both DTDs and instance documents. They can be declared in instance documents and referenced in element content.

For example, an entity called "eacute" can be declared in an internal subset of a DTD and referenced in the content of a city element. This will cause the entity to be resolved before schema validation takes place.

A schema processor will determine the validity of the city element using the resolved entity, in this case, the single character "é".

An artist's illustration of artificial intelligence (AI). This image represents storage of collected data in AI. It was created by Wes Cockx as part of the Visualising AI project launched ...
Credit: pexels.com, An artist's illustration of artificial intelligence (AI). This image represents storage of collected data in AI. It was created by Wes Cockx as part of the Visualising AI project launched ...

You can achieve a similar outcome by declaring an element in a schema and setting its content appropriately. This will cause the schema processor to supply the single character "é" for the contents of the element.

However, using entities can complicate string matching, as the two forms of the name "Montréal" given in the two examples above will not match each other using normal string-comparison techniques.

Constraints and Validation

In XML Schema, we can specify that attribute or element values must be unique within a certain scope using the unique element.

This allows us to indicate that a particular attribute or element value is unique, and we use the unique element to select a set of elements and identify the attribute or element field that has to be unique.

For example, in the report schema, the selector element's xpath attribute contains an XPath expression, r:regions/r:zip, that selects a list of all the zip elements in a report instance.

Credit: youtube.com, Mastering Check Constraints in SSMS: How to Add and Manage Data Validation Rules

The field element's xpath attribute contains a second XPath expression, @code, that specifies that the code attribute values of those elements must be unique.

The XPath expressions limit the scope of what must be unique, so a report might contain another code attribute, but its value does not have to be unique because it lies outside the scope defined by the XPath expressions.

We can also indicate combinations of fields that must be unique by using multiple field elements to identify all the values involved.

Intriguing read: Http Redirect Code

Union

A union element defines a new simple type that is the union of two or more other simple types. This new type consists of everything that the component types comprise.

The types may be listed by name in the memberTypes attribute, or defined in the contents of the union element, or both.

Uniqueness Constraints

XML Schema enables us to indicate that any attribute or element value must be unique within a certain scope.

Credit: youtube.com, SQL UNIQUE Constraint: Ensuring Data Integrity in Databases 🛡️

This is done using the unique element, which first selects a set of elements and then identifies the attribute or element field that has to be unique within the scope of the selected elements.

The selector element's xpath attribute contains an XPath expression that limits the scope of what must be unique.

In our report schema, the selector element's xpath attribute contains an XPath expression, r:regions/r:zip, that selects a list of all the zip elements in a report instance.

Note that the XPath expressions limit the scope of what must be unique, and values outside this scope do not have to be unique.

For example, in our report schema, the report might contain another code attribute, but its value does not have to be unique because it lies outside the scope defined by the XPath expressions.

We can also indicate combinations of fields that must be unique, such as a unique combination of part number and product name for each item element.

To define combinations of values, we simply use multiple field elements to identify all the values involved.

Readers also liked: Redirect Chain Contains Http

Lexical vs. Value Spaces

Diagram on White Background
Credit: pexels.com, Diagram on White Background

When working with XML Schema, it's essential to understand the distinction between lexical and value spaces. This distinction can be crucial in ensuring that your constraints and validation rules are applied correctly.

Ordinarily, there's no need to keep this distinction in mind, but for many types, a single value can be represented by more than one string. For example, a single xs:integer is represented by '1', '+1', and '01'.

Restriction by a regular expression acts on the lexical space, not the value space. This means that if you're using a regular expression to derive a type, you need to be careful that the two spaces are one-to-one.

A single xs:normalizedString can be represented by 'normalized string' and 'normalized string'. This highlights the importance of being aware of the differences between lexical and value spaces.

It's best to avoid deriving types by regular expression for which the two spaces are not one-to-one, as it makes confusion likely. This is because the regular expression will be acting on the lexical space, but the derived type will be considered in the context of the value space.

Take a look at this: Azure Instance Types

Nil Values

Credit: youtube.com, Understanding @Null Constraint Validation in Programming: A Comprehensive Guide

Nil values can be a problem in XML documents, as the absence of an element doesn't necessarily mean anything.

In the context of purchase orders, for example, the absence of a shipDate element might indicate that the item hasn't been shipped yet. However, it's often more desirable to represent unknown or inapplicable information explicitly.

XML Schema's nil mechanism allows you to signal that an element is nil by using an attribute, rather than relying on its absence. This is done through an "out of band" nil signal, which means there's no actual nil value that appears as element content.

To illustrate this, consider modifying the shipDate element declaration to include the nil attribute. This attribute can be set to true in the instance document to explicitly represent that the shipDate has a nil value.

Namespaces and Qualification

A namespace is a unique identifier for an XML Schema, allowing it to be distinguished from others. It's defined in the xsd file using the targetNamespace attribute.

Credit: youtube.com, custom object has xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> for some developers and...

The target namespace plays a crucial role in identifying which element and attribute declarations in the schema should be used to check the instance document.

In XML Schema, the author has the option to decide whether locally declared elements and attributes in an instance document must be qualified by a namespace. This choice affects the structures of schemas and instance documents.

Elements and attributes that must be qualified appear prefixed in instance documents, whereas those that don't require qualification appear without prefixes. Attributes that must be qualified, like xsi:nil, must be explicitly prefixed.

The qualification mechanism can be controlled on a declaration-by-declaration basis using the form attribute. This allows specific attributes or elements to be required to be qualified, overriding the default qualification setting.

In cases where a schema is designed without a target namespace, it's recommended to explicitly qualify XML Schema elements and types with a prefix, such as xsd:. This helps avoid confusion between XML Schema types and user-defined types.

Namespaces & Qualification

Credit: youtube.com, Lesson 101 - Components and Root Namespaces

Namespaces are unique identifiers for XML Schemas, allowing them to be distinguished from one another. They are defined in the xsd file using the targetNamespace attribute.

A namespace is defined as a URI, but this is only an identifier and not a physical location. The namespace is not a file path, but rather a unique value that identifies the schema.

In the Jakarta Servlet 6.0 example, the web-app_6_0.xsd file defines a namespace using the targetNamespace attribute. This namespace is used to identify the schema and its elements.

Namespaces can be used to avoid naming conflicts between different schemas. If two schemas have the same name for a type, element, or attribute, but are in different namespaces, there is no conflict.

In the purchase order schema example, the schema does not declare a target namespace, so the definitions and declarations from that schema are referenced without namespace qualification. This can lead to naming conflicts if the schema is used with other schemas that have the same names.

To avoid naming conflicts, it's recommended to use a namespace for all XML Schema elements and types. This can be done by explicitly qualifying the elements and types with a prefix such as xsd:.

Extension

Credit: youtube.com, What are XML Namespaces? And how do you work with them?

In order to create a new type by adding elements and/or attributes to a simple or complex type, you need to use an extension element. The type to extend must be defined at the top level and have a name.

The type to extend is identified using the extension element's base attribute. This attribute is crucial in linking the extension to the parent type.

Elements are added using a group, all, choice, or sequence element in the contents of the extension. These elements allow you to define the structure of your new type.

Attributes are added using attribute and/or attributeGroup elements or an anyAttribute element in the contents of the extension. This gives you the flexibility to customize your type as needed.

Here are the basic steps to follow when creating an extension:

  • The type to extend must be defined at the top level and have a name.
  • The type to extend is identified using the extension element's base attribute.
  • Elements are added using a group, all, choice, or sequence element in the contents of the extension.
  • Attributes are added using attribute and/or attributeGroup elements or an anyAttribute element in the contents of the extension.

5.6 Location

The schemaLocation attribute is used to provide hints to a processor about the location of schema documents. This attribute is used in three circumstances: in an instance document, in a schema, and in an import element.

A person analyzes financial data and diagrams on a laptop and paper at a desk, highlighting office work.
Credit: pexels.com, A person analyzes financial data and diagrams on a laptop and paper at a desk, highlighting office work.

In an instance document, the schemaLocation attribute value consists of one or more pairs of URI references, separated by white space. The first member of each pair is a namespace name, and the second member is a hint describing where to find an appropriate schema document for that namespace.

The presence of these hints does not require the processor to obtain or use the cited schema documents, and the processor is free to use other schemas obtained by any suitable means, or to use no schema at all.

Here's a breakdown of the schemaLocation attribute:

  • In an instance document, it provides hints for the locations of schema documents.
  • In a schema, it's used to compose a final effective schema by merging the declarations and definitions of the including and the included schemas.
  • In an import element, it provides a hint from the author to a processor regarding the location of a schema document.

Note that the schemaLocation is only a hint and some processors and applications will have reasons to not use it.

Importing

Importing is a crucial part of working with XML Schema, and it's essential to understand how it works.

In a schema, the include element has a required schemaLocation attribute, which contains a URI reference that must identify a schema document. This attribute is used to compose a final effective schema by merging the declarations and definitions of the including and the included schemas.

Credit: youtube.com, Import Schema

The effect of using the include element is to add the type definitions, attribute and local element declarations from the included schema to the element declarations of the including schema. For example, in Advanced Concepts II: The International Purchase Order, the type definitions of Address, USAddress, and UKAddress from address.xsd were added to the element declarations of purchaseOrder and comment.

The include element is used to import components from another schema, and it's essential to note that the schemaLocation attribute is only a hint and some processors and applications will have reasons to not use it.

Here are some key points to keep in mind when using the include element:

  • The include element has a required schemaLocation attribute.
  • The schemaLocation attribute contains a URI reference that must identify a schema document.
  • The include element is used to compose a final effective schema by merging the declarations and definitions of the including and the included schemas.

Additionally, the import element has optional namespace and schemaLocation attributes. If present, the schemaLocation attribute is understood in a way that parallels the interpretation of xsi:schemaLocation, providing a hint from the author to a processor regarding the location of a schema document that the author warrants supplies the required components for the namespace identified by the namespace attribute.

5.7 Conformance

Credit: youtube.com, Lecture: INFOST 780 W3C XML Schema for Data Interchange

Conformance in XML Schema instance documents is crucial for ensuring that documents are properly validated against the schema.

To achieve conformance, a document must be in a valid XML document, as shown in the example of a valid XML document in section 4.2.2.

The document must also have a root element that matches the target namespace of the schema, as seen in the example of a simple XML document in section 4.2.3.

The document's elements and attributes must be correctly declared and used, following the rules outlined in section 4.4.2.

The document's structure and organization must also adhere to the schema's structure and organization, as demonstrated in the example of a more complex XML document in section 4.4.3.

Conformance checking can be performed using tools such as the W3C's XML Schema Validator, which is mentioned in section 4.5.

Frequently Asked Questions

How to open XML Schema file?

To open an XML Schema file, right-click the file and select "Open" in the context menu. This will launch the XML Schema editor where you can view or edit the data.

How do I generate an XML instance from XSD?

To generate an XML instance from an XSD file, right-click the global element in the XML Schema Explorer and select "Generate Sample XML". This will create a sample XML document based on the XSD schema.

Patricia Dach

Junior Copy Editor

Patricia Dach is a meticulous and detail-oriented Copy Editor with a passion for refining written content. With a keen eye for grammar and syntax, she ensures that articles are polished and error-free. Her expertise spans a range of topics, from technology to lifestyle, and she is well-versed in various style guides.

Love What You Read? Stay Updated!

Join our community for insights, tips, and more.