jjzjj

xml - processContents strict vs lax vs skip for xsd :any

coder 2024-06-23 原文

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>

我可以像这样针对这个模式写一个 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>

其中子元素“security”定义了自己的模式。

最佳答案

因为XSD指定

<any processContents="strict" />

aspect的内容模型中,由于processContents="strict",你的XML是无效的,这要求XML处理器必须能够获取XSD 定义,在这种情况下,安全 并且必须能够验证它

如果你把这个改成

<any processContents="lax" />

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

如果你把这个改成

<any processContents="skip" />

您的 XML 将是有效的,XML 处理器将不会尝试验证aspect 下的子内容(除了要求它是一些 每个 sequence 约束的单个元素。

注意事项:

关于xml - processContents strict vs lax vs skip for xsd :any,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27420156/

有关xml - processContents strict vs lax vs skip for xsd :any的更多相关文章

随机推荐