mikatakana
mikatakana

Reputation: 523

Invalid XSD Schema in DOMDocument validation

I have an XML document with such format:

<?xml version="1.0" encoding="utf-8"?>
<books-feed xmlns="{NS-URL}">
<generation-date>{DATE}</generation-date>
<book id="{BOOK-ID}">
    <title>{BOOK-TITLE}</title>
    <author>{BOOK-AUTHOR}</author>
    .......................................... ← other information tags [any]
</book>
.............................................. ← other <book> elements
</books-feed>

And I'm trying to validate this file with this XSD-schema:

<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:element name="books-feed">
    <xsd:complexType>
        <xsd:sequence>
            <xsd:element name="generation-date" type="xsd:string"/>
            <xsd:element name="book" maxOccurs="unbounded">
                <xsd:complexType>
                    <xsd:sequence>
                        <xsd:any maxOccurs="unbounded" processContents="lax"/>
                    </xsd:sequence>
                    <xsd:attribute name="id" type="xsd:integer" use="required"/>
                </xsd:complexType>
            </xsd:element>
        </xsd:sequence>
    </xsd:complexType>
</xsd:element>
</xsd:schema>

So DOMDocument::schemaValidate() returns FALSE. Is this problem in namespaces (books-feed xmlns="{NS-URL}" not equals schema xmlns)? Or I need insert xsd:attribute for xmlns book-feed into schema (in will cause "Invalid schema" warning). Or what?

Upvotes: 2

Views: 5629

Answers (2)

Petru Gardea
Petru Gardea

Reputation: 21638

Yes, you have to make sure that you have an XSD with a targetNamespace which matches the one of the root element.

<xsd:schema xmlns="{NS-URL}" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="{NS-URL}" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="books-feed">
          ....
  </xsd:element>
</xsd:schema>

Assuming you want your XSD to match the provided XML, then your valid XSD may look as below:

<?xml version="1.0" encoding="utf-8"?>
<!--XML Schema generated by QTAssistant/XML Schema Refactoring (XSR) Module (http://www.paschidev.com)-->
<xsd:schema xmlns="{NS-URL}" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="{NS-URL}" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <xsd:element name="books-feed">
    <xsd:complexType>
      <xsd:sequence>
        <xsd:element name="generation-date" type="xsd:string" />
        <xsd:element maxOccurs="unbounded" name="book">
          <xsd:complexType>
            <xsd:sequence>
              <xsd:element name="title" type="xsd:string" />
              <xsd:element name="author" type="xsd:string" />
                    <xsd:any maxOccurs="unbounded" processContents="lax" minOccurs="0"/>
            </xsd:sequence>
            <xsd:attribute name="id" type="xsd:string" use="required" />
          </xsd:complexType>
        </xsd:element>
      </xsd:sequence>
    </xsd:complexType>
  </xsd:element>
</xsd:schema>

Upvotes: 0

DanMan
DanMan

Reputation: 11561

The the actual problem should be logged in your error_log.

Alternatively, see this comment: http://www.php.net/manual/en/domdocument.schemavalidate.php#71014

The gist:

Set libxml_use_internal_errors(true) and then retrieve the errors with libxml_get_errors(). Afterwards turn it off again, using libxml_use_internal_errors(false)

Upvotes: 2

Related Questions