定义必须为空或十进制的 XML 元素 [英] Defining an XML element that must be empty or decimal

查看:49
本文介绍了定义必须为空或十进制的 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 XSD,它是由 VS 从示例 XML 文件生成的:

I have the following XSD which was generated by VS from an example XML file:

                <xs:element name="amperage_rating">
                  <xs:complexType>
                    <xs:simpleContent>
                      <xs:extension base="xs:decimal">
                        <xs:attribute name="unit" type="xs:string" use="required" />
                      </xs:extension>
                    </xs:simpleContent>
                  </xs:complexType>
                </xs:element>

需要一个数值.但有时值是未知的,这也必须允许:

which demands a numeric value. But sometimes the value is not known and this must also be allowed:

<amperage_rating unit="A"></amperage_rating>

我试过 nillable="true" 但没有效果.我还尝试定义一个长度为 0 的字符串的联合,但一直说在这种情况下不支持".

I've tried nillable="true" but it had no effect. I have also tried to define a union with a 0-length string but that keeps saying it is "not supported in this context".

那么我该如何调整 XSD 以允许这样做呢?

So how do I adjust the XSD to allow this?

推荐答案

您可以通过 xs:union 组合您希望允许的两种类型:

You can combine the two types you wish to allow via xs:union:

<xs:element name="amperage_rating" type="empty_or_decimal"/>

<xs:simpleType name="empty_or_decimal">
  <xs:union memberTypes="empty xs:decimal"/>
</xs:simpleType>

<xs:simpleType name="empty">
  <xs:restriction base="xs:string">
    <xs:enumeration value=""/>
  </xs:restriction>
</xs:simpleType>

这篇关于定义必须为空或十进制的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆