我有一个格式如下的XML文档:
<?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>
我正在尝试使用此XSD架构验证此文件:
<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>
所以DOMDocument :: schemaValidate()返回FALSE。命名空间中的这个问题(books-feed xmlns =“{NS-URL}”不等于schema xmlns)?或者我需要为xmlns book-feed插入xsd:attribute到schema(将导致“Invalid schema”警告)。或者是什么?
答案 0 :(得分:1)
实际问题应该记录在error_log中。
或者,请参阅此评论:http://www.php.net/manual/en/domdocument.schemavalidate.php#71014
要点:
设置libxml_use_internal_errors(true)
,然后使用libxml_get_errors()
检索错误。然后使用libxml_use_internal_errors(false)
答案 1 :(得分:0)
是的,您必须确保您的XSD的targetNamespace与根元素之一匹配。
<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>
假设您希望XSD与提供的XML匹配,那么您的有效XSD可能如下所示:
<?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>