如何使元素在XML模式中成为自己的子元素?
我希望能够对同一父元素的子元素进行任意级别的嵌套,例如:如何使元素在XML模式中成为自己的子元素?,xml,schema,nested,Xml,Schema,Nested,我希望能够对同一父元素的子元素进行任意级别的嵌套,例如: <path expr="/"> <path expr="usr"> <path expr="bin"> <path expr="X11" /> </path> </path> <path expr="var" /> </path> 我正在编写XML模式文件,我不知道如何在模式中表示这种父/子关系:下
<path expr="/">
<path expr="usr">
<path expr="bin">
<path expr="X11" />
</path>
</path>
<path expr="var" />
</path>
我正在编写XML模式文件,我不知道如何在模式中表示这种父/子关系:下面是我所拥有的,但它不是有效的模式定义:
<xs:element name="path">
<xs:complexType>
<xs:sequence>
<xs:element ref="path" minOccurs="0" />
</xs:sequence>
<xs:attribute name="expr" type="xs:string" use="required" />
</xs:complexType>
</xs:element>
更新:谢谢你的回复。我试过了,但得到了以下错误:“w3.org/2001/XMLSchema:complexType”元素在此上下文中不受支持。我应该提到,我所描述的路径层次结构本身就是一个名为application的元素的子元素,因此整个结构类似于:
<application name="test">
<path expr="/">
<path expr="usr">
<path expr="bin">
<path expr="X11" />
</path>
</path>
<path expr="var" />
</path>
</application>
以下几点应该可以做到。 XSD标准很难直接使用,我总是使用像这样的编辑器
(来源:) XSD是有效的。对于您描述的新XML,您需要将其更改为如下所示
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio - Developer Pro Edition 7.1.0.1135 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="path" type="PathType" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:complexType name="PathType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="path" type="PathType" />
</xs:sequence>
<xs:attribute name="expr" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>
就个人而言,我更喜欢XML模式。可能值得您花时间试用。谢谢您的回复。我试过了,但得到了以下错误:“”元素在此上下文中不受支持。我应该指出,我所描述的路径层次结构本身是名为application的元素的子元素,因此,整个结构类似于此:@Nick Davis:最好在问题本身中添加类似于此的澄清(您可以随时编辑它)。在评论中,您没有格式,并且潜在的重要细节无法查看。感谢您的提示--我已经用我的评论编辑了原始问题。
<?xml version="1.0" encoding="utf-8" ?>
<!--Created with Liquid XML Studio - Developer Pro Edition 7.1.0.1135 (http://www.liquid-technologies.com)-->
<xs:schema elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Application">
<xs:complexType>
<xs:sequence>
<xs:element name="path" type="PathType" />
</xs:sequence>
<xs:attribute name="name" type="xs:string" />
</xs:complexType>
</xs:element>
<xs:complexType name="PathType">
<xs:sequence>
<xs:element minOccurs="0" maxOccurs="unbounded" name="path" type="PathType" />
</xs:sequence>
<xs:attribute name="expr" type="xs:string" use="required" />
</xs:complexType>
</xs:schema>