首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >具有不同值的相同元素的xml需要一个架构

具有不同值的相同元素的xml需要一个架构
EN

Stack Overflow用户
提问于 2011-02-18 15:00:06
回答 1查看 420关注 0票数 1

对于下面的xml,需要一个模式。

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="UTF-8"?>
<overall_operation>
    <operation type="list_products">
        <ops_description>Listing all the products of a company</ops_description>
        <module>powesystem</module>
        <comp_name>APC</comp_name>
        <prod_price>50K$</prod_price>
        <manf_date>2001</manf_date>
        <pool_name>Electrical</pool_name>
        <fail_retry>2</fail_retry>
        <storage_type>avialble</storage_type>
        <storage_check>false</storage_check>
        <api_type>sync</api_type>
        <product_name>transformer</product_name>
    </operation>
    <operation type="search_product">
        <ops_description>Search the products of a company from the repository</ops_description>
        <module>high-voltage</module>
        <module>powesystem</module>
        <comp_name>APC</comp_name>
        <pool_name>Electrical</pool_name>
        <fail_retry>2</fail_retry>
        <storage_type>avialble</storage_type>
        <storage_check>false</storage_check>
        <api_type>sync</api_type>
        <product_name>setup-transformer</product_name>
    </operation>
</overall_operation>

这里有不同的元素和操作,比如list_productssearch_products等等。每个元素都有一些共同的属性,比如ops_descriptionmodule等等。

还有每个元素的一些唯一属性,如prod_pricemanf_date等。

我希望有一个要验证的xml模式。其中一些属性也是可选的。

我尝试使用抽象和派生,但无法使其工作。

EN

回答 1

Stack Overflow用户

发布于 2011-02-19 00:26:40

使用xml模式不可能实现您想要实现的目标。该定义规定,每个元素或属性都必须自己验证,而不能引用其他元素或属性。

你能得到的最好的解决方案是group可选但依赖的元素:

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="overall_operation">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="operation" minOccurs="0" maxOccurs="unbounded">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element name="ops_description" />
                            <xs:element name="module" minOccurs="1" maxOccurs="2" />
                            <xs:element name="comp_name" />
                            <xs:group ref="price_and_date" minOccurs="0" maxOccurs="1" />
                            <xs:element name="pool_name" />
                            <xs:element name="fail_retry" />
                            <xs:element name="storage_type" />
                            <xs:element name="storage_check" />
                            <xs:element name="api_type" />
                            <xs:element name="product_name" />
                        </xs:sequence>
                        <xs:attribute name="type" type="xs:string" />
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

    <xs:group name="price_and_date">
        <xs:sequence>
            <xs:element name="prod_price" />
            <xs:element name="manf_date" />
        </xs:sequence>
    </xs:group>
</xs:schema>

使用minOccursmaxOccurs属性控制可选元素和组。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5038504

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档