XML Schema 构造,具有不同值的相同元素的 xml 需要一个 schema

发布于 2025-01-03 09:07:47 字数 5197 浏览 1 评论 0原文

所以我在检查属性时遇到问题。到目前为止,我已经尝试了我所知道的一切,但没有成功。如果 perishable=no,则有一些额外的元素,如果是,则有一些与 perishable=no 不同的元素。我尝试过团体,但没有成功。我本来想把食物和库存分组,但对易腐烂的东西有限制。只有当易腐烂是的时候,我们才有食物元素,如果不是,我们就有库存元素。请帮忙!

    <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>

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>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

流年已逝 2025-01-10 09:07:47

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

然而,无法使用易腐烂的属性来控制这种情况。除非,您可能会使用 XSD 1.1 中的断言来做到这一点(我不知道)。上次我检查过,XSD 1.1 仍然只是一个草案,但如果您可以选择,您可以研究一下。我相信有些工具支持它(Saxon.Xerces?)

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.

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?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文