What do the attributes 'final' and 'block' mean in XSD?
Asked Answered
F

2

5

In XSD, < complextType > can have the attributes 'block' and 'final', which can take a value of #all or a list of extensions or restrictions. What do these attributes mean? How do we use them?

I could not get a clear answer from the W3C recommendation for XSD 1.1. Could someone give me some examples?

Falbala answered 2/10, 2014 at 8:55 Comment(0)
M
13

As is so often the case with XML Schema, the non-normative "primer" provides a much clearer explanation than the normative specs. In this case the section "controlling the creation and use of derived types" has a worked example of both final and block in terms of different types of address.

Essentially, final means that the type cannot have any subtypes at all (with the appropriate derivation style), whereas block says that the type can have subtypes but when an element is declared to be of a blocked type then that element must be of the declared type specifically, and not of a subtype.

Miff answered 2/10, 2014 at 9:25 Comment(1)
Getting that explanation into three lines is brilliant. The actual rules are horrendous, and I wouldn't advise anyone to use the feature for that reason, but this is a pretty good short summary.Cabby
R
0

A sample test done with sample XSD and XML snippet available in spec using the eclipse XSD/XML validation feature:

  1. Restricting derivation of types using final

<<ipo.xsd>>

<schema targetNamespace="http://www.example.com/IPO" xmlns="http://www.w3.org/2001/XMLSchema"
xmlns:ipo="http://www.example.com/IPO">
<complexType name="Address" block="extension">
    <sequence>
        <element name="name" type="string" />
        <element name="street" type="string" />
        <element name="city" type="string" />
    </sequence>
</complexType>
<complexType name="USAddress">
    <complexContent>
        <extension base="ipo:Address">
            <sequence>
                <element name="state" type="ipo:USState" />
                <element name="zip" type="positiveInteger" />
            </sequence>
        </extension>
    </complexContent>
</complexType>
<complexType name="UKAddress">
    <complexContent>
        <extension base="ipo:Address">
            <sequence>
                <element name="postcode" type="ipo:UKPostcode" />
            </sequence>
            <attribute name="exportCode" type="positiveInteger" fixed="1" />
        </extension>
    </complexContent>
</complexType>
<!-- other Address derivations for more countries -->
<simpleType name="USState">
    <restriction base="string">
        <enumeration value="AK" />
        <enumeration value="AL" />
        <enumeration value="AR" />
        <!-- and so on ... -->
    </restriction>
</simpleType>
<!-- simple type definition for UKPostcode -->
<simpleType name="UKPostcode">
    <restriction base="string">
        <enumeration value="PO16" />
        <enumeration value="G41" />
        <enumeration value="WC1A" />
        <!-- and so on ... -->
    </restriction>
</simpleType>
....
....

Screenshot of ipo.xsd validation errors

  1. Restricting which derivation can be used in instance documents using block

<<ipo.xsd>>

<schema targetNamespace="http://www.example.com/IPO" xmlns="http://www.w3.org/2001/XMLSchema"
            xmlns:ipo="http://www.example.com/IPO">   
      <complexType name="Address" block="extension">
        <sequence>
          <element name="name" type="string"/>
          <element name="street" type="string"/>
          <element name="city" type="string"/>
        </sequence>
      </complexType>
      <complexType name="USAddress">
        <complexContent>
          <extension base="ipo:Address">
            <sequence>
              <element name="state" type="ipo:USState"/>
              <element name="zip" type="positiveInteger"/>
            </sequence>
          </extension>
        </complexContent>
      </complexType>
      <complexType name="UKAddress">
        <complexContent>
          <extension base="ipo:Address">
            <sequence>
              <element name="postcode" type="ipo:UKPostcode"/>
            </sequence>
            <attribute name="exportCode" type="positiveInteger" fixed="1"/>
          </extension>
        </complexContent>
      </complexType>
      .....
      .....
    </schema>


<<ipo.xml>>

<?xml version="1.0"?>
<ipo:purchaseOrder xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ipo="http://www.example.com/IPO"
                   orderDate="1999-12-01"
                   xsi:schemaLocation="http://www.example.com/IPO ipo.xsd">

  <shipTo  xsi:type="ipo:UKAddress" exportCode="1">
    <name>Helen Zoe</name>
    <street>47 Eden Street</street>
    <city>Cambridge</city>
    <postcode>WC1A</postcode>
  </shipTo>
  <billTo xsi:type="ipo:USAddress">
    <name>Robert Smith</name>
    <street>8 Oak Avenue</street>
    <city>Old Town</city>
    <state>AL</state>
    <zip>95819</zip>
  </billTo>
  <items>
    <item partNum="833-AA">
      <productName>Lapis necklace</productName>
      <quantity>1</quantity>
      <USPrice>99.95</USPrice>
      <ipo:comment>Want this for the holidays!</ipo:comment>
      <shipDate>1999-12-05</shipDate>
    </item>
  </items>
</ipo:purchaseOrder>

Screenshot of ipo.xml validation errors
Note that ipo.xsd validation shows no error for the 2nd case (using block="extension").

Rescript answered 15/4, 2018 at 16:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.