Php определить это xml

DOMDocument::schemaValidate

DOMDocument::schemaValidate — Validates a document based on a schema. Only XML Schema 1.0 is supported.

Description

Validates a document based on the given schema file.

Parameters

A bitmask of Libxml schema validation flags. Currently the only supported value is LIBXML_SCHEMA_CREATE. Available since Libxml 2.6.14.

Return Values

Returns true on success or false on failure.

See Also

  • DOMDocument::schemaValidateSource() — Validates a document based on a schema
  • DOMDocument::relaxNGValidate() — Performs relaxNG validation on the document
  • DOMDocument::relaxNGValidateSource() — Performs relaxNG validation on the document
  • DOMDocument::validate() — Validates the document based on its DTD

User Contributed Notes 6 notes

For more detailed feedback from DOMDocument::schemaValidate, disable libxml errors and fetch error information yourself. See http://php.net/manual/en/ref.libxml.php for more info.

function libxml_display_error ( $error )
$return = «
\n» ;
switch ( $error -> level ) case LIBXML_ERR_WARNING :
$return .= «Warning $error -> code : » ;
break;
case LIBXML_ERR_ERROR :
$return .= «Error $error -> code : » ;
break;
case LIBXML_ERR_FATAL :
$return .= «Fatal Error $error -> code : » ;
break;
>
$return .= trim ( $error -> message );
if ( $error -> file ) $return .= » in $error -> file » ;
>
$return .= » on line $error -> line \n» ;

function libxml_display_errors () $errors = libxml_get_errors ();
foreach ( $errors as $error ) print libxml_display_error ( $error );
>
libxml_clear_errors ();
>

// Enable user error handling
libxml_use_internal_errors ( true );

$xml = new DOMDocument ();
$xml -> load ( ‘example.xml’ );

if (! $xml -> schemaValidate ( ‘example.xsd’ )) print ‘DOMDocument::schemaValidate() Generated Errors!‘ ;
libxml_display_errors ();
>

?>

Old error message:
Warning: DOMDocument::schemaValidate() [function.schemaValidate]: Element ‘child_integer’: ‘Error condition.’ is not a valid value of the atomic type ‘xs:integer’. in example.php on line 40

New error message:
DOMDocument::schemaValidate() Generated Errors!
Error 1824: Element ‘child_integer’: ‘Error condition.’ is not a valid value of the atomic type ‘xs:integer’. in example.xml on line 4

Initial situation:
— Debian Lenny
— Apache 2 with PHP 5.2.6
— libxml 2.6.32

Problem: While trying to validate a manually created (!) DOMDocument against an existing XML Schema, I ran into a warning like the one below. Validation fails, even though the document IS valid and the command line tool xmllint confirms this (even with libxml 2.6.32, so this must be a problem with DOM). The validation works fine with libxml 2.7.3.

Warning: DOMDocument::schemaValidate() [domdocument.schemavalidate]: Element ‘YourRootElementName’: No matching global declaration available for the validation root. in /var/www/YourFile.php on line X

Solution: As libxml 2.7.3 is not provided for Debian Lenny yet and this problem seems to be caused by DOM (s.o.), I currently use the following workaround on my machines. DOM obviously has some namespace problems with documents, that have been created manually (i.e. they were not loaded from a file).

So my workaround is saving the DOMDocument temporarily, re-loading it and then validating the temporary DOMDocument. Strangely enough the validation of the same document (= same content) now works. Sure, creating a temporary file is not a nice solution, but unless this bug is fixed, this workaround should do just fine.

// Works with libxml 2.7.3 and higher.
public function isValid ()
return $this -> dom -> schemaValidate ( ‘schema.xsd’ );
>

// Workaround for prior libxml versions, e.g. 2.6.32.
public function isValid ()
// Create temporary file and save manually created DOMDocument.
$tempFile = time () . ‘-‘ . rand () . ‘-document.tmp’ ;
$this -> dom -> save ( $tempFile );

// Create temporary DOMDocument and re-load content from file.
$tempDom = new DOMDocument ();
$tempDom -> load ( $tempFile );

// Delete temporary file.
if ( is_file ( $tempFile ))
unlink ( $tempFile );
>

// Validate temporary DOMDocument.
return $tempDom -> schemaValidate ( ‘schema.xsd’ );
>

A note for people trying to validate complex schemas with PHP. It seems that libxml will not automatically try to import referenced schemas, but simply skip the validation if the schemas were not explicitly imported.

In our example we tried to validate an XML file against a schema with the namespace «xttp://automotive-his.de/200706/rif» that contains a reference to the namespace «xttp://automotive-his.de/200706/rif-xhtml»:

which basically says that an xhtml-content element can contain any element from the rif-xhtml namespace.

However since libxml does not know where to find the schema file, it will stop the validation if the elements from the referenced namespace, passing also document with invalid xhtml content as valid.

The solution was to create a combined schema, which contains import statements for all files matching the referenced schemas:

Note that the schemaLocation tells the validator, where to find the files for the corresponding namespaces. When validating the XML document against this combined schema, libxml validated correctly the content inside the XHTML-Content.

Hope this helps someone out there.

I had a tricky issue using this method, i thought it was bug but i realized i misundestood something about namespaces.
When you want to use a schema to describe an xml document, you basically put the default namespace to a personnal namespace (and you refer to this namespace in the targetNamespace attribute of your schema).

That xmlns attribute specifies a «default namespace» and means that the root element and its children are in this namespace.
What i misunderstood is that, there’s no way with the DOM api to specify a «default namespace» for each children of the root element.
Thus you may need to use createElementNS() and createAttributeNS() methods for each created element or attribute in the document specifying the URI of your namespace each time («http://my.uri.net»).

This only applies if you want to validate a document built with the API, not with a document loaded from an xml file or stream.

In older versions of PHP5 this function might cause error messages when dealing with namespaces. I had problems with PHP 5.2.14 which comes with libXML V2.6.16. After switching to PHP 5.3.5 with libXML V2.7.7 I no longer had problems. Took me only about 30 hrs to figure that one out.

In his previous comment, Mike A wrote about validating documents using an XSD. However, you can validate without one. In my case, I needed to ensure that the content entered was just valid XML or not, and I couldn’t find an XSD to support that. So I wrote this:

public static function validate($xml)
libxml_use_internal_errors(true);

$doc = new DOMDocument(‘1.0’, ‘utf-8’);
$doc->loadXML($xml);

$errors = libxml_get_errors();
if (empty($errors))
return true;
>

$error = $errors[0];
if ($error->level < 3)
return true;
>

$lines = explode(«\r», $xml);
$line = $lines[($error->line)-1];

$message = $error->message.’ at line ‘.$error->line.’:
‘.htmlentities($line);

The catch here is that the function only checks for the first error is LIBXML_ERR_FATAL, which would break XSL/XML compilation.

In my experience, the errors are returned by libxml_get_errors in descending severity, so this may be an OK thing to do.

  • DOMDocument
    • _​_​construct
    • createAttribute
    • createAttributeNS
    • createCDATASection
    • createComment
    • createDocumentFragment
    • createElement
    • createElementNS
    • createEntityReference
    • createProcessingInstruction
    • createTextNode
    • getElementById
    • getElementsByTagName
    • getElementsByTagNameNS
    • importNode
    • load
    • loadHTML
    • loadHTMLFile
    • loadXML
    • normalizeDocument
    • registerNodeClass
    • relaxNGValidate
    • relaxNGValidateSource
    • save
    • saveHTML
    • saveHTMLFile
    • saveXML
    • schemaValidate
    • schemaValidateSource
    • validate
    • xinclude

    Источник

    Как проверить xml с php

    но иногда некоторые клиенты отправляют мне недействительный xml, и я хочу вернуть хороший ответ.

    отредактирован:

    Хорошо, я думаю, что я задал неправильный вопрос, я хочу проверить узлы, и если некоторые узлы пропали, я верну лучший ответ.

    Я использовал для проверки этого с помощью php, и я должен проверять все узлы. но этот способ очень трудно изменить.

       example.ir 60  ns1.example.ir 192.0.2.2   ex61-irnic ex61-irnic ex61-irnic ex61-irnic  TOKEN   

    Solutions Collecting From Web of «Как проверить xml с php»

    К сожалению, XMLReader не подтвердил многие вещи в моем случае.

    Вот небольшой кусочек класса, который я написал некоторое время назад:

    /** * Class XmlValidator * @author Francesco Casula */ class XmlValidator < /** * @param string $xmlFilename Path to the XML file * @param string $version 1.0 * @param string $encoding utf-8 * @return bool */ public function isXMLFileValid($xmlFilename, $version = '1.0', $encoding = 'utf-8') < $xmlContent = file_get_contents($xmlFilename); return $this->isXMLContentValid($xmlContent, $version, $encoding); > /** * @param string $xmlContent A well-formed XML string * @param string $version 1.0 * @param string $encoding utf-8 * @return bool */ public function isXMLContentValid($xmlContent, $version = '1.0', $encoding = 'utf-8') < if (trim($xmlContent) == '') < return false; >libxml_use_internal_errors(true); $doc = new DOMDocument($version, $encoding); $doc->loadXML($xmlContent); $errors = libxml_get_errors(); libxml_clear_errors(); return empty($errors); > > 

    Он отлично работает с потоками и vfsStream, а также для тестирования.

    Документация PHP имеет именно то, что вам нужно!

    XML DOMDocument :: validate

    Я уверен, что вы уже определили правильный DTD, верно?

    Load('book.xml'); if ($dom->validate()) < echo "This document is valid!\n"; >?> 

    Вы можете использовать XMLReader::isValid() .

    setParserProperty(XMLReader::VALIDATE, true); var_dump($xml->isValid()); ?> 

    Если вы хотите проверить только в том случае, если документ хорошо сформирован (вы не заботитесь о действительности DTD и даже не имеете DTD для проверки), используйте Domdocument для проверки вашего файла, а не XMLReader, поскольку XMLReader будет передавать поток вы документируете и не загружаете его сразу, поэтому метод isValid () проверяет только первый узел. Вы можете попробовать такой код:

     $doc = new \DOMDocument(); if(@$doc->load($filePath)) < var_dump("$filePath is a valid XML document"); >else

    Вы можете использовать функцию PHP: –

    $xmlcontents = XMLReader::open('filename.xml'); $xmlcontents->setParserProperty(XMLReader::VALIDATE, true); var_dump($xmlcontents->isValid()); 

    Источник

    XMLReader::isValid

    Returns a boolean indicating if the document being parsed is currently valid.

    Parameters

    This function has no parameters.

    Return Values

    Returns true on success or false on failure.

    Examples

    Example #1 Validating XML

    // The validate parser option must be enabled for
    // this method to work properly
    $xml -> setParserProperty ( XMLReader :: VALIDATE , true );

    Notes

    Note: This checks the current node, not the entire document.

    See Also

    • XMLReader::setParserProperty() — Set parser options
    • XMLReader::setRelaxNGSchema() — Set the filename or URI for a RelaxNG Schema
    • XMLReader::setRelaxNGSchemaSource() — Set the data containing a RelaxNG Schema
    • XMLReader::setSchema() — Validate document against XSD

    User Contributed Notes 4 notes

    1. If you validate against relax-ng, no need to call $xml->setParserProperty(XMLReader::VALIDATE, true);

    2. Be aware that $xml->isValid() will return validity for currently active node (ie. node currently positioned using $xml->read()). It won’t check validity of your entire tree at once, but rather on a step by step basis

    Be aware that $xml->isValid() will return validity only for currently active node, so you have to loop trough the nodes.

    Here is a example how to validate a entire XML file against a XSD schema:

    $xmlReader = new \ XMLReader ();
    $xmlReader -> open ( ‘./example.xml’ );
    $xmlReader -> setParserProperty (\ XMLReader :: VALIDATE , true );
    $xmlReader -> setSchema ( ‘./schema.xsd’ );

    if ( $msgs ) throw new \ Exception ( «XML schema validation errors:\n — » . implode ( «\n — » , array_unique ( $msgs )));
    >
    ?>

    I encountered some problems to use isValid method with xml2assoc function.
    I use this way to validate the entire xml file and put it into an associative array.

    $xml = new XMLReader();
    if (!$xml->xml($xml_string, NULL, LIBXML_DTDVALID)) echo «XML not valid: load error»;
    exit();
    >

    $arErrors = libxml_get_errors();
    $xml_errors = «»;
    foreach ($arErrors AS $xmlError) $xml_errors .= $xmlError->message;
    if ($xml_errors != «») echo «XML not valid: «.$xml_errors;
    exit();
    >

    This comment is only partially correct:
    «isValid() always returns false unless you enable checking for validity by $reader->setParserProperty(XMLReader::VALIDATE, true);»
    This enables DTD checking, but you can also check by using RelaxNG (see setRelaxNGSchema() and setRelaxNGSchemaSource()).

    And also, this is NOT correct:
    «If you just need to check if XML file is well formed, successful loading into XMLReader object is usually enough.»
    It is not enough. Pull parsers operate on stream and if you have a large enough file they will not know it is well formed until it is read to the end. If you need to know if it is well formed or/and valid, read it till the end or validation error (you can use next() for fast reading if you don’t care about contents).

    Источник

    Читайте также:  Получить позицию мыши python
Оцените статью