0

我有一个属性是1)不需要的情况,2)使用时可以采用.i)null或空或.ii)将使用正则表达式验证(我希望)的版本字符串。

关于 2.i,如何创建可空属性?在我使用 xml 和 xsds 的短暂时间里,我还没有看到这个

编辑:添加我正在谈论的示例,其中“attribute_1”对应于我的情况

<xsd:element attribute_1="x" attribute_2="y" />
<!-- or -->
<xsd:element attribute_2="y" />
<!-- or -->
<xsd:element attribute_1="" attribute_2="y" />
4

2 回答 2

0

You need to create a complex type for your element and declare the attribute as "optional", something like this:

<xs:element name="tElementType">
  <xs:complexType>
    <xs:sequence>
      <xs:element name="myelement" type="xs:string"/>           
    </xs:sequence>
    <xs:attribute name="myattribute" type="xs:string" use="optional"/>
  </xs:complexType>
</xs:element>-
<xs:element name="thisIsMyRealElement" type="tElementType" />

See http://www.w3schools.com/Schema/schema_example.asp

Hope that helps ;)

于 2013-06-10T17:09:18.620 回答
0

在 XSD 中,'nillable' 是一个仅适用于元素的技术术语;nillability 允许其类型要求它们具有内容的元素即使为空也是有效的。因此,您提出的问题的答案是“不,属性不能为空”。

另一方面,您描述的是一个可以未指定的属性(您可以use="optional"在属性声明中实现)并且允许空字符串作为有效值(这已经是 xs:string 的属性)。那么问题的答案是“当使用空字符串指定时,我可以使属性可忽略且有效吗?” 是是的。”

由于 "" 是 xs:string 的合法值,minLength="0"因此您指定的模式和模式都不是必需的;如果您有一个验证器拒绝输入为 xs:string 的属性的空字符串,那么您的验证器已损坏。

于 2013-06-21T16:28:46.430 回答