是否可以使用存储在字符串中的 XSD 模式来验证 SimpleXMLElement?
我通过 CURL 得到这个 xml:
<production_feedback>
<production_number>DA1100208</production_number>
<production_status>DONE</production_status>
</production_feedback>
在我这边,我是这样理解的:
if ( $_SERVER['REQUEST_METHOD'] === 'POST' ){
$post_text = file_get_contents('php://input');
$xml = new SimpleXMLElement($post_text);
error_log(print_r($xml , true));
}
这是在我的 error_log()
中:
SimpleXMLElement Object\n(\n [production_number] => DA1100208\n [production_status] => PRODUCTION_IN_PROGRESS\n)\n
所以我可以使用 Xpath 访问数据。这很好用。我想用 XSD 验证它。是否有可能,或者是否有任何其他方法 2 使用 XSD 字符串验证 XML 字符串?
顺便说一句,这是我的 XSD 存储在一个变量中:
$production_XSD ='<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="production_feedback">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="production_number"/>
<xs:element type="xs:string" name="production_status"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>'
最佳答案
SimpleXMLElement 类不支持(只要 php.net 上的文档是最新的)。
DOMDocument使用 DOMDocument::schemaValidateSource 方法提供您正在寻找的功能。
----原创
XMLReader然而,类具有 setSchema 方法,可用于针对 XSD 文件 进行验证(这不完全是您要查找的内容,但这是我在不依赖任何外部库的情况下发现的内容)
关于使用 XSD 进行 PHP SimpleXMLElement 验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12507331/