Konstantin Milyutin
Konstantin Milyutin

Reputation: 12366

Include the same attributes into multiple elements without copy paste

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?

Upvotes: 2

Views: 451

Answers (1)

Petru Gardea
Petru Gardea

Reputation: 21638

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>

Upvotes: 2

Related Questions