是否可以使用存储在字符串中的XSD shema验证SimpleXMLElement?使用XSD进行PHP SimpleXMLElement验证
我得到这个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 BTW存储在一个变量:
$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>'
我必须将字符串保存到xml文件才能使用xml读取器或? –
是的,我认为是。 (你可以直接写下这个文件,然后再删除它,但我不知道你是否想这样做) 当使用RelaxNG Schema时,你可以直接将模式字符串传递给setRelaxNGSchemaSource方法(但是,不知道放松NG,所以我不能告诉你这是否是一种好的方法)。 –
我刚刚找到[DOMDocument](http://php.net/manual/de/class.domdocument.php)类,它支持使用DOMDocument :: schemaValidateSource方法根据模式字符串验证XML。你可能想尝试一下。 –