Solved

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

Posted on 2011-03-24
8
338 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Cloud Training Guides

FREE GUIDES: In-depth and hand-crafted Linux, AWS, OpenStack, DevOps, Azure, and Cloud training guides created by Linux Academy instructors and the community.

 

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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

The Confluence of Individual Knowledge and the Collective Intelligence At this writing (summer 2013) the term API (http://dictionary.reference.com/browse/API?s=t) has made its way into the popular lexicon of the English language.  A few years ago, …
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

615 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