Bug #78352 [NEW]: schemaValidate ignores namespaces dynamically added to a DOMDocument
| From: | fred5 at originsystems dot co dot za | Date: | Mon, 29 Jul 2019 20:31:02 +0000 |
| Subject: | Bug #78352 [NEW]: schemaValidate ignores namespaces dynamically added to a DOMDocument | ||
| Groups: | php.bugs | ||
| Request: | Send a blank email to php-bugs+get-222007@lists.php.net to get a copy of this message | ||
From: fred5 at originsystems dot co dot za
Operating system: CentOS 7
PHP version: 7.3.7
Package: DOM XML related
Bug Type: Bug
Bug description:schemaValidate ignores namespaces dynamically added to a DOMDocument
Description:
------------
When building an XML document node by node via DOMDdocument, the
DOMDocument::schemaValidate/Source() methods incorrectly ignore
xmlns:... attributes, breaking the validation.
However, if DOMDocument::loadXML($xml) is used to create the
DOMDocument, then the xmlns:... attributes are respected.
It is an elusive bug to identify when working with complex XML documents
and schemas however I have put together a very simple code sample
illustrating the issue clearly.
This issue was identified here 10 years ago:
https://www.php.net/manual/en/domdocument.schemavalidate.php#89893
and still persists.
Test script:
---------------
<?php
function printComment(string $text) : void {
print PHP_EOL . str_pad("-",5, "-") . " {$text} " . PHP_EOL;
}
$xsd = '<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://purl.oclc.org/ooxml/wordprocessingml/main"
targetNamespace="http://purl.oclc.org/ooxml/wordprocessingml/main"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="document"/>
</xs:schema>';
printComment("First, a super simple schema.");
print $xsd . PHP_EOL;
printComment("Now, let's use DOM to build an XML document that is valid
for the schema...");
$dom = new DOMDocument();
$dom->appendChild($dom->createElement("w:document"));
$dom->documentElement->setAttributeNS("http://www.w3.org/2000/xmlns/","xmlns:w","http://purl.oclc.org/ooxml/wordprocessingml/main");
printComment("Great, here's our created XML document. (with 'w:document'
referencing xmlns:w as desired)");
print $dom->saveXML();
printComment("Let's validate our new document against our schema...");
printComment(($dom->schemaValidateSource($xsd)?"Validation
succeeded!":"Validation failed. Oh dear, this is an unhappy suprise, the
XML looks fine to me!"));
printComment("Now lets refresh our DOMDocument with a full reload of its
own XML content");
$dom->loadXML($dom->saveXML());
printComment("Let's try validating it again against our schema...");
printComment(($dom->schemaValidateSource($xsd)?"Validation succeeded.
How is this possible, nothing has changed in the XML!":"As expected we
failed again - it's weird but at least the behaviour is consistent."));
Expected result:
----------------
----- First, a super simple schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://purl.oclc.org/ooxml/wordprocessingml/main"
targetNamespace="http://purl.oclc.org/ooxml/wordprocessingml/main"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="document"/>
</xs:schema>
----- Now, let's use DOM to build an XML document that is valid for the
schema...
----- Great, here's our created XML document. (with 'w:document'
referencing xmlns:w as desired)
<?xml version="1.0"?>
<w:document
xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main"/>
----- Let's validate our new document against our schema...
----- Validation succeeded!
----- Now lets refresh our DOMDocument with a full reload of its own
XML content
----- Let's try validating it again against our schema...
----- Validation succeeded. How is this possible, nothing has changed
in the XML!
Actual result:
--------------
----- First, a super simple schema.
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns="http://purl.oclc.org/ooxml/wordprocessingml/main"
targetNamespace="http://purl.oclc.org/ooxml/wordprocessingml/main"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="document"/>
</xs:schema>
----- Now, let's use DOM to build an XML document that is valid for the
schema...
----- Great, here's our created XML document. (with 'w:document'
referencing xmlns:w as desired)
<?xml version="1.0"?>
<w:document
xmlns:w="http://purl.oclc.org/ooxml/wordprocessingml/main"/>
----- Let's validate our new document against our schema...
Warning: DOMDocument::schemaValidateSource(): Element 'w:document': No
matching global declaration available for the validation root. in
C:\OriginSystems\Git\Test-me\DOCX_schemaValidate\validationError.php on
line 24
----- Validation failed. Oh dear, this is an unhappy suprise, the XML
looks fine to me!
----- Now lets refresh our DOMDocument with a full reload of its own
XML content
----- Let's try validating it again against our schema...
----- Validation succeeded. How is this possible, nothing has changed
in the XML!
--
Edit bug report at https://bugs.php.net/bug.php?id=78352&edit=1
--
Try a snapshot (PHP 5.4): https://bugs.php.net/fix.php?id=78352&r=trysnapshot54
Try a snapshot (PHP 5.5): https://bugs.php.net/fix.php?id=78352&r=trysnapshot55
Try a snapshot (trunk): https://bugs.php.net/fix.php?id=78352&r=trysnapshottrunk
Fixed in SVN: https://bugs.php.net/fix.php?id=78352&r=fixed
Fixed in release: https://bugs.php.net/fix.php?id=78352&r=alreadyfixed
Need backtrace: https://bugs.php.net/fix.php?id=78352&r=needtrace
Need Reproduce Script: https://bugs.php.net/fix.php?id=78352&r=needscript
Try newer version: https://bugs.php.net/fix.php?id=78352&r=oldversion
Not developer issue: https://bugs.php.net/fix.php?id=78352&r=support
Expected behavior: https://bugs.php.net/fix.php?id=78352&r=notwrong
Not enough info: https://bugs.php.net/fix.php?id=78352&r=notenoughinfo
Submitted twice: https://bugs.php.net/fix.php?id=78352&r=submittedtwice
register_globals: https://bugs.php.net/fix.php?id=78352&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=78352&r=php4
Daylight Savings: https://bugs.php.net/fix.php?id=78352&r=dst
IIS Stability: https://bugs.php.net/fix.php?id=78352&r=isapi
Install GNU Sed: https://bugs.php.net/fix.php?id=78352&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=78352&r=float
No Zend Extensions: https://bugs.php.net/fix.php?id=78352&r=nozend
MySQL Configuration Error: https://bugs.php.net/fix.php?id=78352&r=mysqlcfg