【问题标题】:XSD does not return fault with DOMDocumentXSD 不返回 DOMDocument 错误
【发布时间】:2013-03-06 15:46:45
【问题描述】:

我正在尝试使用DOMDocument 在 PHP 中使用 XSD 验证 XML 文件。 translation 元素不能出现在我的 XML 中,但 schemaValidate 不会返回错误。我用另一个验证工具进行了验证,它返回一个错误。我不明白为什么schemaValidate 会验证错误的 XML。

XSD 的一部分

<xs:complexType name="CV">
  <xs:complexContent>
     <xs:restriction base="CE">
        <xs:sequence>
           <xs:element name="originalText" type="ED" minOccurs="0" maxOccurs="1">
              <xs:annotation>
                 <xs:documentation>
                    The text or phrase used as the basis for the coding.
                 </xs:documentation>
              </xs:annotation>
           </xs:element>
           <xs:element name="translation" type="CD" minOccurs="0" maxOccurs="0"/>
        </xs:sequence>
      </xs:restriction>
    </xs:complexContent>
  </xs:complexType>

我的 XML

<?xml version="1.0"?>
<CV><translation/></CV>

我的代码

$dom = new DOMDocument("1.0","UTF-8");
$dom->loadXML("<?xml version=\"1.0\"?>
<CV><translation></translation></CV>");
echo $dom->schemaValidate("TestClasses.xsd");

对于完整的 XSD TestClasses.xsd

感谢您的帮助。

【问题讨论】:

    标签: php xml xsd domdocument xsd-validation


    【解决方案1】:

    乍一看,这确实像是模式验证器中的一个错误。 PHP manual 建议(但没有明确说明)schemaValidate() 使用 libxml。而且libxml 的XSD 支持还没有完成。

    对于它的价值,我注意到注释掉该行

    <xs:element name="translation" type="CD" 
                minOccurs="0" maxOccurs="0"/>
    

    允许 libxml 在此处生成正确的结果,而不会使架构无效(据我从 Saxon 和 Xerces J 的快速测试中可以看出)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-05-21
      • 1970-01-01
      • 2011-08-03
      • 2012-02-27
      • 1970-01-01
      • 2012-05-18
      • 1970-01-01
      • 2021-08-09
      相关资源
      最近更新 更多