Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/15.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 用于声明不能有子元素或其他内容的元素的XSD语法_Xml_Xsd_Schema - Fatal编程技术网

Xml 用于声明不能有子元素或其他内容的元素的XSD语法

Xml 用于声明不能有子元素或其他内容的元素的XSD语法,xml,xsd,schema,Xml,Xsd,Schema,这是声明不能有子元素或其他内容的XML元素Foo的正确方法吗 <xs:element name="Foo" type="xs:string" fixed="" nillable="true" /> XML文档中此元素的有效示例如下: <Foo></Foo> 及 任何其他内容都将无效,例如: <Foo>stuff</Foo> 东西 下面的内容是有效的,尽管不是必要的 <Foo> </Foo>

这是声明不能有子元素或其他内容的XML元素Foo的正确方法吗

<xs:element name="Foo" type="xs:string" fixed="" nillable="true" />

XML文档中此元素的有效示例如下:

<Foo></Foo>


任何其他内容都将无效,例如:

<Foo>stuff</Foo>
东西
下面的内容是有效的,尽管不是必要的

<Foo>
</Foo>



您可以省略属性标记使其完全为空…

定义强制执行空内容的类型有两种方法:一种是使用简单类型的xs:string,该类型使用枚举或模式方面(或使用固定)约束为零长度;另一种方法是使用一个定义为(比如)元素空序列的复杂类型。出于验证目的,它们之间没有太多选择;但是,如果您对使用模式做更高级的事情感兴趣,比如定义类型层次结构和替换组,或者将模式映射到Java对象模型,那么您选择的模式可能会有所不同。

我将一些选项组合到一个模式中,并尝试使用它来验证一些测试元素

资料来源:

模式:

<?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>

测试用例:

<?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>

无效的
无效的
无效的
看起来这三个元素声明中的任何一个都可以实现您想要的功能,除了额外的空行功能


我不会包括type=“xs:string”、fixed=“”或nillable=“true”,因为它们在语义上都与空标记不同。一个空标记实际上不会有类型(比如字符串),因为它是空的。它也不会有空字符串的固定值,因为这不同于空字符串。nillable元素在语义上与空标记不同,因为它在语义上等同于缺少的标记(来源:Michael Kay的XSLT 2.0和XSLT 2.0程序员参考,第182页)。像

这样的空标记并不意味着在这种情况下不存在换行符。这意味着换行符存在,但没有内容或属性。

我不确定它是否强制执行关键限制<代码>不能包含其他元素或内容。谢谢!现在我正试图做相反的事情,即创建一个必须包含字符串的元素
。顺便说一句,
具有多个属性。
<?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>
<?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>