Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New Coder
    Join Date
    Jan 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts

    How can I stitch this code together?

    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;

    Code:
    <?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;

    Code:
    $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.
    Last edited by fatmess; 03-02-2009 at 02:32 AM.

  • #2
    New Coder
    Join Date
    Jan 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When I try this;

    Code:
    <?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;

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

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    What are you trying to do here:
    PHP Code:
    $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 Code:
    <?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.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 

  • #4
    New Coder
    Join Date
    Jan 2009
    Posts
    13
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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 Code:
    <?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?
    Code:
    echo($cd->item($i)->childNodes->item(0)->nodeValue);
    Last edited by fatmess; 03-02-2009 at 03:48 AM.

  • #5
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    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).
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •