dimanche 28 juin 2015

how to validate my xml file by 2 elements using assert in xsd

i have to write xml and xsd files. here is the task.

Periodicals. Title - the name of the publication. Type - the type of publication (newspaper, magazine, booklet). Monthly - monthly or not. Chars (should be more than one) - characteristics: color (yes or no), volume (n pages), gloss (yes [only for magazines and brochures] or not [for newspapers]), a subscription index (newspapers and magazines) . The element called Paper.

here is my xml files which i wrote.

<Paper>
    <title>quest</title>
    <type>Newspaper</type>
    <monthly>true</monthly>

    <chars>
        <color>true</color>
        <count>123</count>
        <glossy>false</glossy>
        <hasIndex>true</hasIndex>
    </chars>
</Paper>

<Paper>
    <title>.NET</title>
    <type>Journal</type>
    <monthly>true</monthly>

    <chars>
        <color>true</color>
        <count>1233</count>
        <glossy>true</glossy>
        <hasIndex>true</hasIndex>
    </chars>
</Paper>

<Paper>
    <title>IT</title>
    <type>Booklet</type>
    <monthly>true</monthly>
    <chars>
        <color>true</color>
        <count>1233</count>
        <glossy>true</glossy>
        <hasIndex>false</hasIndex>
    </chars>
</Paper>

and i have my xsd to validate it.

<xs:element name="Papers">






    <xs:complexType>
        <xs:sequence>
            <xs:element name="Paper" type="paper" maxOccurs="unbounded" />
        </xs:sequence>
    </xs:complexType>


</xs:element>




<!-- paper -->
<xs:complexType name="paper">
    <xs:annotation>
        <xs:appinfo>
            <sch:pattern>
                <sch:rule context="paper">
                    <sch:assert
                        test="(((@title='Journal' and @glossy='true') 
                        or 
                        (@title='Booklet' and @glossy='true') 
                        or 
                        (@title='Newspaper' and @glossy='false')) 
                        and 
                        ((@title='Newspaper' and @hasIndex='true')
                        or
                        (@title='Journal' and @hasIndex='true')
                        or
                        (@title='Booklet' and @hasIndex='false')
                        ))">

                        Chars (должно быть несколько) - характеристики: цветное (да
                        либо нет),
                        объем (n страниц), глянцевое (да [только для журналов и
                        буклетов]
                        либо нет [для газет]), имеет подписной индекс (только
                        для газет и
                        журналов).

                    </sch:assert>
                </sch:rule>

            </sch:pattern>
        </xs:appinfo>
    </xs:annotation>
    <xs:sequence>
        <xs:element name="title" type="xs:string" />
        <xs:element name="type" type="enumType" />
        <xs:element name="monthly" type="xs:boolean" />
        <xs:element name="chars" type="chars" />
    </xs:sequence>
</xs:complexType>
<!-- paper -->


<xs:simpleType name="enumType">
    <xs:restriction base="xs:string">
        <xs:enumeration value="Newspaper" />
        <xs:enumeration value="Journal" />
        <xs:enumeration value="Booklet" />
    </xs:restriction>
</xs:simpleType>

<xs:complexType name="chars">
    <xs:sequence>
        <xs:element name="color" type="xs:boolean" />
        <xs:element name="count" type="xs:integer" />
        <xs:element name="glossy" type="xs:boolean" />
        <xs:element name='hasIndex' type='xs:boolean' />
    </xs:sequence>
</xs:complexType>

but if i put wrong values into elements - for example:

<Paper>
        <title>quest</title>
        <type>Newspaper</type>
        <monthly>true</monthly>

        <chars>
            <color>true</color>
            <count>123</count>
            <glossy>true</glossy> <!-- must be only false -->
            <hasIndex>true</hasIndex>
        </chars>
    </Paper>

Assert does not work. How to resolve it? Maybe exists another way to accomplish my goals?

THE PROBLEM WAS SOLVED HERE IS AN EXAMPLE

<xsd:element name="container">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="component" type="parent" maxOccurs="unbounded"/>
        </xsd:sequence>        
    </xsd:complexType>        
</xsd:element>

<xsd:complexType name="parent" abstract="true"/>

<xsd:complexType name="childA">
    <xsd:complexContent>
        <xsd:extension base="parent">
            <xsd:sequence>
                <xsd:element name="a" type="xsd:string" />
            </xsd:sequence>
            <xsd:attribute name="atr" type="xsd:string" use="required"/>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

<xsd:complexType name="childB">
    <xsd:complexContent>
        <xsd:extension base="parent">
            <xsd:sequence>
                <xsd:element name="b" type="xsd:string"/>
            </xsd:sequence>
        </xsd:extension>
    </xsd:complexContent>
</xsd:complexType>

Валидный XML документ

<component xsi:type="childA" atr="value of atr">
    <a>content of a</a>
</component>

<component xsi:type="childB">
    <b>content of b</b>
</component> 

Aucun commentaire:

Enregistrer un commentaire