Xml 使用`<;对嵌套元素进行不一致的XSD验证;xs:any>`
我正在开发一个工具来帮助用户编写与JSP文件性质类似的XHTML ish文档。文档是XML,可以包含XHTML名称空间中任何格式良好的标记,它们之间是来自我的产品名称空间的元素。除此之外,该工具还使用XSD验证输入 输入示例:Xml 使用`<;对嵌套元素进行不一致的XSD验证;xs:any>`,xml,validation,xsd,xml-namespaces,xmllint,Xml,Validation,Xsd,Xml Namespaces,Xmllint,我正在开发一个工具来帮助用户编写与JSP文件性质类似的XHTML ish文档。文档是XML,可以包含XHTML名称空间中任何格式良好的标记,它们之间是来自我的产品名称空间的元素。除此之外,该工具还使用XSD验证输入 输入示例: <?xml version="1.0"?> <markup> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<c:paragraph>
<span>This is a test!</span>
<a href="http://www.google.com/">click here for more!</a>
</c:paragraph>
</c:section>
</html>
</markup>
但是,验证失败:
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</div>
</c:section>
</html>
</markup>
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:paragraph my-invalid-attr="true">This is a test</c:paragraph>
</div>
</c:section>
</html>
</markup>
xmllint --schema markup.xsd example.xml
<html>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</html>
<div>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</div>
以下是我的XSD文件:
文件:markup.xsd
文件:my_tag_lib.xsd
未声明
div
元素,因此没有任何东西可以阻止if不接受架构中的无效类型,并且段落
元素不允许我的无效属性
也许一些例子可以让这一点更清楚
如果元素被声明(例如html
,节
,段落
),并且其内容来自taglib名称空间(您声明为具有processContents=“strict”
),则它们将被视为strict。这意味着必须声明属性或子元素。这将导致验证失败:
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</div>
</c:section>
</html>
</markup>
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<c:paragraph my-invalid-attr="true">This is a test</c:paragraph>
</div>
</c:section>
</html>
</markup>
xmllint --schema markup.xsd example.xml
<html>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</html>
<div>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</div>
由于c:my invalid element
也未声明,因此它将允许任何内容或属性。这是有效的:
<div>
<c:my-invalid-element invalid-attribute="hi"> <!-- VALID -->
<c:invalid></c:invalid>
<html></html>
</c:my-invalid-element>
</div>
如果在声明的元素中使用未声明的属性(该属性将不匹配xs:any
),则无论嵌套有多深,都会发生同样的情况:
<div>
<c:my-invalid-element invalid-attribute="hi"> <!-- VALID -->
<c:invalid>
<b>
<c:section bad-attribute="boo"></c:section> <!-- FAILS! -->
...
...
您缺少的是对问题的理解
首先,看看这个小实验
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<html>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</html>
</div>
</c:section>
</html>
</markup>
这是一个测试
这与您的有效示例相同,只是现在我将c:MyInvalid元素的评估上下文从“lax”更改为“strict”。这是通过插入html元素来完成的,现在它强制标记名称空间中的所有元素都是严格的。正如你可以很容易地确认的那样,上述条款是无效的
这告诉您(不必阅读)在您的示例中,确定的上下文必须是“宽松的”,而不是您的期望,即“严格的”
为什么上下文松散
div
被“宽松地”处理(它匹配通配符,但不存在它的定义),因此它的子项将被宽松地评估。与lax的意思相匹配:在第一种情况下,没有找到c:my invalid element
的定义,因此给出的指令是,如果不能,请不要担心-都很好。在无效示例中,可以找到c:paragration
的定义,因此对于该定义必须是有效的-不好,因为有意外的属性。所以无法强制一个名称空间中的所有元素都是严格的-都基于当前上下文?
<div>
<c:my-invalid-element invalid-attribute="hi"> <!-- VALID -->
<c:invalid>
<b>
<c:section bad-attribute="boo"></c:section> <!-- FAILS! -->
...
<?xml version="1.0"?>
<markup>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:c="https://my_tag_lib.example.com/">
<c:section>
<div>
<html>
<c:my-invalid-element>This is a test</c:my-invalid-element>
</html>
</div>
</c:section>
</html>
</markup>