Query errors in DOMXPath

287 views Asked by At

I'm trying to parse a xml file using DOMXPath class in PHP but I get some errors when I do queries. My XML have something like this

XML

<?xml version="1.0" encoding="UTF-8"?>
<news><new><title><!CDATA[bla bla bla]]></title><content><!CDATA[<p>bla bla bla</p>]]></content></new></news>

DOMXPath

$xml = new DOMDocument();
$xml->load( $filename );
$xpath = new DOMXPath( $xml );

$news = $xpath->query('//news');

The $news length is always 0, and news tag exists in xml file.

What am I doing wrong?

Best regards,

Ismael.

2

There are 2 answers

3
prakash tank On BEST ANSWER

Check this :

I have modified some of your code :

<?php
    $doc = new DOMDocument;

    // We don't want to bother with white spaces
    $doc->preserveWhiteSpace = false;

    $doc->Load('book.xml');

    $xpath = new DOMXPath($doc);
    // We starts from the root element
    $query = '//news';

    $news = $xpath->query($query);
    print_r($news); 

?>

I am getting length value 1

book.xml

<?xml version="1.0" encoding="UTF-8"?>
<news><new><title>adsds</title><content>asdsa</content></new></news>
2
Shirraz On

Note that you're querying the root tag ("news"), which is somewhat useless since there's only one root tag per XML file. Actually, you query is the same as query('/news'), which lengths will always be "1".

For your problem, note that your CDATA is malformed. It's <![CDATA[blah blah blah]]>, not <!CDATA[bla bla bla]] (missing bracket before CDATA).

This works

<?xml version="1.0" encoding="UTF-8"?>
<news>
  <new><title><![CDATA[<p>bla bla bla</p>]]></title><content><![CDATA[<p>bla bla bla</p>]]></content></new>
  <new><title><![CDATA[<p>bla bla bla</p>]]></title><content><![CDATA[<p>bla bla bla</p>]]></content></new>
  <new><title><![CDATA[<p>bla bla bla</p>]]></title><content><![CDATA[<p>bla bla bla</p>]]></content></new>
</news>

PHP script

<?php
$xml = new DOMDocument();
$xml->load('./test.xml');
$xpath = new DOMXPath( $xml );

$news = $xpath->query('//new');

var_dump($news->length);

Perfectly returns "3".