Solved

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

Posted on 2011-03-24
8
321 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 

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
 
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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

809 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