加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

xml – XSD属性(不是元素)不应该是空字符串

发布时间:2020-12-16 07:47:03 所属栏目:百科 来源:网络整理
导读:参见英文答案 XSD Element Not Null or Empty Constraint For Xml?5个 我需要在下面定义的模式中做出哪些更改,以便名称代码的属性不应该是空字符串/验证代码是否为空? xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualif
参见英文答案 > XSD Element Not Null or Empty Constraint For Xml?5个
我需要在下面定义的模式中做出哪些更改,以便名称代码的属性不应该是空字符串/验证代码是否为空?
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified">
<xsd:attribute name="code" type="xsd:string"/>
    <xsd:element name="Root">
        <xsd:complexType>
            <xsd:sequence>
                <xsd:element name="Child" nillable="false">
                    <xsd:complexType>
                        <xsd:sequence>
                            <xsd:element name="childAge">
                                <xsd:simpleType>
                                    <xsd:restriction base="xsd:integer"/>
                                </xsd:simpleType>
                            </xsd:element>
                        </xsd:sequence>
                        <xsd:attribute ref="code" use="required"></xsd:attribute>
                    </xsd:complexType>
                </xsd:element>
            </xsd:sequence>
        </xsd:complexType>
</xsd:element>
键入xsd:string类型包含空字符串,因此使用
<Child code="">

根据您的架构有效.有几种方法可以限制类型.如果您只想限制可以使用的长度:

<xsd:attribute name="code">
    <xsd:simpleType>
        <xsd:restriction base="xsd:string">
            <xsd:minLength value="1"/>
        </xsd:restriction>
    </xsd:simpleType>
</xsd:attribute>

或者,您可以使用不包含空字符串的类型作为有效类型,例如:

<xsd:attribute name="code" type="xsd:NMTOKEN" />

它也不允许使用特殊字符或空格.如果您的代码需要特定模式,则可能需要在正则表达式中指定该模式,例如:

<xsd:restriction base="xsd:string">
    <xsd:pattern value="[A-Z][0-9]{4}"/>
</xsd:restriction>

这也不会验证空字符串.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读