Editing and validating XML files sample details

These samples illustrate how you can use some of the guided editing features in the XML Editor to edit and validate XML files. For more information about editing XML files with DTD or XML Schema constraints, refer to the XML Editor online help.

Example Description
anyElement

This is an advanced example that shows how you can use the any element from the XML Schema spec to extend the content model of an XML document.

Editing PurchaseOrder.xml

Shows how an XML Schema file (PurchaseOrder.xsd) can be used to provide editing assistance when editing an XML file (PurchaseOrder.xml).

The international directory shows how to build derived type and to use the xsi:type attribute in the XML instance document.

The report.xsd example shows how to work with multiple schemas from different namespaces.

Using Substitution Groups Shows how an XML Schema allows named groups of elements to be substituted for other elements.

anyElement

This sample shows how you can use the any element in XML Schema to create an extensible content model in your XML document. The sample is divided into 2 directories:
  1. anyNamespace - shows how to use the ##any namespace
  2. specifiedNamespace - shows how to use a specific namespace

Using the ##any namespace

The Book.xsd schema under the anyNamespace directory has a complex type BookType. The content of the BookType is title, author, year, isbn, and then optionally followed by any well-formed XML as indicated by the any element: <any namespace="##any" minOccurs="0" maxOccurs="unbounded"/>

This means that in an instance document, we can insert any XML element to extend the definition of the BookType. In this sample, we have two other schemas: Publisher.xsd and Reviewer.xsd. We will use them to extend the definition of BookType in our instance documents. Note that these two schemas are in different namespaces.

BookRev.xml is associated with both Book.xsd and Reviewer.xsd through the xsi:schemaLocation attribute. It shows how you can extend the BookType with a reviewer element to provide review comments for the book.
  • In the Design View of the XML Editor, select the opensource:reviewer element. From the pop-up menu, select Add Child > Comment to add a comment for the reviewer.
  • Run Validate - the document is valid, since the reviewer element conforms to constraint that is defined in the Reviewer.xsd file.
BookPub.xml is associated with both Book.xsd and Publisher.xsd through the xsi:schemaLocation attribute. It shows how you can extend the BookType with a company element to provide publisher information for the book.
  • In the Design View of the XML Editor, select the pub:company element. From the pop-up menu, select Remove to remove it.
  • Run Validate - the document is valid since the any element is optional.

Using a specific namespace

The Book.xsd schema under the specifiedNamespace directory has a complex type BookType. The content of the BookType is title, author, year, isbn, and then optionally followed by any well-formed XML that belongs to the namespace http://www.wesley.com as indicated by the any element: <any namespace="http://www.wesley.com" minOccurs="0" maxOccurs="unbounded"/>

This means that in an instance document, we can insert any XML element to extend the definition of the BookType, provided that they belong to the namespace http://www.wesley.com. In this example, the Reviewer.xsd file belongs to this namespace.

Book.xml is associated with both Book.xsd and Reviewer.xsd through the xsi:schemaLocation attribute. It shows how you can extend the BookType with a reviewer element to provide review comments for the book.

For Book.xml, in the Design View of the XML Editor, select the rev:reviewer element. From the pop-up menu, select Add Child > Comment to add a comment for the reviewer. Run Validate to make sure the document is valid.

Generating instances from schemas with an any element

You can use the New XML Wizard to create an instance of an XML schema. If the schema contains an element with a specified namespace uri (such as BookType from Book.xsd in the specifiedNamespace directory) the wizard will prompt you to enter the schema locations for these namespaces.
  • In the Navigator view, select the Book.xsd object in the "specifiedNamespace" directory. Right click on the Book.xsd and select Generate > XML File to invoke the New XML Wizard.
  • On the first page of the wizard specify the new file name as Book1.xml. Click Next.
  • You will notice that XML Schema Information group box contains a table that shows 2 namespaces. The first is the namespace that corresponds to Book.xsd. The second is the namespace that is referenced by the any element within Book.xsd.
  • Click Finish. A message is displayed to warn you that you haven't specified a location for the 'http://www.wesley.com'
  • Click No.
  • Select the 'http://www.wesley.com' namespace in the table and click Edit.
  • Click Browse and select the Publisher.xsd file from the anyElement/anyNamespace directory.
  • You will notice that a company element for the 'http://www.wesley.com' was generated as a child of the book element from the 'http://www.ibm.com' namespace.

The New XML Wizard prompted you for the schema locations that are required to generate correct content. If you repeat the steps above but choose not to enter a schema location for the 'http://www.wesley.com' namespace, you will notice that an ANY-ELEMENT place holder element will be generated instead of the company element. You will be required to modify the ANY-ELEMENT to include the elements that you desire.

Adding namespaces to an XML document

The XML Editor provides a convenient mechanism for adding more namespaces to an XML document. Adding namespaces manually (by typing in the source view) can be error prone; schema namespace URIs can be easily misspelled and the schema location attribute syntax can be tricky. A helpful dialog is provided to assist in the task. From the Outline or Design View, you can access the Edit Namespaces dialog to assist in this task.
  • Open the BookPub.xml file under the anyElement/anyNamespace directory.
  • Right click on a root element and select the Edit Namespaces menu item.
  • Click Add to invoke a dialog to enter the information for a new namespace.
  • Select Specify New Namspace. Click Browse and locate the Reviewer.xsd file under the anyNamespace directory.
  • Fill in Prefix and Namespace Name fields.
  • Click OK and OK again. You will notice that a xmlns:openSource attribute has been added and the xsi:schemalocation attribute value has been updated.
  • Now that the namespace has been added you will notice that when you right click on the pub:company element and choose the Add Before or Add After pop-up, you can add elements from Reviewer.xsd namespace (i.e. http://www.apache.com).

Editing PurchaseOrder.xml

The PurchaseOrder.xsd file provides the rules for defining the content of the PurchaseOrder.xml file. It is associated with the PurchaseOrder.xml file through the xsi:schemaLocation attribute.

Occurrence constraints

In PurchaseOrder.xsd, the item element (under complex type Items) has an optional shipDate element with minOccurs=0:
		<element name="shipDate" type="date" minOccurs="0"/>

For PurchaseOrder.xml, in the Design View of the XML Editor, select the first item under the items element. Right-click it to select Add Child > shipDate to add the shipDate to the item element. The shipDate element with a value set to the current date is added. Notice that the Add Child pop-up menu no longer has the shipDate option, as the constraint of having at most one shipDate element is satisfied.

If you select the shipDate element and choose Remove from the pop-up to remove it, then the Add Child > shipDate option will be available again.

Enumerated element type

In PurchaseOrder.xsd. the USAddress complex type has an element state, whose type is a simple type USState
		<element name="state" type="po:USState"/>
The USState simple type is derived from string with an enumeration facet as follows:
		<simpleType name="USState"> 
			<restriction base="string">
				<enumeration value="CA"></enumeration>
 				<enumeration value="PA"></enumeration>
				<enumeration value="AR"></enumeration>
			</restriction>
		</simpleType>

For PurchaseOrder.xml, in the Design View of the XML Editor, select the state text field under the shipTo element. A list will appear where you can select among the three values (CA, PA, and AR), limited by the enumeration values in the USState simple type.

Using Derived Types in Instance Documents (xsi:type attribute)

The ipo.xml file shows how goods can be shipped to the UK and the bill is sent to a US address. In ipo.xsd, billTo and shipTo elements are defined as Address types:
		<element name="shipTo" type="ipo:Address"/>
		<element name="billTo" type="ipo:Address"/>

In address.xsd, two complex types USAddress and UKAddressextend the base type Address.

The instance document, ipo.xml, can choose to use different derivation of Address (e.g. USAddress) by using an xsi:type attribute.

In the Design View of the XML Editor, select the xsi:type attribute under the shipTo element. Notice how a list is available to let you choose the Address type that you want to use. The guided assistance will be based on the type that you choose in the combo-box.

Working with multiple schemas and namespaces

The Quarterly report example shows how to work with multiple schemas from different namespaces. It includes the following files under PurchaseOrder/international folder: address.xsd, ipo.xsd, report.xsd and report.xml.

As schemas become larger, it is often desirable to divide their content among several schema documents for purposes such as ease of maintenance, reuse, and readability. XML Schema defines two constructs to support this: include and import. The include element brings in definitions and declarations from the included schema into the current schema. It requires the included schema to be in the same target namespace as the including schema. The import element behaves in a similar way, with the exception that the imported schema can come from a different namespace.

For more information on include and import, refer to the XML schema editor online help.

Using Substitution Groups

The Catalogue.xsd file declares Book and Magazine to be in a substitution group with Publication:
		<element name="Book" type="Catalogue:BookType"
				substitutionGroup="Catalogue:Publication"/>
		<element name="Magazine" type="Catalogue:MagazineType"
				substitutionGroup="Catalogue:Publication"/>

The content of Catalogue can be any element that is in the substitution group. In the Design View of the XML Editor, select the Add Child menu on the Catalogue element. Notice how both Book and Magazine can be substituted as the content of the Publication element. In addition, we have declared the Publication element as abstract, to prevent the Publication element to be used directly in the instance document.

Parent topic: Editing and validating XML files

Feedback