Hi list

I've tried to set up PHP 4.1.1 on Windows XP Pro with php_xslt.dll
I've done all the initial work (i think) and phpinfo() shows that XSLT support is enabled.

But when I try the script below found as an example, I get unknown encoding '' error in line 1.
I feel I have tried anything.

Please tell me what wrong with the snippet below or if I'm missing something in the php.ini

$xh = xslt_create();

$html = xslt_process($xh, "test.xml","test.xsl");
if ($html!=""){
print "SUCCESS, test.xml was transformed by test.xsl into result.xml";
print ", result.xml has the following contents\n<br>\n";
echo $html;
}
else {
print "Sorry, transformation failed<br><br>";
}

xslt_free($xh);

Kind regards

Lennart Pedersen

    Can you include the top 5 rows from your XML and XSLT files? It can be that there is nothing wrong about your PHP but with your XML.

    /Andrin

      Hi Andrin

      Here's the top 5 lines you requested.

      XML File

      <?xml version="1.0" encoding="utf-8"?>

      <book>

      <!-- Title of the book -->
      <title>Professional Php Programming (Programmer to Programmer)</title>

      <!-- Authors of the book -->
      <text>This book has been authored by:</text>
      <authors>

      XSL File

      <?xml version="1.0" encoding="utf-8"?>
      <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">

      <xsl:template match="/">
      <body>
      <xsl:apply-templates/>
      </body>
      </xsl:template>

      <!-- This is the title of the page -->
      <xsl:template match="title">

      I will send you the complete files if you need them.
      btw. i've also tried including the files with no content (empty files) with the same result. its like the xslt_process function doesn't read the files correctly.

      I look forward for your answar and thank you for you help.

      Kind regards

        Hey :-)

        I thought first there could be an error in your xml and/or xsl file. But that was not the case :-)

        I don´t know what version of PHP you are using but since 4.1 there are new things about XSLT and Sablotron.
        I checked the manual: http://www.php.net/manual/en/function.xslt-process.php. It says that you should write like this:

        <?php

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

        // Process the document
        if (xslt_process($xh, 'sample.xml', 'sample.xsl', 'result.html')) {
        print "SUCCESS, sample.xml was transformed by sample.xsl into result.html";
        print ", result.xml has the following contents\n<br>\n";
        print "<pre>\n";
        readfile('result.xml');
        print "</pre>\n";
        }
        else {
        print "Sorry, sample.xml could not be transformed by sample.xsl into";
        print " result.xml the reason is that " . xslt_error($xh) . " and the ";
        print "error code is " . xslt_errno($xh);
        }

        xslt_free($xh);

        ?>

        Notice the fourth argument in the function xslt_process()? :-)

        If this not works try to switch encoding to ISO-8859-1 in both your xml and xsl file.

        Maybe, maybe not the answer. Good luck
        /Andrin

          Hi Andrin

          I seems that did not work, but i may have found out why.

          In the example send to you the code was something like
          if (xslt_process($xh, 'sample.xml', 'sample.xsl', 'result.html')) { ...

          But if I use file paths like above PHP proceduces an error.. Can't find file in c:\php\

          So my files are used like this
          if (xslt_process($xh, 'http://something.com/sample.xml', 'http://something.com/sample.xsl'

          This gives the Unknown encoding. It seems I have a problem with my configuration, since it doesn't use the current directory.
          I a script is ran from say c:\apache\httpdocs\script.php using releative paths and the xml,xsl files is in the same folder then the script should use that path, but it don't.

          So it works, but nok like I would like it to.

          Any ideas about PHP.ini config problems

            OK, i finally found a solution/ the error.

            This is for anyone who has the same problem. I don't know if its my php configuration but it seems that when suing the readfile() function with relative paths it looks in the php root folder and if you specify a path it fails to read the content correctly´, hence unknown encoding.

            So I rewrote my script using the fopen() instead. For this to work I put the content into variables and applied them to an argument array as shown below and now it works.

            Kind regards

            Lennart Pedersen

            <?php
            // $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);
            if ($result) {
            print "SUCCESS, sample.xml was transformed by sample.xsl into the \$result";
            print " variable, the \$result variable has the following contents\n<br>\n";
            print "<pre>\n";
            print $result;
            print "</pre>\n";
            }
            else {
            print "Sorry, sample.xml could not be transformed by sample.xsl into";
            print " the \$result variable the reason is that " . xslt_error($xh) .
            print " and the error code is " . xslt_errno($xh);
            }
            xslt_free($xh);
            ?>

              2 months later

              If You want to use relative paths the set
              something like
              xslt_set_base($xh, "file://d:/webroot/xslt/");

              But there is still a problem with other languages than english.
              How do You make XSLT with encoding windows-1257 or ISO-8859-13 ?

              -Arnis

                Write a Reply...