Dynamic XML Schema Validates Subsection of Document

时间秒杀一切 提交于 2019-12-09 02:19:29

I got this working in the end. The reality is that I think I was doing a few little things wrong, that meant I went around in circless. Eventually I was able to produce the following - the only thing missing was elementFormDefault="qualified" in the xs:schema atributes, and that the processContents should be strict if the schemas are validating correctly.

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.message.org"
           elementFormDefault="qualified">
     <xs:element name="MESSAGES">
         <xs:complexType>
             <xs:sequence>
                 <xs:element name="MESSAGE" maxOccurs="unbounded">
                     <xs:complexType>
                         <xs:sequence>
                             <xs:element name="RECIPIENT" type="xs:string"/>
                             <xs:any processContents="strict"/>
                         </xs:sequence>
                    </xs:complexType>
                 </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>

Armed with this XSD I was able to get the following XML to work:

<MESSAGES xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
          xmlns="http://www.messages.org"
          xsi:schemaLocation="http://www.messages.org xsd/messages.xsd">
    <MESSAGE>
         <RECIPIENT>Foo</RECIPIENT>
         <PAYLOAD xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                  xmlns="http://www.payload.org"
                  xsi:schemaLocation="http://www.payload.org xsd/payload-type-a.xsd">
              <!-- Large message containing many possible elements -->
         </PAYLOAD>
    <MESSAGE>
</MESSAGES>

Finally it's worth noting that you can add multiple schemas at the top of the XML and then select them by altering the xmlns just like the above example:

<MESSAGES xsi:schemaLocation="http://www.messages.org xsd/messages.xsd
                              http://www.payload.org xsd/payload.xsd>"
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!