15

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>

Can I write a XML file against this schema like this:

<?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>

where the child element "security" has its own schema defined.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
CaiNiaoCoder
  • 3,269
  • 9
  • 52
  • 82

1 Answers1

25

Because the XSD specifies

<any processContents="strict" />

in the content model of aspect, your XML is invalid due to processContents="strict", which requires that the XML processor must be able to obtain the XSD definition for, in this case, security and must be able to validate it.

If you change this to

<any processContents="lax" />

your XML will be valid, and if you come to define security in your XSD, the definition will be used during validation. (If the definition cannot be found, your document will still be considered to be valid.) This requires that the content be valid only if XML processor can find its definition.

If you change this to

<any processContents="skip" />

your XML will be valid and the XML processor will make no attempt to validate the children content under aspect (other than that requiring it to be some single element per the sequence constraint).

Notes:

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Is that valid to specify a xsi:schemaLocation attribute on my "security" element ? – CaiNiaoCoder Dec 12 '14 at 02:38
  • No, you would bring the XSD containing the declaration of `security` into your master XSD via `xsi:schemaLocation` attribute on `xsd:import` or `xsd:include` as I mentioned in the answer. – kjhughes Dec 12 '14 at 02:52
  • But I want to let user to specify the schema for the arbitrary element. – CaiNiaoCoder Dec 12 '14 at 07:05
  • A type can be specified for an element in an XML document via `xsi:type`, but the type must defined in the XSDs. XSDs are composed using `xsd:import` and `xsd:include`. You can use `xsi:schemaLocation` with those. You can also use `xsi:schemaLocation` on the root element of an XML document instance. – kjhughes Dec 12 '14 at 13:21