Solved

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

Posted on 2011-03-24
8
296 Views
Last Modified: 2012-06-27
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

0
Comment
Question by:dilithiumtoys_dot_com
  • 4
  • 4
8 Comments
 

Author Comment

by:dilithiumtoys_dot_com
ID: 35212620
I get this error

Warning: XSLTProcessor::transformToXml() [xsltprocessor.transformtoxml]: Invalid Document
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35213980
It doesn't like your XML. I notice that your sample data does not start with the usual header

<?xml version="1.0"?>
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35213988
Also, is there a reason why you are using DOM rather than SimpleXML? Are you on PHP4?
0
 

Author Comment

by:dilithiumtoys_dot_com
ID: 35215042
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.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 34

Accepted Solution

by:
Beverley Portlock earned 500 total points
ID: 35215072
SImpleXML is no use at transforming documents, but is only available in PHP5, not PHP4, thus my question about it. It is also very simple to use, thus its name.......

Try adding the <?xml header to the generated code and attempt your re-import.
0
 

Author Comment

by:dilithiumtoys_dot_com
ID: 35221948
That did not work.

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

0
 

Author Closing Comment

by:dilithiumtoys_dot_com
ID: 35221950
Partially correct. I figured out my own answer
0
 
LVL 34

Expert Comment

by:Beverley Portlock
ID: 35230484
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!
0

Featured Post

Easy Project Management (No User Manual Required)

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

746 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now