DOMXPath->query() ...

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

DOMXPath->query() ...

Post 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?
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post 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
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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...
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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.
RobertPaul
Forum Contributor
Posts: 122
Joined: Sun Sep 18, 2005 8:54 pm
Location: OCNY

Post 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:
Post Reply