Search code examples
phpxmlxsltbyte-order-mark

styling xml-file by including xsl-stylesheet using php


thanks to this helpful community I've been enabled to make a xsl-stylesheet extracting some metainformation from xml-files on my site. Of course, I do not want to code the stylesheet directly in the xml-files, which shall be left untouched. Also, I do not want to preprocess the files in OxyGen and upload the metainfo-files.

So I simply tried this, in metainfo.php:

<?php echo '<?xml-stylesheet type="text/xsl" href="metainfo.xsl"?>'; include ('sample.xml') ?>

Still, loading metainfo.php will display the whole xml file. The source code looks fine, but when I copy it, save it as xml and open it in OxyGen, there is this little bugger '' in the code, which apperntly is called a BOM:

<?xml-stylesheet type="text/xsl" href="metainfo.xsl"?> <?xml-stylesheet type="text/xsl" href="metainfo.xsl"?>

Might this cause the trouble in the browser too? Or is it something else, more basic?


Solution

  • After some extra work, there's what I figured out as a solution myself:

    <?php
    $signatur = $_GET['signatur'];
    # LOAD XML FILE
    $XML = new DOMDocument();
    $XML->load( 'xml/'.$signatur.'.xml' );
    
    # START XSLT
    $xslt = new XSLTProcessor();
    
    # IMPORT STYLESHEET 1
    $XSL = new DOMDocument();
    $XSL->load( 'metainfo.xsl' );
    $xslt->importStylesheet( $XSL );
    
    #PRINT
    print $xslt->transformToXML( $XML );
    ?>