当包含complexType时,如何使*xsd:element可选?

时间:2021-07-12 17:17:18

I have this XSD element defined:

我定义了这个XSD元素:

<xsd:element name="CU_FIRST_NAME">
    <xsd:annotation>
        <xsd:documentation>
            The first name of the customer that is getting billed for the order
        </xsd:documentation>
    </xsd:annotation>
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:maxLength value="50"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:element>

I know this could be replaced by a single element, but I have a more complex element (a sequence) that I need to make optional.

我知道这可以被单个元素替换,但我有一个更复杂的元素(序列),我需要使它成为可选的。

Is there a way to make a first-child (i.e. just below <xsd:schema> in the hierarchy) element optional?

有没有办法让第一个孩子(即层次结构中的 下面)元素可选? :schema>

To be clear, I'd like to make the entire CU_FIRST_NAME node, along with all of its children, optional.

为了清楚起见,我想让整个CU_FIRST_NAME节点及其所有子节点都是可选的。

2 个解决方案

#1


1  

I ended up realizing that the element I was using was actually a ref within a higher level object.

我最终意识到我使用的元素实际上是更高级别对象中的ref。

The way I solved this was thus:

我解决这个问题的方式是这样的:

Original code:

原始代码:

<xsd:element ref="CU_FIRST_NAME"/>

New code:

新代码:

<xsd:element ref="CU_FIRST_NAME" minOccurs="0"/>

So, really I was asking the wrong question.

所以,我真的在问错误的问题。

#2


0  

<xs:element name="item" maxOccurs="unbounded">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

use minOccurs="0" in the very first element. this will make it so that if you select the top you have to select the rest. but if you put that in the children as well then they will all be optional.

在第一个元素中使用minOccurs =“0”。这将使它如果你选择顶部你必须选择其余的。但如果你把它放在孩子身上那么他们都是可选的。

#1


1  

I ended up realizing that the element I was using was actually a ref within a higher level object.

我最终意识到我使用的元素实际上是更高级别对象中的ref。

The way I solved this was thus:

我解决这个问题的方式是这样的:

Original code:

原始代码:

<xsd:element ref="CU_FIRST_NAME"/>

New code:

新代码:

<xsd:element ref="CU_FIRST_NAME" minOccurs="0"/>

So, really I was asking the wrong question.

所以,我真的在问错误的问题。

#2


0  

<xs:element name="item" maxOccurs="unbounded">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="title" type="xs:string"/>
      <xs:element name="note" type="xs:string" minOccurs="0"/>
      <xs:element name="quantity" type="xs:positiveInteger"/>
      <xs:element name="price" type="xs:decimal"/>
    </xs:sequence>
  </xs:complexType>
</xs:element>

use minOccurs="0" in the very first element. this will make it so that if you select the top you have to select the rest. but if you put that in the children as well then they will all be optional.

在第一个元素中使用minOccurs =“0”。这将使它如果你选择顶部你必须选择其余的。但如果你把它放在孩子身上那么他们都是可选的。