XSD 使属性可以为空 [英] XSD make attribute nullable

查看:54
本文介绍了XSD 使属性可以为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

创建 XML 时,属性 wait 可能并不总是包含值.如何编辑架构以允许属性 wait 包含数字或不包含值?

When creating the XML the attribute wait may not always contain a value. How can I edit the schema so it allows the attribute wait to contain either a number or no value?

<xs:complexType name="CommandType">
        <xs:simpleContent>
            <xs:extension base="xs:string">
                <xs:attribute type="xs:string" name="exe" use="required" />
                <xs:attribute type="xs:string" name="args" use="required" />
                <xs:attribute type="xs:int" name="wait" use="required" />
                <xs:attribute type="xs:string" name="expectedOutput" use="required" />
                <xs:attribute type="xs:string" name="toVariable" use="required" />
            </xs:extension>
        </xs:simpleContent>
    </xs:complexType>

我已经尝试过这两个 nillable="true" xsi:nil="true" 但它们不起作用.当我尝试验证 XSD 时出现错误.

I have tried doing both these nillable="true" xsi:nil="true" but they don't work. When I tried to validate the XSD I got errors.

推荐答案

"nillable" 仅适用于元素,不适用于属性 - 即便如此它也不是很有用,因为如果元素为空,您必须添加 xsi:nil=true",完全是多余的.

"nillable" only works for elements, not for attributes - and even then it's not very useful because if the element is empty you have to add xsi:nil="true", which is completely redundant.

要么 (a) 定义一个类型,它是 xs:integer 和零长度字符串的联合,如 IMSoP 所建议的,或者 (b) 定义一个列表类型,项目类型为整数,minLength 0,maxLength 1.我更喜欢后一种解决方案,因为它与模式感知 XSLT 和 XQuery 配合得更好.

Either (a) define a type that's a union of xs:integer and a zero-length string, as suggested by IMSoP, or (b) define a list type with item type integer, minLength 0, maxLength 1. I prefer the latter solution as it plays better with schema-aware XSLT and XQuery.

这篇关于XSD 使属性可以为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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