我想扩展XHTML以将其用作模板语言。我有一个像XML Schema - 是否可以在整个文档中只允许一次某个元素?
<mylang:content/>
我想这个标记标签出现在那里div
或p
可以出现一个标记标签,但只有一次完整的文件内。我认为使用XML Schema是不可能的,但也许一些XML Schema专家知道得更好。
当包含元素被允许显示为无界时,是否可以在整个文档中仅允许某个元素一次?
我想扩展XHTML以将其用作模板语言。我有一个像XML Schema - 是否可以在整个文档中只允许一次某个元素?
<mylang:content/>
我想这个标记标签出现在那里div
或p
可以出现一个标记标签,但只有一次完整的文件内。我认为使用XML Schema是不可能的,但也许一些XML Schema专家知道得更好。
当包含元素被允许显示为无界时,是否可以在整个文档中仅允许某个元素一次?
如果您知道根元素会,那么我认为你可以将文档元素定义约束
<xs:unique name="one-content">
<xs:selector xpath=".//mylang:content"/>
<xs:field xpath="."/>
</xs:unique>
这是说,所有的mylang:内容的后代必须具有不同的字符串值;但由于元素被限制为空,如果每个元素都必须是不同的,那么只能有一个元素。
在XSD 1.1中,断言变得更容易。
完整的例子
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="html">
<xs:complexType>
<xs:sequence>
<xs:element ref="body" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="body">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="content" />
</xs:choice>
</xs:complexType>
<xs:unique name="content">
<xs:selector xpath="content" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
<xs:element name="content">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="0" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
当心: - 如果您在模式中增加一个目标名称,唯一约束突然停止工作。这是因为xs:unique,xs:key和xs:keyref不使用默认命名空间。你必须改变你的模式是这样的:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema
xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3.org/1999/xhtml"
xmlns="http://www.w3.org/1999/xhtml">
<xs:element name="html">
<xs:complexType>
<xs:sequence>
<xs:element ref="body" />
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="body">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element ref="content" />
</xs:choice>
</xs:complexType>
<xs:unique name="content" xmlns:html="http://www.w3.org/1999/xhtml">
<xs:selector xpath="content" />
<xs:field xpath="." />
</xs:unique>
</xs:element>
<xs:element name="content">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:maxLength value="0" />
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:schema>
伟大的答案,我试图重新定义我的身体元素,但失败了,所以我在这里发布了一个新问题http://stackoverflow.com/questions/7724674 – Janning
哎迈克尔,我拥有一本你自己的书,现在你正在从封面上看着我:-)顺便说一句, – Janning
内容必须用简单的类型来定义。 <:元素名称= “内容” XS> \t \t \t \t \t 如果检查唯一 \t \t \t 一个空元素引发错误\t \t \t \t \t \t \t XS:限制> \t \t XS:的simpleType> \t –
Janning