PHP Doku:: Performs relaxNG validation on the document - domdocument.relaxngvalidatesource.html

Verlauf / Chronik / History: (1) anzeigen

Sie sind hier:
Doku-StartseitePHP-HandbuchFunktionsreferenzXML-ManipulationDocument Object ModelThe DOMDocument classDOMDocument::relaxNGValidateSource

Ein Service von Reinhard Neidl - Webprogrammierung.

The DOMDocument class

<<DOMDocument::relaxNGValidate

DOMDocument::save>>

DOMDocument::relaxNGValidateSource

(PHP 5)

DOMDocument::relaxNGValidateSource Performs relaxNG validation on the document

Beschreibung

bool DOMDocument::relaxNGValidateSource ( string $source )

Performs » relaxNG validation on the document based on the given RNG source.

Parameter-Liste

source

A string containing the RNG schema.

Rückgabewerte

Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.

Siehe auch


Ein BenutzerBeitrag:
- Beiträge aktualisieren...
gem at rellim dot com
13.10.2004 1:47
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



PHP Powered Diese Seite bei php.net
The PHP manual text and comments are covered by the Creative Commons Attribution 3.0 License © the PHP Documentation Group - Impressum - mail("TO:Reinhard Neidl",...)