Link to home
Start Free TrialLog in
Avatar of dilithiumtoys_dot_com
dilithiumtoys_dot_com

asked on

How do I dump the xml I retrieve using PHP FUNCTION DOMXPATH::QUERY?

Hi,

I am trying to extract xml from a file and pass it to the xslt parser but its not working. How do I get the attached code to work?

I need to extract the products node and its children to pass to the stylesheet.

Thanks
<config>
<top40>
<products>
<product>
 ...
</product>
</products>
</config>

Open in new window

<?php
$doc = new DOMDocument();
$xsl = new XSLTProcessor();

$doc->load('xsl/products.xsl');
$xsl->importStyleSheet($doc);
$doc->load('xml/config.xml');
$xpath = new DOMXPath($doc);
$p = $doc->getElementsByTagName('top40')->item(0);
$x = $xpath->query('*',$p);

$data = $xsl->transformToXML($x);
echo $data;
?>

Open in new window

Avatar of dilithiumtoys_dot_com
dilithiumtoys_dot_com

ASKER

I get this error

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: Invalid Document
Avatar of Beverley Portlock
It doesn't like your XML. I notice that your sample data does not start with the usual header

<?xml version="1.0"?>
Also, is there a reason why you are using DOM rather than SimpleXML? Are you on PHP4?
I am generating the XML from SQL Server, I am sure I can return a declaration, if I can find out the method. May I transform documents to html using SimpleXML? I am using PHP 5.
ASKER CERTIFIED SOLUTION
Avatar of Beverley Portlock
Beverley Portlock
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
That did not work.

What worked was a mixture of simplexml extraction and saving as xml being passed to DOM and XSLTPROCESSOR.

Partially correct. I figured out my own answer
I didn't want to suggest SimpleXML as it seems that those using DOM either don't like SimpleXML or have requirements beyond its needs. Still I'm glad that you got it sorted out.

Cheers!