11.2. An Example of a Complex Type That Adds Attributes to a Simple Type

A complex type can add attribute types to a simple type. Listing 11.2 is the complex type salePriceType. salePriceType extends the globally described dollarPriceType simple type by adding the saleAttributeGroup attribute group. Note that the simpleContent element provides the functionality to extend simple types into complex types.

Listing 11.2. Adding an Attribute to a Simple Type (pricing.xsd)
<xsd:complexType name="salePriceType" 
                 block="#all" 
                 final="extension" 
                 id="salePriceType.pricing.cType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            Anything on sale must have a price 
            and an authorization. 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:simpleContent> 
        <xsd:extension base="dollarPriceType"> 
            <xsd:attributeGroup ref="saleAttributeGroup"/> 
    </xsd:extension>   
    </xsd:simpleContent> 
</xsd:complexType> 

<xsd:attribute name="currency" 
               type="xsd:token" 
               fixed="U S Dollars"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            U S Dollars are the only currency 
            currently allowed.  This attribute 
            is a great example of using "ref" 
            (elsewhere), but is not set up well 
            for extending to other currencies 
            later.  This should really be a 
            type that keeps getting restricted. 
        </xsd:documentation> 
    </xsd:annotation> 
</xsd:attribute> 

<xsd:simpleType name="currencyAmountType" 
                id="pricing.currencyAmount.sType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            Limit all transactions to less than 
            500,000.00 of any currency 
            This can be represented as NNNNNN.NN 
            or eight total digits, two of which are 
            after the decimal point. 

            ********************************************* 
               Note that the W3C XML Schema 
               Recommendation does not support 
               non-instantiable simple types. 

               This simple type is conceptually 
               not instantiable.  This type is not 
               intended to be used directly, only 
               indirectly, such as via the ...DollarType 
               simple types. 
            ********************************************* 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:restriction base="xsd:decimal"> 
        <xsd:totalDigits value="8" fixed="true"/> 
        <xsd:fractionDigits value="2" fixed="true"/> 
        <xsd:minExclusive value="0.00" fixed="true"/> 
        <xsd:maxInclusive value="500000.00" fixed="true"/>  
    </xsd:restriction> 
</xsd:simpleType> 

<xsd:simpleType name="restrictedDollarAmountType" 
                id="pricing.restrictedDollarAmount.sType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            Nothing sells for less than $1 or 
            greater than or equal to $10,000.00. 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:restriction base="currencyAmountType"> 
        <xsd:minInclusive value="1.00" 
                          fixed="true"/> 
        <xsd:maxExclusive value="10000.00" 
                          fixed="true"/> 
    </xsd:restriction> 
</xsd:simpleType> 

<xsd:complexType name="dollarPriceType" 
                 final="restriction" 
                 block="restriction" 
                 abstract="true" 
                 id="dollarPriceType.pricing.cType"> 
    <xsd:annotation> 
        <xsd:documentation xml:lang="en"> 
            Currently, currency is limited to 
            U S Dollars. Note that this type is 
            non-instantiable.  A derived type must be 
            defined that sets the range. 
        </xsd:documentation> 
    </xsd:annotation> 
    <xsd:simpleContent> 
        <xsd:extension base="restrictedDollarAmountType"> 
            <xsd:attribute ref="currency"/> 
        </xsd:extension> 
    </xsd:simpleContent> 
</xsd:simpleType> 

The XML representation of an element type whose structure type is salePriceType might look like the following element:

<xsd:element name="salePrice" type="salePriceType"/> 

Given the preceding element type, the following element is valid in an XML instance:

<salePrice>123.45</salePrice> 

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

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