如何解决使用递归XML模式进行的验证未在嵌套元素中找到预期的错误
我创建了一个具有长递归路径的XML模式:
Rectangle->Rectangle_t->Content_t->TopContent_t->[Rectangle or TextBlock]
其中以“ _t”结尾的名称是类型,而不是元素。我使用的是带有即时验证功能的XML编辑器,它知道TextBlock
中允许使用Rectangle
。但是,它不会抱怨嵌套TextBlock
上的无效属性(确实会在Rectangle
的同级上捕获它们)。我使用了多个验证器来确保它不仅是XML编辑器中的缺陷。
这是模式(很抱歉,它的长度实际上是归结为一些):
<?xml version="1.0" encoding="UTF-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- definition of attributes -->
<xs:attribute name="Name" type="xs:string"/>
<xs:attribute name="Height" type="xs:integer"/>
<xs:attribute name="Width" type="xs:integer"/>
<xs:attribute name="Top" type="xs:integer"/>
<xs:attribute name="Left" type="xs:integer"/>
<xs:attribute name="Text" type="xs:string"/>
<xs:attributeGroup name="Element">
<xs:attribute ref="Name"/>
<xs:attribute ref="Top" />
<xs:attribute ref="Left" />
</xs:attributeGroup>
<!-- definition of complex elements -->
<xs:complexType name="TopContent_t">
<xs:choice maxOccurs="unbounded">
<xs:element name="Rectangle" minOccurs="0" maxOccurs="unbounded" />
<xs:element name="TextBlock" minOccurs="0" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
<xs:element name="Project">
<xs:complexType>
<xs:choice minOccurs="1" maxOccurs="unbounded">
<xs:element name="Component" maxOccurs="unbounded" />
<xs:element name="Image" maxOccurs="unbounded" />
</xs:choice>
</xs:complexType>
</xs:element>
<xs:complexType name="Component_t">
<xs:complexContent>
<xs:extension base="TopContent_t">
<xs:attribute ref="Name" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Component" type="Component_t" />
<xs:complexType name="Image_t">
<xs:complexContent>
<xs:extension base="TopContent_t">
<xs:attribute ref="Name" use="required"/>
<xs:attribute ref="Height" use="required"/>
<xs:attribute ref="Width" use="required"/>
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Image" type="Image_t" />
<xs:complexType name="Content_t">
<xs:complexContent>
<xs:extension base="TopContent_t">
<xs:attributeGroup ref="Element" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:complexType name="Rectangle_t">
<xs:complexContent>
<xs:extension base="Content_t">
<xs:attribute ref="Height" use="required" />
<xs:attribute ref="Width" use="required" />
</xs:extension>
</xs:complexContent>
</xs:complexType>
<xs:element name="Rectangle" type="Rectangle_t" />
<xs:element name="TextBlock">
<xs:complexType>
<xs:attributeGroup ref="Element" />
<xs:attribute ref="Text" />
</xs:complexType>
</xs:element>
</xs:schema>
这里认为它是有效的XML文件,但不应为:
<?xml version="1.0" encoding="utf-8"?>
<Project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="ScreenDesigner.xsd">
<Image Name="Main Screen" Width="200" Height="250" Fill="White">
<Rectangle Name="Box" Width="30" Height="30">
<TextBlock foo="bar" ></TextBlock>
</Rectangle>
<TextBlock Text="testing"></TextBlock>
<Image Name="Not allowed" Height="0" Width="0"></Image>
</Image>
</Project>
嵌套在TexBlock
中的Rectangle
具有属性“ foo”,应将其标记为错误,但不是。如果我将该属性放在外部TextBlock
上,则会对其进行标记。另外,它允许Image
内包含Image
,这也应该是错误的。
这是我第一次尝试创建XML模式,希望能帮助我找出我做错了什么。
解决方法
当我通过Saxon的模式验证器运行此命令时,会收到以下形式的警告
Warning at xs:element on line 21 column 74 of test.xsd:
Local element declaration matches the name of a global element,and allows any content.
Did you mean to write ref='Rectangle' instead of name='Rectangle'? To suppress this
warning,add type='xs:anyType'
这些警告是解决问题的关键。当您编写没有<xs:element name="Rectangle">
属性的type
时,您是在定义一个允许任何内容的局部元素声明,而不是引用限制该内容的全局元素声明。一个容易犯的错误,因此是警告。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。