XML/XML Schema/fixed — различия между версиями

Материал из Web эксперт
Перейти к: навигация, поиск
 
м (1 версия)
 
(нет различий)

Текущая версия на 08:26, 26 мая 2010

element fixed value is the restriction value

File: Data.xml
<?xml version="1.0"?>
<ElementExamples xmlns="http://www.wbex.ru"
                   xmlns:ex="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <aircraft/>                      
    <aircraft>Boeing 747</aircraft>  
</ElementExamples>
File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:element name="ElementExamples">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="aircraft" fixed="Boeing 747" maxOccurs="2">
                    <xsd:simpleType>
                        <xsd:restriction base="xsd:string">
                            <xsd:enumeration value="Boeing 747"/>
                        </xsd:restriction>
                    </xsd:simpleType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Fixed facets ensure that the minimum value of our minInclusive cannot be modified

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  targetNamespace="http://www.wbex.ru" xmlns="http://www.wbex.ru"
  elementFormDefault="qualified">
  <xs:simpleType name="minInclusive">
    <xs:restriction base="xs:float">
      <xs:minInclusive value="10" fixed="true" />
    </xs:restriction>
  </xs:simpleType>
</xs:schema>



fixed value is one of the value in restriction

File: Data.xml
<?xml version="1.0"?>
<AttributeExamples xmlns="http://www.wbex.ru"
                   xmlns:i="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <image/>                                
    <image i:src="http://www.wbex.ru"/>  
</AttributeExamples>
File: Schema.xml
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:element name="image">
        <xsd:complexType>
            <xsd:attribute ref="src" fixed="http://www.wbex.ru"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:attribute name="src">
        <xsd:simpleType>
            <xsd:restriction base="xsd:anyURI">
                <xsd:enumeration value="http://www.wbex.ru"/>
                <xsd:enumeration value="http://www.wbex.ru/style/logo.png"/>
                <xsd:enumeration value="http://www.wbex.ru/isbn.html"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:element name="AttributeExamples">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="image" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Reference element with fixed value

File: Data.xml
<?xml version="1.0"?>
<ElementExamples xmlns="http://www.wbex.ru"
                   xmlns:ex="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation=
                              "http://www.wbex.ru
                               Schema.xsd">
    <aircraft/>                      
    <aircraft>Boeing 747</aircraft>  
</ElementExamples>

File: Schema.xsd
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:element name="aircraft" fixed="Boeing 747">
        <xsd:simpleType>
            <xsd:restriction base="xsd:string">
                <xsd:enumeration value="Boeing 747"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:element>
    <xsd:element name="ElementExamples">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="aircraft" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>



Set fixed value for attribute during simpleType declaration

File: Data.xml
<?xml version="1.0"?>
<AttributeExamples xmlns="http://www.wbex.ru"
                   xmlns:i="http://www.wbex.ru"
                   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                   xsi:schemaLocation="http://www.wbex.ru Schema.xsd">
    <image/>                                
    <image i:src="http://www.wbex.ru"/>  
</AttributeExamples>
File: Schema.xml
<?xml version="1.0"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            targetNamespace="http://www.wbex.ru"
            xmlns="http://www.wbex.ru"
            elementFormDefault="qualified">
    <xsd:element name="image">
        <xsd:complexType>
            <xsd:attribute ref="src"/>
        </xsd:complexType>
    </xsd:element>
    <xsd:attribute name="src" fixed="http://www.wbex.ru">
        <xsd:simpleType>
            <xsd:restriction base="xsd:anyURI">
                <xsd:enumeration value="http://www.wbex.ru"/>
                <xsd:enumeration value="http://www.wbex.ru/style/logo.png"/>
                <xsd:enumeration value="http://www.wbex.ru/isbn.html"/>
            </xsd:restriction>
        </xsd:simpleType>
    </xsd:attribute>
    <xsd:element name="AttributeExamples">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element ref="image" maxOccurs="2"/>
            </xsd:sequence>
        </xsd:complexType>
    </xsd:element>
</xsd:schema>