...

View Full Version : How can I stitch this code together?



fatmess
03-02-2009, 01:05 AM
Hi
my book2.php file sends the results of a search of my book2.xml file to my book2.html page.
I want to format the results with my book2.xsl file.

My php file looks like this;



<?php

$q=$_GET["q"];$xmlDoc = new DOMDocument();
$xmlDoc->load("book2.xml");
$x=$xmlDoc->getElementsByTagName('title');
for ($i=0; $i<=$x->length-1; $i++)
{
//Process only element nodes
if ($x->item($i)->nodeType==1)
{
if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
{
$y=($x->item($i)->parentNode);
}
}
}
$cd=($y->childNodes);
for ($i=0;$i<$cd->length;$i++)
{

//Process only element nodes
if ($cd->item($i)->nodeType==1)
{
echo ($cd->item($i)->childNodes->item(0)->nodeValue);
}
}
?>


To have my book2.xsl file format the results I guess I need to use something like;



$xsl = new DOMDocument;
$xsl->load('book2.xsl');

$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl);
echo $proc->transformToXML(something here!?!?);

How can incorporate the code?
Thank you for any help.

fatmess
03-02-2009, 02:21 AM
When I try this;



<?php
$q=$_GET["q"];$xmlDoc = new DOMDocument();
$xmlDoc->load("book2.xml");
echo("Root element name is: ");
echo($xmlDoc->documentElement->nodeName);
echo("<hr>");
$x=$xmlDoc->getElementsByTagName('title');
for ($i=0; $i<=$x->length-1; $i++)
{
//Process only element nodes
if ($x->item($i)->nodeType==1)
{
if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
{
$y=($x->item($i)->parentNode);
}
}
}
$cd=($y->childNodes);
for ($i=0;$i<$cd->length;$i++)
{
//Process only element nodes
if ($cd->item($i)->nodeType==1)
{
$myPrint = new DOMDocument;
$myPrint = ($cd->item($i)->childNodes->item(0)->nodeValue);
$xsl = new DOMDocument;
$xsl->load('book2.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($myPrint);
}
}
?>


I get this;



Catchable fatal error: Argument 1 passed to XSLTProcessor::transformToXml() must be an instance of DOMDocument, string given in /home8/egibbera/public_html/book2_home/book2.php on line 32

Fou-Lu
03-02-2009, 03:24 AM
What are you trying to do here:


$myPrint = new DOMDocument;
$myPrint = ($cd->item($i)->childNodes->item(0)->nodeValue);
$xsl = new DOMDocument;
$xsl->load('book2.xsl');
// Configure the transformer
$proc = new XSLTProcessor;
$proc->importStyleSheet($xsl); // attach the xsl rules
echo $proc->transformToXML($myPrint);

I'm not sure why you're opening so many domdocument objects, but you only need the one attached to book2. Anyway, as soon as you apply a $cd->item($i)->childNodes->item(0)->nodeValue to $myPrint, it loses track of the DOMDocument that it originally had, and is assigned a value of a string instead. What you want is to open you're XSLTProcessor, attach the domdocumet stylesheet ($xsl), then attach the original xml to the transformToXML method.

In other words, this is all you should need to do:


<?php

$xml = new DOMDocument('1.0');
$xsl = new XSLTProcessor();
$xml->load('book2.xsl');

$xsl->importStyleSheet($xml);

$xml->load('book2.xml');
print $xsl->transformToXML($xml);
?>

Done and done. I don't know what you're trying to do with you're preprocessing though.

fatmess
03-02-2009, 03:44 AM
Hi Fou_Lu
many thanks for your reply to my question.
I'm sorry that I wasn't clear about what I was trying to achieve.
My original code is this;



<?php
$q=$_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("book2.xml");
echo("Root element name is: ");
echo($xmlDoc->documentElement->nodeName);
$x=$xmlDoc->getElementsByTagName('title');
for ($i=0; $i<=$x->length-1; $i++)
{
//Process only element nodes
if ($x->item($i)->nodeType==1)
{
if ($x->item($i)->childNodes->item(0)->nodeValue == $q)
{
$y=($x->item($i)->parentNode);
}
}
}
$cd=($y->childNodes);
for ($i=0;$i<$cd->length;$i++)
{
//Process only element nodes
if ($cd->item($i)->nodeType==1)
{
echo($cd->item($i)->childNodes->item(0)->nodeValue);
}
}
?>


I guess what I am asking is how do I alter the code so that the book2.php file uses the book2.xsl file to properly format the results of the search.
I'm sure your answer is the right answer but I don't know where to place it in my code to make it work.
In other words, how can I use book2.xls format this line?

echo($cd->item($i)->childNodes->item(0)->nodeValue);

Fou-Lu
03-02-2009, 06:34 PM
Drop the nodevalue off. This should be a domnode and according to the api, the xsltprocessor will take any DOMNode as its value.
You can do this with xpath as well instead of searching through the dom. This means you can technically implement it as a part of xsl, but I don't know if it can be used with the standards that PHP's XSLTProcess uses (best I know, its a little out of date).



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum