SitePoint Sponsor

User Tag List

Results 1 to 15 of 15
  1. #1
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    XML parser error: working with XSLT

    i've been reading up on XSLT recently and was using command-line MSXML to transfrom my XML files using XSLT stylesheets... then i remembered this article Kevin wrote which uses XSLT with PHP... i tried it out but stumbled right in my first try...

    could someone help out a newbie? :)

    here's the error message:
    Warning: Sablotron error on line 1: XML parser error 4: not well-formed (invalid token) in c:\apache\htdocs\xslt\xslt.php on line 14
    XSLT processing error: XML parser error 4: not well-formed (invalid token)
    php file:
    PHP Code:
    <?php

    $xml_file
    ='c:/apache/htdocs/xslt/test.xml';
    $xsl_file='c:/apache/htdocs/xslt/test.xsl';

    xml2html($xml_file$xsl_file);

    function 
    xml2html($xml_file$xsl_file)
    {
        
    // create an XSLT processor
        
    $xsltproc xslt_create();

        
    // perform the transformation
        
    $html xslt_process($xsltproc$xml_file$xsl_file);

        
    // check for errors
        
    if(!$html) die('XSLT processing error: ' xslt_error($xsltproc));

        
    // destroy the XSLT processor
        
    xslt_free($xsltproc);

        echo 
    $html;
    }
    ?>
    xml document:
    Code:
    <?xml version="1.0"?>
    <strings>
    	<s>Para 1</s>
    	<s>Para 2</s>
    </strings>
    xslt stylesheet:
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" indent="yes" encoding="UTF-8"/>
    	<xsl:template match="/">
    		<html>
    		<body>
    			<xsl:for-each select="strings/s">
    				<p><xsl:value-of select="."/></p>
    			</xsl:for-each>
    		</body>
    		</html>
    	</xsl:template>
    </xsl:stylesheet>

    thanks for any help!

    - Joel

    PS. i don't know why but i had to use absolute paths for the XSL file (not so for the XML file but i did so anyway)... do i have to do that or am i doing something wrong?

  2. #2
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Doing the PHP like that has never worked for me, and I think quite a few other people, if you follow the way to do it in Example #3 here, it should work:
    http://www.php.net/manual/en/function.xslt-process.php

    eg this part:
    PHP Code:
    // $xml and $xsl contain the XML and XSL data

    $arguments = array(
         
    '/_xml' => $xml,
         
    '/_xsl' => $xsl
    );

    // Allocate a new XSLT processor
    $xh xslt_create();

    // Process the document
    $result xslt_process($xh'arg:/_xml''arg:/_xsl'NULL$arguments); 
    Oh no! the coots are eating my nodes!

  3. #3
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ahh! thanks tim! i'll have to go back and try it out... any idea why it doesn't work like Kevin said it should?

  4. #4
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well I dunno, but it would seem as if it is something funny to do with the way PHP is working when it does that stuff. Seems strange that it doesn't work for a whole lot of people, yet it must have worked for Kevin, for him to publish the article detailing how he does it, and for php.net to list that as a way of using the xslt_process() function...

    Whether it is the set up of PHP on different systems or a random bug that no one has discovered yet, or that we're all just doing something wrong I'm not quite sure.
    Oh no! the coots are eating my nodes!

  5. #5
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I hate to dredge this thread up half a year later, but it came up in search and I felt no need to re-create it

    Redemption, did your problem get fixed? I'm experiencing it also(with code which is for all intents and purposes identical to timnz's.)

    The weird thing is that everything is error-free client side with Marc Lyianage's TestXSL(which also uses Sablotron as the XSL processor.) As soon as it gets to the server, I get the "invalid token" kiss of death.

  6. #6
    SitePoint Zealot
    Join Date
    Oct 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use "file://c:/your/path/to/your/file.ext" to get this working.
    include_once('./sig.inc.php');

  7. #7
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by KillaByte
    You have to use "file://c:/your/path/to/your/file.ext" to get this working.
    Would this be file://username/public_html/path on *nix?

  8. #8
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I _think_ that it didn't fix the problem.

  9. #9
    Sidewalking anode's Avatar
    Join Date
    Mar 2001
    Location
    Philadelphia, US
    Posts
    2,205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved!
    PHP Code:
    $myxsl implode(file('path/to/file.xsl'), ' '); 

  10. #10
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Also, a point to note here. In your example, you had
    .
    .
    .
    <s>...</s>
    <s>...</s>
    .
    .
    .

    This isn't a good XML practice, if you need to put the XML data into a database, or vice-versa, you are going to have repeatitive data

  11. #11
    SitePoint Zealot
    Join Date
    Oct 2002
    Posts
    131
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by redemption
    I _think_ that it didn't fix the problem.
    At least it did for me
    include_once('./sig.inc.php');

  12. #12
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by KillaByte

    At least it did for me
    I meant timnz's suggestion... I don't think it fixed the problem for me.

    I didn't try (nor was I referring to) your suggestion (which probably does work)

  13. #13
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird, it's always worked for me. Oh well...

    Oh no! the coots are eating my nodes!

  14. #14
    SitePoint Wizard silver trophy redemption's Avatar
    Join Date
    Sep 2001
    Location
    Singapore
    Posts
    5,269
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It was a long time ago and I may have (probably should have ) forgotten whether I got it to work or not.

    I was making a sort of "educated guess" since I can only remember that I failed to continue learning XSLT

    Thanks too timnz for that help! I didn't mean to offend.

  15. #15
    Node mutilating coot timnz's Avatar
    Join Date
    Feb 2001
    Location
    New Zealand
    Posts
    516
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I didn't mean to make you feel as if you had to explain that you didn't mean to offend me (not that you did). Hope that made sense.

    Oh no! the coots are eating my nodes!


Bookmarks

Posting Permissions

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