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. |
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.
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
to add a comment for the reviewer. Run Validate to make sure the document is valid.Generating instances from schemas with an any element
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 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
<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 pop-up menu no longer has the shipDate option, as the constraint of having at most one shipDate element is satisfied.
to add the shipDate to the item element. The shipDate element with a value set to the current date is added. Notice that theIf you select the shipDate element and choose Remove from the pop-up to remove it, then the option will be available again.
Enumerated element type
<element name="state" type="po:USState"/>
<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)
<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.
<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.