attributeGroup

We discussed earlier how you can make use of the ref attribute to make a reference to an attribute declaration that is declared globally within your schema:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:attribute name="sku" type="xs:string"/>

  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="item">
          <xs:complexType>
            <xs:attribute ref="sku"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

References are one way to organize your attributes and to reuse attributes—another way is the attribute group.

An <attributeGroup> allows you to declare attributes together within a common structure, and then reference that structure in place of individual attribute declarations.

For example, let's say you had a number of attributes:

<attribute name="sku" type="xs:string"/> 
<attribute name="price" type="xs:string"/>
<attribute name="size" type="xs:string"/>
<attribute name="color" type="xs:string"/>

You could add these all to an element with four different references. However, there is an easier way—declare the attributes within an <attributeGroup>:

<attributeGroup name="itemInfoGroup"> 
  <attribute name="sku" type="xs:string"/>
  <attribute name="price" type="xs:string"/>
  <attribute name="size" type="xs:string"/>
  <attribute name="color" type="xs:string"/>
</attributeGroup>

Now, the entire set of attributes can be referred to in an element declaration using the name of the group:

<?xml version="1.0" encoding="utf-8"?> 
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:attributeGroup name="itemInfoGroup">
    <xs:attribute name="sku" type="xs:string"/>
    <xs:attribute name="price" type="xs:string"/>
    <xs:attribute name="size" type="xs:string"/>
    <xs:attribute name="color" type="xs:string"/>
  </xs:attributeGroup>

  <xs:element name="catalog">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="item">
          <xs:complexType>
            <xs:attributeGroup ref="itemInfoGroup"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>

</xs:schema>

With the attributeGroup included with the element by referencing the group definition by name, we could have the following XML:

<?xml version="1.0"?> 
<catalog xsi:noNamespaceSchemaLocation="catalog2.xsd" xmlns:xsi="http://www.w3.org/2001
/XMLSchema-instance">
  <item sku="012345" price="1.25" size="Medium" color="blue"/>
</catalog>

There are several advantages to using an attribute group in this way. First, it allows you to declare a group of attributes with a global scope. This allows you to reuse the attributes as much as you need to, with more than just one element, simply with a reference to the attribute group.

Second, this can make your element declarations easier to read, by allowing you to use multiple attributes in the element declaration with a single reference.

The <attributeGroup> element itself accepts three attributes:

  • id

  • name

  • ref

The id attribute allows you to specify an ID for the attribute group. The name attribute specifies the name of the group, which is then used when referencing the attribute group. Finally, you can actually reference other attribute groups within an attribute group, which is why <attributeGroup> also accepts the ref attribute.

As for the content of the <attributeGroup> element itself, it may contain <attribute> declarations, as we saw in the previous example. It may also contain other attributeGroup elements, as references to create a group of groups.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.165.247