Page 1 of 1

DOMXPath->query() ...

Posted: Tue Jan 24, 2006 3:13 pm
by RobertPaul
XML snippet I'm trying to query on:

Code: Select all

<FormattedSection SectionNumber="1">
	<FormattedReportObjects>
		<FormattedReportObject FieldName="{Kit.EngrChgOrder}">
			<ObjectName>Field32</ObjectName>
			<FormattedValue>SNAME</FormattedValue>
			<Value>Some Name</Value>
		</FormattedReportObject>
		<!-- SNIP -->
	</FormattedReportObjects>
</FormattedSection>
XPath Query I'm trying to use: //FormattedReportObject[@FieldName='{Kit.EngrChgOrder}']/FormattedValue

The query works on the XML when I tried it with an online XPath tool. It does not work when I try the following:

Code: Select all

<?php
$file = 'C:/php5/apps/xmlReport/data/test.xml';

$data = new DOMDocument();
$data->load($file);
//load failure check snipped, I know it's loading fine

$xpath = new DOMXPath($data);
$query = "//FormattedReportObject[@FieldName='{Kit.EngrChgOrder}']/FormattedValue";
$result = $xpath->query($query);
foreach($result as $node) {
	print_r($node->nodeValue);
}
If I do some stupidly simple query like //@*/.. it works fine, and I get tons of output. But any query that uses element names returns nothing, even though it works on the aforementioned online XPath tool. There are no errors generated... the formatting of the XML itself kind of sucks, but there's nothing I can do about that.

I am, once again, stumped. Any ideas?

Posted: Tue Jan 24, 2006 5:07 pm
by raghavan20

Code: Select all

$query = "//FormattedReportObject[@FieldName='{Kit.EngrChgOrder}']/FormattedValue";
If you say this path works in a XPATH tool then the problem should be using of curly braces which is normally used in PHP to get the value of an array element... try replacing curly braces with character entities...I found out this but it might be wrong...

Code: Select all

Character 					Entity 
single left-pointing angle quotation mark 		&lsaquo; 
single right-pointing angle quotation mark		 &rsaquo

Posted: Tue Jan 24, 2006 6:42 pm
by RobertPaul
I thought it might be the brackets, but even when I tried something like //FormattedReportObjects it gave 0 results.

FWIW, the XML encoding is UTF-8. Not sure if/how that would affect things for me, though...

Posted: Wed Jan 25, 2006 9:26 am
by RobertPaul
OK so I'm reasonably confident that it's not character encoding issues ... I tried converting it to ISO-8859-1and got the same results.

I also tried using SimpleXML to run my XPath, and am having the same problems.

Posted: Wed Jan 25, 2006 12:44 pm
by RobertPaul
Well...

Turns out that this was the problem:

Code: Select all

<FormattedReport [b]xmlns = 'urn:crystal-reports:schemas'[/b] xmlns:xsi = 'http://www.w3.org/2000/10/XMLSchema-instance'>
No prefix. Changing it to xmlns:cr seems to have fixed the problem thus far.

Now to clean up all the hair I've pulled out over this. :roll: