【问题标题】:validate a xml file against a xsd using php使用 php 针对 xsd 验证 xml 文件
【发布时间】:2012-07-23 22:08:51
【问题描述】:

如何根据 xsd 验证 xml 文件?有 domdocument::schemaValidate() 但它不告诉错误在哪里。有什么课程吗?从头开始制作该解析器是否值得?还是只是重新发明他的轮子,

【问题讨论】:

    标签: php xml validation xsd xml-parsing


    【解决方案1】:

    这段代码做生意:

    $xml= new DOMDocument();
    $xml->loadXML(<A string goes here containing the XML data>, LIBXML_NOBLANKS); // Or load if filename required
    if (!$xml->schemaValidate(<file name for the XSD file>)) // Or schemaValidateSource if string used.
    {
       // You have an error in the XML file
    }
    

    查看http://php.net/manual/en/domdocument.schemavalidate.php中的代码以检索错误。

    贾斯汀在 redwiredesign dot com 2006 年 11 月 8 日 03:32 发布。

    【讨论】:

    • 我认为它无法说明错误在哪里、在哪里失败以及为什么失败。是吗?
    • 不幸的是,PHP 版本使用了不支持 XSLT 2.0 规范的 XSD 的 libxml2...
    【解决方案2】:

    来自http://php.net/manual/en/domdocument.schemavalidate.php的用户贡献

    它就像一个魅力!

    有关来自 DOMDocument::schemaValidate 的更详细反馈,请禁用 libxml 错误并自己获取错误信息。看 http://php.net/manual/en/ref.libxml.php 了解更多信息。

    example.xml

    <?xml version="1.0"?>
    <example>
        <child_string>This is an example.</child_string>
        <child_integer>Error condition.</child_integer>
    </example>
    

    example.xsd

    <?xml version="1.0"?>
    <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
    elementFormDefault="qualified">
        <xs:element name="example">
            <xs:complexType>
                <xs:sequence>
                    <xs:element name="child_string" type="xs:string"/>
                    <xs:element name="child_integer" type="xs:integer"/>
                </xs:sequence>
            </xs:complexType>
        </xs:element>
    </xs:schema>
    

    PHP

    <?php
    
    function libxml_display_error($error)
    {
        $return = "<br/>\n";
        switch ($error->level) {
            case LIBXML_ERR_WARNING:
                $return .= "<b>Warning $error->code</b>: ";
                break;
            case LIBXML_ERR_ERROR:
                $return .= "<b>Error $error->code</b>: ";
                break;
            case LIBXML_ERR_FATAL:
                $return .= "<b>Fatal Error $error->code</b>: ";
                break;
        }
        $return .= trim($error->message);
        if ($error->file) {
            $return .=    " in <b>$error->file</b>";
        }
        $return .= " on line <b>$error->line</b>\n";
    
        return $return;
    }
    
    function libxml_display_errors() {
        $errors = libxml_get_errors();
        foreach ($errors as $error) {
            print libxml_display_error($error);
        }
        libxml_clear_errors();
    }
    
    // Enable user error handling
    libxml_use_internal_errors(true);
    
    $xml = new DOMDocument();
    $xml->load('example.xml');
    
    if (!$xml->schemaValidate('example.xsd')) {
        print '<b>DOMDocument::schemaValidate() Generated Errors!</b>';
        libxml_display_errors();
    }
    
    ?>
    

    【讨论】:

    • 看起来像ibm.com/developerworks/library/x-validxphp上的代码。
    • 很抱歉多年后才发布,但是您如何更改上面的代码来验证依赖于其他架构文件 (xsd) 的架构文件 (xsd)?
    • 多年后再次 ;) 如果您使用 schemaValidate 它将解决依赖关系,如果您使用 schemaValidateSource 它不会。如果您遇到问题,您可能需要检查依赖项的相对路径。
    【解决方案3】:

    这是显示xsd验证错误的完整代码sn-p:

        $xml = '<test/>';
        $xsd = '/path/to/xsd';
        // needed for getting errors
        libxml_use_internal_errors(true);
    
        $domDocument= new DOMDocument();
        $domDocument->loadXML($xml); 
        if (!$domDocument->schemaValidate($xsd)) {
            $errors = libxml_get_errors();
            foreach ($errors as $error) {
                print_r($error);
            }
            libxml_clear_errors();
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-10-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-01-23
      相关资源
      最近更新 更多