本文介绍了XML Schema 构造,具有不同值的相同元素的 xml 需要一个模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在检查属性时遇到了问题.到目前为止,我已经尝试了我所知道的一切,但没有奏效.如果 perishable=no 有一些额外的元素,如果是,那么有一些与 perishable=no 不同的元素.我试过团体,但没有奏效.我正在考虑将食物和库存分组,但对易腐烂有限制.只有当易腐烂是肯定的,我们才有食物元素,如果不是,我们就有库存元素.请帮忙!!!

So I'm having a problem with checking attributes. I've tried everything I know so far and it hasn't worked. If perishable=no there are some extra elements, if its yes then there are some different elements from the perishable=no. I've tried groups and it hasn't worked. I was thinking to group the food and stock but there is that restriction on perishable. Only if perishable is yes we have food element and if its no we have stock element. Help please!!!

    <product id = "p12" perishable = "yes">
    <name>Ice cream</name>
    <manufacturer>xsz Co.</manufacturer>
    <quantity>25</quantity>
    <price>2</price>

    <food>
        <nutrition>
            <calcium>10.30</calcium>
            <proteins>35.5</proteins>
            <fat>10</fat>
        </nutrition>

        <expirationDate>2000-09-12</expirationDate>
    </food>
</product>

<product id = "p13" perishable = "no">
    <name>AA Battries</name>
    <manufacturer>DCells</manufacturer>
    <quantity>100</quantity>
    <price>4</price>

    <stock>
        <warehouse id = "w12">
        xsz warehouse
            <stock>25000</stock>
        </warehouse>

        <warehouse id = "w13">
        rza warehouse
            <stock>5000</stock>
        </warehouse>
    </stock>

</product>
<!-- defining the nutrition element for the perishable product -->

<xs:element name="nutrition">   
    <xs:complexType>
        <xs:sequence>
            <xs:element name="calcium" type="xs:decimal"/>
            <xs:element name="proteins" type="xs:decimal"/>
            <xs:element name="fat" type="xs:decimal"/>
        </xs:sequence>
    </xs:complexType>
</xs:element>

<!-- defining the group product that is perishable -->

<xs:group name="perishableGroup">
    <xs:sequence>
        <xs:element name="product">
            <xs:complexType>
                <xs:sequence> 
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="manufacturer" type="xs:string"/>
                    <xs:element name="quantity" type="xs:positiveInteger"/>
                    <xs:element name="price" type="xs:decimal"/>
                    <xs:element name="food">
                        <xs:complexType> <!-- defining the food element for perishable product -->
                            <xs:sequence>
                                <xs:element ref="nutrition"/> <!-- defined above -->
                                <xs:element name="expirationDate" type="xs:date"/>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="id" type="xs:ID" use="required"/>
                <xs:attribute name="perishable" type="xs:string" use="required" fixed="yes"/>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:group>

<!-- defining the group product that is nonperishable -->

<xs:group name="nonperishableGroup">
    <xs:sequence>
        <xs:element name="product">
            <xs:complexType>
                <xs:sequence> 
                    <xs:element name="name" type="xs:string"/>
                    <xs:element name="manufacturer" type="xs:string"/>
                    <xs:element name="quantity" type="xs:positiveInteger"/>
                    <xs:element name="price" type="xs:decimal"/>
                    <xs:element name="stock">
                        <xs:complexType>
                            <xs:sequence>
                                <xs:element name="warehouse" maxOccurs="unbounded">
                                    <xs:complexType mixed="true">
                                        <xs:sequence>
                                            <xs:element name="stock" type="xs:positiveInteger"/>
                                        </xs:sequence>
                                        <xs:attribute name="id" type="xs:ID" use="required"/>
                                    </xs:complexType>
                                </xs:element>
                            </xs:sequence>
                        </xs:complexType>
                    </xs:element>
                </xs:sequence>
                <xs:attribute name="id" type="xs:ID" use="required"/>
                <xs:attribute name="perishable" type="xs:string" use="required" fixed="no"/>
            </xs:complexType>
        </xs:element>
    </xs:sequence>
</xs:group>

<xs:element name="products">
    <xs:complexType>
        <xs:choice minOccurs="1" maxOccurs="unbounded">
            <xs:group ref="perishableGroup"/>
            <xs:group ref="nonperishableGroup"/>
        </xs:choice>
    </xs:complexType>
</xs:element>

推荐答案

您不能使用属性来控制元素的内容模型,只有一个例外:xsi:type 属性.xsi:type 属性可以出现在您的实例文档中.它告诉验证解析器应该使用什么类型来验证元素.指定的类型必须是元素声明类型(或声明类型本身)的子类型.然后您将定义两个相关的类型(一个扩展或限制另一个,或者两者都扩展或限制某个公共抽象基类).其中一种类型支持您拥有易腐烂物品的情况,另一种类型支持您拥有不易腐烂物品的情况.

You cannot use attributes to control the content model of an element, with one exception: the xsi:type attribute. The xsi:type attribute can appear in your instance document. It tells the validating parser what type should be used to validate the element. The type specified must be a subtype of the element's declared type (or the declared type itself). You would then define two related types (one extends or restricts the other, or else both extend or restrict some common abstract base). One of these types would support the case where you have a perishable item, the other one where you have a non-perishable item.

但是,无法使用 perishable 属性来控制这一点.除非,您可能会使用来自 XSD 1.1 的断言(我不知道)来做到这一点.最后我查了一下,XSD 1.1 仍然只是一个草案,但如果您可以选择它,您可以研究一下.我相信一些工具支持它(Saxon.Xerces?)

There is no way, however, to control this using the perishable attribute. UNLESS, possibly you might do it using assertions from XSD 1.1 (I don't know). Last I checked, XSD 1.1 was still only a draft, but if that is an option for you, you could look into it. I believe some tools have support for it (Saxon. Xerces?)

这篇关于XML Schema 构造,具有不同值的相同元素的 xml 需要一个模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-22 01:06