对于SimpleTypes,如何摆脱'minOccurs'属性不允许的错误?

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了对于SimpleTypes,如何摆脱'minOccurs'属性不允许的错误?相关的知识,希望对你有一定的参考价值。

验证我的架构时,我看到上述错误

xmllint --noout --schema main.xsd main.xml
main.xsd:44: element element: Schemas parser error : Element     'http://www.w3.org/2001/XMLSchemaelement': The attribute 'minOccurs' is not allowed.
main.xsd:53: element element: Schemas parser error : Element     'http://www.w3.org/2001/XMLSchemaelement': The attribute 'minOccurs' is not allowed.
WXS schema main.xsd failed to compile

我的架构如图所示。搜索后,我发现此错误在包含元素但未指定序列标签的复杂类型中可见。

我看到此错误的原因是什么?

 <?xml version="1.0" encoding="UTF-8" ?>
 <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:simpleType name="ProtocolType"> 
    <xs:restriction base="xs:string"> 
        <xs:enumeration value="HTTP"/> 
        <xs:enumeration value="HTTPS"/> 
        <xs:enumeration value="SSL"/> 
        <xs:enumeration value="TCP"/>
    </xs:restriction> 
</xs:simpleType>

<xs:simpleType name="URL">
    <xs:restriction base="xs:string">
        <xs:pattern value="[hH][tT]2[pP]://[wW]3.*"/> 
    </xs:restriction>
</xs:simpleType>

<xs:simpleType name="ipv4">
    <xs:annotation>
        <xs:documentation>
            An IP version 4 address.
        </xs:documentation>
    </xs:annotation>
    <xs:restriction base="xs:token">
    <xs:pattern
        value="(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])\.(25[0-5]|2[0-4][0-9]|[0-1]?[0-9]?[0-9])"/>
        <xs:pattern
        value="[0-9A-Fa-f]8"/>
    </xs:restriction>
</xs:simpleType>

<!-- Server: Either ip adddress or url. 
    We might support more than one here -->
<xs:simpleType name="Server">
    <xs:union memberTypes="URL ipv4" />
</xs:simpleType>

<xs:simpleType name="Protocols">
    <xs:list itemType="ProtocolType" />
</xs:simpleType>

<xs:element name="TotalRunTime" minOccurs="0" default="0"> 
    <xs:simpleType>
        <xs:restriction base="xs:positiveInteger"> 
            <xs:minExclusive value="0" />
            <xs:maxInclusive value="3600"/> 
        </xs:restriction> 
    </xs:simpleType> 
</xs:element> 

<xs:element name="Interface" minOccurs="0" type="xs:string" />

</xs:schema>
答案

尝试将这两个element包装在根元素中。当处理列表时,minOccurs仅与[[really相关,而xml文档的根与列表无关。

<xs:element name="root"> <xs:complexType> <xs:all> <xs:element name="TotalRunTime" minOccurs="0" default="0"> <xs:simpleType> <xs:restriction base="xs:positiveInteger"> <xs:minExclusive value="0" /> <xs:maxInclusive value="3600"/> </xs:restriction> </xs:simpleType> </xs:element> <xs:element name="Interface" minOccurs="0" type="xs:string" /> </xs:sequence> </xs:complexType> </xs:element>
另一答案
全局元素声明(显示为xs:schema的子元素)是对元素的可重用描述,这些元素描述了它们可以包含的内容,而不是它们出现的位置。元素出现在哪里(以及出现频率)的约束来自复杂类型,该复杂类型通过声明使用可重用的描述。该引用必须包含出现限制,而不是元素声明本身。

马修·豪根(Matthew Haugen)答案中使用的形式将元素声明和引用组合到单个结构(本地元素声明)中,该结构更简洁但灵活性较低,因为如果以后发现它可以出现,则无法重用同一元素声明在多个地方。

另一答案
您只能在minOccurs内的元素上具有maxOccurscomplexType属性。具有这些属性的元素不能是全局的(架构元素的直接子元素)。

以上是关于对于SimpleTypes,如何摆脱'minOccurs'属性不允许的错误?的主要内容,如果未能解决你的问题,请参考以下文章

如何摆脱右侧阴影但保持按钮上的底部阴影

如何摆脱进入全屏菜单项?

SAP MM 事务代码MI31之思考

oracle 中如何选择每天某个时间段中的数据,如每天7点到11点的数据?

如何摆脱问题'二进制XML文件第8行:二进制XML文件第8行:使用XYPlot时会膨胀类com.androidplot.xy.XYPlot类]

如何摆脱悬停时附加的内置CSS?