views:

74

answers:

2
+2  Q: 

PHP XML Validation

What's the best way to validate an XML file (or a portion of it) against multiple XSD files?

For example, I have the following schema for a configuration loader:

<xsd:schema xmlns="http://www.kauriproject.org/schema/configuration"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    targetNamespace="http://www.kauriproject.org/schema/configuration"
    elementFormDefault="qualified">

    <xsd:element name="configuration" type="configuration" />

    <xsd:complexType name="configuration">
        <xsd:choice maxOccurs="unbounded">
            <xsd:element name="import" type="import" minOccurs="0" maxOccurs="unbounded" />
            <xsd:element name="section" type="section" />
        </xsd:choice>
    </xsd:complexType>

    <xsd:complexType name="section">
        <xsd:sequence>
            <xsd:any minOccurs="0" maxOccurs="unbounded" processContents="lax" />
        </xsd:sequence>

        <xsd:attribute name="name" type="xsd:string" use="required" />
        <xsd:attribute name="type" type="xsd:string" use="required" />
    </xsd:complexType>

    <xsd:complexType name="import" mixed="true">
        <xsd:attribute name="resource" type="xsd:string" />
   </xsd:complexType>
</xsd:schema>

As the Configuration class exists now, it lets one add a <section> tag with a define concrete parser class (much like custom configuration sections in ASP.NET). However, I'm unsure of how to validate the section being parsed.

If it possible to validate just this section of code with an XSD file/string without writing it back to a file?

A: 

Instead of thinking of it as one large XML file, think of it in terms of chunks. The custom XML sections aren't part of your main file's schema, its just data. Trying validating with this XSD file, then when you load the custom section validate that XML with another XSD file.

Kevin Wiskia
A: 

I was solving the same problem and I just simply merged the files (it was possible in my case as it was a really simple one).

Link: http://stackoverflow.com/questions/2746008/is-it-possible-to-validate-xml-against-multiple-schemas-in-php

MartyIX