Defining Elements and Attributes in XML Schemas

You can define an element or attribute as part of a complexType. You can also define an element or an attribute as a top-level item. In other words, in the XML document that defines the XML Schema, the element or attribute is a child of the xsd:schema element. An element or attribute that is an immediate child of the xsd:schema element is a global element or attribute.

A global element or attribute cannot

  • Contain a reference to another element or attribute
  • Specify values for the minOccurs, maxOccurs, or use properties

This section covers the following topics:

Defining Elements That Carry Attributes and Contain Data in XML Schemas

You might want to define an element that carries attributes and contains data, but does not contain subelements. In the purchaseOrder.xsd document, an example of this is the internationalPrice element, shown here in the Diagram tab.

Figure 254. internationalPrice Element in purchaseOrder.xsd

This element has a currency attribute, and it contains data based on the xsd:decimal simpleType.

Diagram View

To define a complex type that contains only attributes:
1. Right-click the schema node .
2. In the shortcut menu, select Add > Element.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > Element menu and from the Add button .

The new element is added to the XML Schema. It is displayed in the diagram and in the text pane (if you have it open). The properties for the new element are displayed in the Properties window.

3. Create a complexType of the element - right-click the element and select Add > ComplexType.
4. Make sure the new complexType is selected.
5. Click the QuickEdit button and select Derive by extension or Derive by restriction. These choices let you extend or restrict a base simpleType.

The Type Derivation dialog box appears.

Figure 255. Type Derivation Dialog Box

6. Expand the W3C XML Schema and select the simpleType on which you want to base the data allowed by the complexType.
7. Click OK.

The XML Schema is updated with the element's new definition. Figure 256 shows an extension of the decimal simpleType.

Figure 256. complexType with simpleContent Defined

The simpleContent node ( ) specifies that the complexType can contain only data and attributes. It cannot contain subelements.

8. To add an attribute, right-click the element and select Add > Attribute.

Tree View

To define an element that contains raw data and carries attributes:
1. Click the Schema node.
2. In the left tool bar, click New Element Definition . In the Tree view, Stylus Studio displays a field for the new element definition.
3. Type the name of the element and press Enter twice. If you press Enter once, Stylus Studio displays a pop-up menu that lists the possible types for the new element. You need to define a new type, so you cannot select from this list. If the pop-up menu does appear, press Enter or click outside the menu. You should now have a named element with no type specified.
4. In the left tool bar, click New complexType . In the Tree view, Stylus Studio displays a field for the new complexType.
5. In the left tool bar, click New Content . Stylus Studio displays a drop-down list.
6. In the drop-down list that appears, double-click simpleContent. This is the Content Type property. When the content type is simpleContent, the complexType you are defining can contain data and attributes. It cannot contain subelements.
7. If you want the contained data to be one of the simpleTypes already defined with no restrictions, click New Extension in the left tool bar.

Stylus Studio displays a scrollable, drop-down list of the simpleTypes built in to XML Schema and previously defined in the current schema.

8. If you clicked New Extension, double-click the type of the data you want this element to contain. Go to step 9.

If you clicked New Restriction, follow these steps:

a. Double-click the simpleType whose values you want to restrict.
b. In the left tool bar, click New Facet . Stylus Studio displays a pop-up menu.
c. Double-click the type of facet you want to specify.
d. In the Properties window, double-click the Value field.
e. Enter a value for the new facet.
f. To add another facet, click the restriction node for the simpleType, and repeat step b.
9. In the left tool bar, click the complexType node that you created in step 4.
10. In the left tool bar, click New Attribute Definition . In the Tree view, Stylus Studio displays a field for the new attribute definition.
11. Type a name for the new attribute and press Enter. Stylus Studio displays a scrollable, drop-down list of the possible types for the new attribute.
12. Double-click the attribute type. If you want to, specify a value for the attribute's Default or Fixed Value property in the Properties window.
13. To add additional attributes, repeat step 9 through step 12.

Defining Elements That Contain Subelements in XML Schemas

An element that contains subelements is a complexType. Consequently, you can define an element that contains subelements in either of the following ways:

  • Define a top-level complexType. That is, it is a child of the xsd:schema node. In the complexType definition, define the subelements. Elsewhere in the schema, define an element that uses the complexType you defined.
  • Define an element that is a child of the xsd:schema node or a Model Group node. In the element definition, define a complexType that contains your subelements.

To define a complexType that contains elements, see Defining complexTypes That Contain Elements and Attributes - Diagram View or Defining complexTypes That Contain Elements and Attributes - Tree View.

Diagram View

To define an element and define subelements in the element definition:
1. Right-click the schema node .
2. In the shortcut menu, select Add > Element.

Alternatives: This operation is also available from the XMLSchema > Diagram > Add > Element menu and from the Add button .

The new element is added to the XML Schema. It is displayed in the diagram and in the text pane (if you have it open). The properties for the new element are displayed in the Properties window.

3. Right-click the new element and click QuickEdit. Select one of the following from the QuickEdit menu:
    • Add Elements Sequence
    • Add Elements Choice
    • Add Elements All
    • Add Elements Any
    • Stylus Studio updates the element definition to include a complexType with the sequence, choice, all, or any element you selected in the previous step.

4. Add subelements to the element you created in step 3.

Tree View

In the Tree view, to define an element and define subelements in the element definition:
1. Click the Schema node or a Model Group ( all, any, choice, sequence) node.
2. In the left tool bar, click New Element Definition .
3. Enter the name for your new element. Stylus Studio displays a pop-up menu that lists the built-in simpleTypes and any simple or complexTypes already defined in your schema.
4. Press Enter again. Rather that using a type that is already defined, you want to define a new complexType in the definition of your element. You do not want to assign a type to your new element.
5. In the left tool bar, click New complexType .
6. Enter a name for the new type.

At this point, the procedure for defining a complexType in the definition of an element is the same as defining a complexType as a child of the Schema node. See Defining complexTypes That Contain Elements and Attributes - Tree View.

Adding an Identity Constraint to an Element

XML Schemas provide a feature that is similar to the DTD ID identity constraint. In a DTD, the value of an ID attribute must be unique within an XML document. In XML Schemas, the type of an identity constraint can be unique, key, or keyref. You use XPath expressions to define the scope of the constraint.

You associate an identity constraint with an element.

  • A unique identity constraint forces the result of evaluation of an XPath expression to be unique. Stylus Studio evaluates the XPath expression against the element for which you define the identity constraint. If the element is present, the result must be unique among the children of that element.
  • A key identity constraint specifies that the fields that form the expression must be present in all instance documents. For example, if a key is based on date and number attributes, the date and number attributes must always be specified.
  • A keyref identity constraint is equivalent to the IDREF attribute in DTDs. It specifies that the contents of a field in the instance document is the value of a key that is defined in another document. For example, a Quote document would have a reference to the RFQ that originated it.

This section covers the following topics:

Example of an Identity Constraint

This topic provides an example of an element with an identity constraint. Introductory information about identity constraints is in Adding an Identity Constraint to an Element. See also Tree View.

Suppose you define the following element in an XML Schema:

<element name="purchaseReport"> 
               

              
<complexType> 
                   
<sequence> 
                   

                
<element name="parts"> <complexType> <sequence> <element name="part" maxOccurs="unbounded"> <attribute name="number" type="SKU"/> <attribute name="vendor" type="xs:string"/> <attribute name="quantity" type="integer"/> </element> </sequence> </complexType> </element>
</sequence> 
                   
</complexType> 
                   

                
</element>

In an XML document that uses this schema, you could define the following elements:

<purchaseReport> 
               

              
<parts> 
                   
<part number="00-01-02" vendor="IBM" quantity="10"/> 
                   
<part number="01-01-02" vendor="BEAS" quantity="1"/> 
                   
... 
                   
</parts> 
                   

                
</purchaseReport>

If you want to enforce that there is just one part element for each product that has been purchased, you add the following to the previous XML Schema example:

... [previous definition] 
               

              
</sequence> 
                   
</complexType>
                   
<unique name="pNumKey"> 
                   
<selector xpath="parts/part"/> 
                   
<field xpath="@number"/> 
                   
<field xpath="@vendor"/> 
                   
</unique>
                   

                
</element>

The schema validator starts with an initial context set that contains purchaseReport elements. It runs the XPath expression parts/part to obtain the data set to be checked. In this example, this is the two part elements. The schema validator then gathers the values from the number and vendor attributes, and builds a key from these values. It then uses the key to check that there are no part elements that have the same tuple.

Diagram View

To specify an identity constraint:
1. Right-click the element for which you want to specify the identity constraint.
2. Select Add > and then Key, KeyRef, or Unique from the menu.
3. Right-click the new identity constraint, and select Selector.
4. In the Properties window, specify the XPath expression that identifies the set of elements to which the identity constraint applies.
5. Return to step 3 and select Field.
6. In the Properties window, specify the XPath expression that identifies the element or attribute for each element identified by the selector element that has to be unique.

Tree View

This topic provides the procedure for specifying an element with an identity constraint. Introductory information about identity constraints is in Adding an Identity Constraint to an Element. See also Example of an Identity Constraint.

To specify an identity constraint:
1. Click the element for which you want to specify the identity constraint.
2. In the XML Schema left-side tool bar, click .
3. In the drop-down list that Stylus Studio displays, double-click unique, key, or keyref.
4. In the Properties window, double-click the Name field and enter a name for the identity constraint.
5. If you selected keyref, then in the Properties window, double-click the Refer field and enter the name of the key definition.
6. In the tree representation, click the identity constraint you just defined.
7. In the left tool bar, click New Selector/Key .
8. In the drop-down list that Stylus Studio displays, double-click selector. You must define exactly one selector for each identity constraint.
9. In the Properties window, double-click the XPath Expression field and enter an XPath expression that returns the element for which you are specifying a constraint.
10. Click the unique, key, or keyref identity constraint you defined in Step 3.
11. In the left tool bar, click New Selector/Key .
12. In the drop-down list that Stylus Studio displays, double-click field. You must define one or more fields for each identity constraint. A field can be whatever the XPath expression (defined in the next step) retrieves.
13. In the Properties window, double-click the XPath Expression field and enter an XPath expression that returns the element or attribute that is the key or one of the keys for the constraint. XPath expressions associated with fields return the data that define the key for each element returned by the selector XPath expression.
14. Repeat step 10 through step 13 for each additional key field.
 
Free Stylus Studio XML Training: