将 xmlns:xsi 属性命名空间添加到根元素时出错
我有一个名为sample.xml
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
和sample.xsd的xml来验证sample.xml
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
我正在尝试通过我的php代码验证sample.xml,并且由于sample.xml中没有命名空间我正在从php代码添加命名空间。
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$file = 'sample.xml';
$schema = 'sample.xsd';
$doc = new DOMDocument();
$doc->load($file);
$doc->createAttributeNS('http://www.w3schools.com', 'xmlns');
$doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');
$doc->createAttributeNS('http://www.w3schools.com sample.xsd', 'xsi:schemaLocation');
print $doc->saveXML();
if ($doc->schemaValidate($schema)) {
print "$file is valid.\n";
} else {
print "$file is invalid.\n";
}
?>
当我运行代码时出现以下错误
致命错误:未捕获异常“DOMException”,消息为“命名空间” /var/www/xsd/test_sample.php 中的错误:20 堆栈跟踪:#0 /var/www/xsd/test_sample.php(20): DOMDocument->createAttributeNS('http://www.w3.o...', 'xmlns:xsi') #1 {main} 抛出在 /var/www/xsd/test_sample.php 第 20 行
如果我注释掉行 $doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance),则 ', 'xmlns:xsi');
代码运行正常,但我无法验证 xml,因为它说 sample.xml 无效
有人可以帮我解决这个问题吗 代码?
I have a xml called sample.xml
<?xml version="1.0"?>
<note>
<to>Tove</to>
<from>Jani</from>
<heading>Reminder</heading>
<body>Don't forget me this weekend!</body>
</note>
and sample.xsd to validate sample.xml
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.w3schools.com"
xmlns="http://www.w3schools.com"
elementFormDefault="qualified">
<xs:element name="note">
<xs:complexType>
<xs:sequence>
<xs:element name="to" type="xs:string"/>
<xs:element name="from" type="xs:string"/>
<xs:element name="heading" type="xs:string"/>
<xs:element name="body" type="xs:string"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
I'm trying to validate sample.xml through my php code and since there are no namespaces in sample.xml I'm adding the namespace from the php code.
<?php
error_reporting(E_ALL);
ini_set('display_errors', 'On');
$file = 'sample.xml';
$schema = 'sample.xsd';
$doc = new DOMDocument();
$doc->load($file);
$doc->createAttributeNS('http://www.w3schools.com', 'xmlns');
$doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');
$doc->createAttributeNS('http://www.w3schools.com sample.xsd', 'xsi:schemaLocation');
print $doc->saveXML();
if ($doc->schemaValidate($schema)) {
print "$file is valid.\n";
} else {
print "$file is invalid.\n";
}
?>
and when I run the code I get the following error
Fatal error: Uncaught exception 'DOMException' with message 'Namespace
Error' in /var/www/xsd/test_sample.php:20 Stack trace: #0
/var/www/xsd/test_sample.php(20):
DOMDocument->createAttributeNS('http://www.w3.o...', 'xmlns:xsi') #1
{main} thrown in /var/www/xsd/test_sample.php on line 20
if I comment out the line $doc->createAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'xmlns:xsi');
the code runs fine but then I'm not able to validate the xml as it says sample.xml is invalid
Can some one help me with this code?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我要解决的第一件事是在您的 appendChild 上使用href="http://www.php.net/manual/en/class.domdocument.php#domdocument.props.documentelement" rel="nofollow">文档元素。如果没有它,该属性将被创建但不会附加到您的树中。
如需验证,请查看此链接。
First thing I would fix would be to also use appendChild on your document element. Without that, the attribute is created but not appended to your tree.
For validation, please take a look at this link.
澄清 Searock 的困惑,为什么在使用 createAttribute 时需要使用appendChild,而在使用 createAttributeNS 时则不需要。
尽管命名相似,但这两个函数的内部功能设计不同:
createAttributeNS() 自动将命名空间属性附加到文档。
createAttribute() 必须附加appendChild()。
Point of clarification on Searock confusion on why it is necessary to use appendChild when using createAttribute but not when using createAttributeNS.
The internal functionality of the two functions are designed differently even though they are named similarly:
createAttributeNS() automatically appends the NameSpace Attribute to the document.
createAttribute() must be appended with appendChild().