对于xsd:any,processContents strict vs lax vs skip

Cai*_*der 11 xml xsd

master.xsd:

<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.gworks.cn/waf_profile"
    xmlns:tns="http://www.gworks.cn/waf_profile" elementFormDefault="qualified">
    <element name="profile">
        <complexType>
            <sequence>
                <element name="aspect">
                    <complexType>
                        <sequence minOccurs="1" >
                            <any processContents="strict" />
                        </sequence>
                        <attribute name="id" type="string" use="required"></attribute>
                        <attribute name="class" type="string" use="required"></attribute>
                        <attribute name="desc" type="string" use="optional"></attribute>
                    </complexType>
                </element>
            </sequence>
            <attribute name="name" type="string" use="required"></attribute>
        </complexType>
    </element>
</schema>
Run Code Online (Sandbox Code Playgroud)

我是否可以针对此架构编写XML文件,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<profile name="??" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://www.gworks.cn/waf_profile"
    xsi:schemaLocation="http://www.gworks.cn/waf_profile http://www.gworks.cn/waf_profile.xsd">
    <aspect id="security" class="cn.gworks.waf.config.SecurityConfig" desc="????">
        <security xsi:schemaLocation="http://www.gworks.cn/config_security http://www.gworks.cn/config_security.xsd">
            <authService impl="com.bgzchina.ccms.security.SSOAuthService" enabled="true">
                <certificate>
                    <field name="Token" isKey="true" />
                </certificate>
            </authService>
            <authService impl="com.bgzchina.ccms.security.NoAuthService" enabled="true">
                <certificate>
                    <field name="username" isKey="true" />
                </certificate>
            </authService>
        </security>
    </aspect>
</profile>
Run Code Online (Sandbox Code Playgroud)

子元素"security"具有自己的架构定义.

kjh*_*hes 18

因为XSD指定

<any processContents="strict" />
Run Code Online (Sandbox Code Playgroud)

在内容模型中aspect,您的XML因此而无效processContents="strict",这要求XML处理器必须能够获取XSD定义,在这种情况下,security 必须能够验证它.

如果你改成这个

<any processContents="lax" />
Run Code Online (Sandbox Code Playgroud)

您的XML将是有效的,如果您security在XSD中定义,则将在验证期间使用该定义.(如果找不到定义,您的文档仍将被视为有效.)这要求内容仅在XML处理器可以找到其定义时才有效.

如果你改成这个

<any processContents="skip" />
Run Code Online (Sandbox Code Playgroud)

您的XML将是有效和XML处理器不会试图验证以下儿童的内容aspect(除需要它是其他一些对于单元素sequence约束).

笔记: