2

I have such elements as Laptop, PC, Printer. All of them should have the same attributes, for example, price and model. Is it possible to declare attribute list just once and include it into 3 elements?

Konstantin Milyutin
  • 11,946
  • 11
  • 59
  • 85

1 Answers1

2

In general, people don't qualify attributes; to reuse them without namespace, and particularly in a list, you use an attributeGroup or as part of a common base type.

This is an XSD that shows the different possibilities you have:

<?xml version="1.0" encoding="utf-8" ?>
<!-- XML Schema generated by QTAssistant/XSD Module (http://www.paschidev.com) -->
<xsd:schema targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns="http://tempuri.org/XMLSchema.xsd" elementFormDefault="qualified" xmlns:xsd="http://www.w3.org/2001/XMLSchema">

    <xsd:attributeGroup name="Priced">
        <xsd:annotation>
            <xsd:documentation>
                Allows reuse of common attributes in components 
                that are not of the same kind.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:attribute name="price" type="xsd:decimal"/>
        <xsd:attribute name="model" type="xsd:string" use="required"/>
    </xsd:attributeGroup>

    <xsd:element name="Printer">
        <xsd:annotation>
            <xsd:documentation>             
                How to reference an attribute group.
                Here Printer is considered in a different
                type hierarchy, so it shares the Priced attributes
                only.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:sequence/>             
            <xsd:attributeGroup ref="Priced"/>
        </xsd:complexType>
    </xsd:element>

    <xsd:complexType name="Base">
        <xsd:annotation>
            <xsd:documentation>
                If Printer, PC, and Laptop are ultimately a
                "Base", and "Priced" is applicable to the "Base"
                type only, then you don't need an attribute                 
                group; reuse through base type.
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence/>         
        <xsd:attribute name="price" type="xsd:decimal"/>
        <xsd:attribute name="model" type="xsd:string" use="required"/>
    </xsd:complexType>

    <xsd:complexType name="AnotherBase">
        <xsd:annotation>
            <xsd:documentation>
                Another base type; here reuse the attribute group.              
            </xsd:documentation>
        </xsd:annotation>
        <xsd:sequence/>         
        <xsd:attributeGroup ref="Priced"/>
    </xsd:complexType>

    <xsd:element name="Laptop">
        <xsd:annotation>
            <xsd:documentation>
                How to reuse a base type.               
            </xsd:documentation>
        </xsd:annotation>
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="Base">
                    <xsd:sequence/>                     
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>

    <xsd:element name="PC">
        <xsd:complexType>
            <xsd:complexContent>
                <xsd:extension base="Base">
                    <xsd:sequence/>
                </xsd:extension>
            </xsd:complexContent>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>
Petru Gardea
  • 21,373
  • 2
  • 50
  • 62