Xml 重写组中的minOccurs
我有一种情况,有一个我不“拥有”的基类型,它允许所有子元素都是可选的。从一般商业角度来看,这是有道理的。例如,假设它是:Xml 重写组中的minOccurs,xml,xsd,restrictions,Xml,Xsd,Restrictions,我有一种情况,有一个我不“拥有”的基类型,它允许所有子元素都是可选的。从一般商业角度来看,这是有道理的。例如,假设它是: <xs:complexType name="BaseType"> <xs:sequence> <xs:element name="id" type="xs:string" minOccurs="0"/> <xs:element name="name" type="xs:string" minOc
<xs:complexType name="BaseType">
<xs:sequence>
<xs:element name="id" type="xs:string" minOccurs="0"/>
<xs:element name="name" type="xs:string" minOccurs="0"/>
</xs:sequence>
</xs:complexType>
此类型的实例将包含在我的流程使用的包含类型中
<xs:complexType name="MyType">
<xs:sequence>
<xs:element name="Widget" type="BaseType"/>
...other stuff...
<xs:sequence>
</xs:complexType>
…其他东西。。。
但是,出于我的特殊目的,我需要BaseType上的'id'元素始终存在。当然,我可以在使用它的代码中强制执行这一点,但是否有某种方式可以在xsd中表示这一点
编辑
我忘了另一个。还有另一种类似的情况,我使用的元素是在一个组中,而不是在一个基类型中
<xs:group name="Address-Group">
<xs:sequence>
<xs:element name="street" type="xs:string" minOccurs="0"/>
<xs:element name="city" type="xs:string" minOccurs="0"/>
...etc....
</xs:sequence>
</xs:group>
等
如果我在MyType中使用其中一个元素,我会遇到同样的问题,即尝试为“city”之类的对象重写minOccurs。假设
出于我的特定目的,我需要基类型上的“id”元素始终存在
您真正的意思是始终,然后您可以应用一个重定义,例如下面的一个:
<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XSR Module (http://www.paschidev.com)-->
<xsd:schema xmlns="http://tempuri.org/XMLSchema.xsd" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://tempuri.org/XMLSchema.xsd" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:redefine schemaLocation="xsd-overriding-minoccurs-from-a-group.xsd">
<xsd:complexType name="BaseType">
<xsd:complexContent>
<xsd:restriction base="BaseType">
<xsd:sequence>
<xsd:element name="id" type="xsd:string"/>
<xsd:element name="name" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:restriction>
</xsd:complexContent>
</xsd:complexType>
<xsd:group name="Address-Group">
<xsd:sequence>
<xsd:element name="street" type="xsd:string"/>
<xsd:element name="city" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:group>
</xsd:redefine>
</xsd:schema>
或者这个(基于Java的):
重定义可以很好地与支持XSD的XML处理器配合使用。但是,一些XSD到代码绑定框架,或者具有内置XSD支持的数据库引擎可能会对此抱怨。我已经编辑了您的标题。请参见“”,其中一致意见是“不,他们不应该”。如果您不想全局重新定义该类型,您当然可以通过限制派生另一个类型,并仅在选定的位置使用它。但这在实践中可能很难实现。
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<paschidev.dummy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="BaseType" xmlns="http://tempuri.org/XMLSchema.xsd">
<id>id1</id>
<name>name1</name>
</paschidev.dummy>
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<!-- Sample XML generated by QTAssistant (http://www.paschidev.com) -->
<paschidev.dummy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="BaseType" xmlns="http://tempuri.org/XMLSchema.xsd">
<name>name1</name>
</paschidev.dummy>
Error occurred while loading [], line 4 position 3
The element 'paschidev.dummy' in namespace 'http://tempuri.org/XMLSchema.xsd' has invalid child element 'name' in namespace 'http://tempuri.org/XMLSchema.xsd'. List of possible elements expected: 'id' in namespace 'http://tempuri.org/XMLSchema.xsd'.
Error while loading [], line 4 position 8
cvc-complex-type.2.4.a: Invalid content was found starting with element 'name'. One of '{"http://tempuri.org/XMLSchema.xsd":id}' is expected.