Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
XML模式—是否可以在整个文档中只允许某个元素使用一次?_Xml_Xsd - Fatal编程技术网

XML模式—是否可以在整个文档中只允许某个元素使用一次?

XML模式—是否可以在整个文档中只允许某个元素使用一次?,xml,xsd,Xml,Xsd,我想扩展XHTML以将其用作模板语言。我有一个像这样的标记 <mylang:content/> 我希望此标记标记出现在div或p可以出现的位置,但在整个文档中仅出现一次。我认为使用XML模式是不可能的,但也许一些XML模式大师知道得更好 当允许包含元素显示为无边界时,是否可以在整个文档中只允许某个元素出现一次 如果您知道根元素将是,那么我认为您可以在doc元素上定义一个约束 <xs:unique name="one-content"> <xs:selec

我想扩展XHTML以将其用作模板语言。我有一个像这样的标记

 <mylang:content/>

我希望此标记标记出现在
div
p
可以出现的位置,但在整个文档中仅出现一次。我认为使用XML模式是不可能的,但也许一些XML模式大师知道得更好


当允许包含元素显示为无边界时,是否可以在整个文档中只允许某个元素出现一次

如果您知道根元素将是,那么我认为您可以在doc元素上定义一个约束

<xs:unique name="one-content">
  <xs:selector xpath=".//mylang:content"/>
  <xs:field xpath="."/>
</xs:unique>

这意味着所有mylang:content子体必须具有不同的字符串值;但是由于元素被约束为空,如果每个元素必须是不同的,那么只能有一个元素

当然,在XSD1.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>

注意: -如果在模式中添加targetNamespace,则唯一约束会突然停止工作。这是因为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>


回答得很好,我试图重新定义我的身体元素,但失败了,所以我在这里发布了一个新问题嘿,迈克尔,我拥有你的一本书,现在你从封面上看着我:-)伟大的书的内容必须用简单的类型来定义。如果检查唯一性,空元素将抛出错误