[英]XSD syntax for declaring element that cannot have a child element or other content
Would this be the proper method of declaring an XML element Foo that cannot have a child element or other content?这是声明不能有子元素或其他内容的 XML 元素 Foo 的正确方法吗?
<xs:element name="Foo" type="xs:string" fixed="" nillable="true" />
The valid examples of this element in an XML document would be: XML 文档中此元素的有效示例是:
<Foo></Foo>
and和
<Foo/>
Anything else would be invalid, eg:其他任何内容都是无效的,例如:
<Foo>stuff</Foo>
It would be nice, though not essential, that the following be valid最好,但不是必需的,以下是有效的
<Foo>
</Foo>
I combined a few options into a single schema and attempted to use it to validate some test elements.我将几个选项组合到一个模式中,并试图用它来验证一些测试元素。
Sources: http://www.w3schools.com/schema/schema_complex_empty.asp资料来源: http://www.w3schools.com/schema/schema_complex_empty.asp
http://www.herongyang.com/XML-Schema/complexType-Empty-Element-Declaration.html http://www.herongyang.com/XML-Schema/complexType-Empty-Element-Declaration.html
The schema:架构:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="root">
<xs:complexType>
<xs:choice maxOccurs="unbounded">
<xs:element name="foo">
<xs:complexType></xs:complexType>
</xs:element>
<xs:element name="y1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:length value="0" />
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="y2">
<xs:complexType>
<xs:complexContent>
<xs:restriction base="xs:anyType" />
</xs:complexContent>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
Test cases:测试用例:
<?xml version="1.0" encoding="UTF-8"?>
<root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="NewXMLSchema.xsd">
<!-- These six are all valid -->
<foo />
<y1 />
<y2 />
<foo></foo>
<y1></y1>
<y2></y2>
<!-- These three are all invalid! -->
<foo>
</foo>
<y1>
</y1>
<y2>
</y2>
<!-- These are invalid too. -->
<foo>invalid</foo>
<y1>invalid</y1>
<y2>invalid</y2>
</root>
It looks like any of the three element declarations would do what you want, except for the extra blank line feature.看起来这三个元素声明中的任何一个都可以满足您的要求,除了额外的空行功能。
I wouldn't include type="xs:string", fixed="", or nillable="true" because these are all semantically different from an empty tag.我不会包括 type="xs:string"、fixed=""、或 nillable="true",因为它们在语义上都不同于空标签。 An empty tag wouldn't really have a type (like string) because it's empty.
一个空标签实际上没有类型(如字符串),因为它是空的。 It also wouldn't have a fixed value of the null string, because that's different from being empty.
它也不会有 null 字符串的固定值,因为这不同于空值。 A nillable element is semantically different from an empty tag because it is a semantically equivalent to the tag being absent (Source: XSLT 2.0 and XSLT 2.0 Programmer's Reference by Michael Kay, p. 182.).
可空元素在语义上不同于空标签,因为它在语义上等同于标签不存在(来源:XSLT 2.0 和 XSLT 2.0 程序员参考,Michael Kay,第 182 页)。 An empty tag like
<br/>
does not mean that, in this case, the line break is absent.像
<br/>
这样的空标签并不意味着在这种情况下没有换行符。 It means that the line break is present but has no content or attriubtes.这意味着存在换行符但没有内容或属性。
There are two approaches to defining a type that enforces empty content:有两种方法可以定义强制为空内容的类型:
For validation purposes, there's not much to choose between them;出于验证目的,它们之间没有太多选择; but if you're interested in doing more advanced things with your schema, like defining type hierarchies and substitution groups, or mapping the schema to a Java object model, it could make a difference which you choose.
但是,如果您有兴趣使用您的架构执行更高级的操作,例如定义类型层次结构和替换组,或将架构映射到 Java object model,那么您的选择可能会有所不同。
<xs:element name="Foo">
<xs:complexType>
<xs:attribute name="Bar" type="xs:positiveInteger"/>
</xs:complexType>
</xs:element>
You could leave out the attribute tag to leave it totally empty...您可以省略属性标签以使其完全为空...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.