I need to take in XML and validate it against a schema file. Afterward i must call a function based on the command (example updateContactList). What is the best way to do this? I am worried about validating the XML (and report errors) and i have no idea what is the best way to put the data into a function to run
我需要接收XML并针对一个模式文件验证它。之后,我必须根据命令调用一个函数(示例updateContactList)。最好的方法是什么?我担心验证XML(以及报告错误),我不知道将数据放入函数中运行的最佳方式是什么
-edit- NOTE: By validating the schema i need to validate the (regex) pattern. It would be great if i can call a function with the XML and schema and have it return false + error msg or true
-编辑-注意:通过验证模式,我需要验证(regex)模式。如果我能用XML和模式调用一个函数,并让它返回false + error msg或true,那就太好了
1 个解决方案
#1
7
I'm not sure what you mean by regex pattern? The most common way to validate an XML document is trough an XSD. You can use DomDocument->schemaValidate
for this:
我不知道你说的regex模式是什么意思?验证XML文档的最常见方法是使用XSD。您可以使用DomDocument->schemaValidate进行以下操作:
$doc = new DOMDocument();
$doc->load($tempFile);
$doc->schemaValidate('schema.xsd');
There is also the corresponding DomDocument->relaxNGValidate
for validating against the lesser used RelaxNG schema.
还有相应的DomDocument->弛缓性验证,用于针对使用较少的弛缓性模式进行验证。
You may also want to use the error-handler functions for libxml, if you plan on capturing the errors and doing something about them, rather than just validate true or false. In essence, call libxml_use_internal_errors(true);
before loading and validating the document, and use libxml_get_errors
and display_xml_error
to fetch the errors.
如果您打算捕获错误并对其进行处理,而不是仅仅验证true或false,那么您可能还希望使用libxml的错误处理程序函数。从本质上讲,叫libxml_use_internal_errors(真正的);在加载和验证文档之前,使用libxml_get_errors和display_xml_error获取错误。
#1
7
I'm not sure what you mean by regex pattern? The most common way to validate an XML document is trough an XSD. You can use DomDocument->schemaValidate
for this:
我不知道你说的regex模式是什么意思?验证XML文档的最常见方法是使用XSD。您可以使用DomDocument->schemaValidate进行以下操作:
$doc = new DOMDocument();
$doc->load($tempFile);
$doc->schemaValidate('schema.xsd');
There is also the corresponding DomDocument->relaxNGValidate
for validating against the lesser used RelaxNG schema.
还有相应的DomDocument->弛缓性验证,用于针对使用较少的弛缓性模式进行验证。
You may also want to use the error-handler functions for libxml, if you plan on capturing the errors and doing something about them, rather than just validate true or false. In essence, call libxml_use_internal_errors(true);
before loading and validating the document, and use libxml_get_errors
and display_xml_error
to fetch the errors.
如果您打算捕获错误并对其进行处理,而不是仅仅验证true或false,那么您可能还希望使用libxml的错误处理程序函数。从本质上讲,叫libxml_use_internal_errors(真正的);在加载和验证文档之前,使用libxml_get_errors和display_xml_error获取错误。