note 46482 added to function.dom-domdocument-relaxngvalidatesource
| From: | gem at rellim dot com | Date: | Tue, 12 Oct 2004 23:47:32 +0000 |
| Subject: | note 46482 added to function.dom-domdocument-relaxngvalidatesource | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-78455@lists.php.net to get a copy of this message | ||
Took me a while to get a working example. Here it is:
<?php
# enable warnings
ini_set( 'track_errors', 1);
ini_set('error_reporting', E_ALL | E_STRICT);
# this is a sample relaxNG definition
$rng = <<<EOT
<?xml version="1.0" encoding="UTF-8"?>
<grammar ns="" xmlns="http://relaxng.org/ns/structure/1.0"
datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes">
<start>
<element name="apple">
<element name="pear">
<data type="NCName"/>
</element>
</element>
</start>
</grammar>
EOT;
# well formed xml, but invalid per schema
# too many pears
$bad_xml =<<<EOT
<?xml version="1.0"?>
<apple>
<pear>Pear</pear>
<pear>Pear</pear>
</apple>
EOT;
# well formed xml and valid per schema
$good_xml =<<<EOT
<?xml version="1.0"?>
<apple>
<pear>Pear</pear>
</apple>
EOT;
# this function does the work, it tests the relaxNG in the string $rng
# against the xml in string $xml
Function relaxNG ( $xml, $rng ) {
$dom_xml = new DomDocument;
$dom_xml->loadXML($xml);
if ( $dom_xml->relaxNGValidateSource ( $rng ) ) {
echo "Good\n";
} else {
echo $php_errormsg . "\n";
}
}
# test the good xml, will echo:
# Good
relaxNG ($good_xml, $rng);
# test the bad xml, will echo:
# Did not expect element pear there
relaxNG ($bad_xml, $rng);
?>
results:
Good
Did not expect element pear there
----
Manual Page -- http://www.php.net/manual/en/function.dom-domdocument-relaxngvalidatesource.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46482
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46482&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46482&report=yes
Search -- http://master.php.net/manage/user-notes.php