Solved

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

Posted on 2011-03-24
8
306 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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Site hacked - decoding the PHP? 15 66
php string masking 4 29
unset shopping cart session 15 32
str_replace not working in php script 4 11
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Many times as a report developer I've been asked to display normalized data such as three rows with values Jack, Joe, and Bob as a single comma-separated string such as 'Jack, Joe, Bob', and vice versa.  Here's how to do it. 
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

896 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