aelsheikh
aelsheikh

Reputation: 2328

xsl:include not working

I've been trying for hours now but I just can't get the include to work. All I'm left with is a blank page. Take a gander at the code:

something.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes" />
    <xsl:output 
      doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" 
      doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/>

    <xsl:template match="/">
        <xsl:element name="html">
            <xsl:element name="head">
                <xsl:element name="title">Something</xsl:element>
            </xsl:element>

            <xsl:element name="body"> 
                <xsl:attribute name="onload">
                    <xsl:text>initialize();</xsl:text>
                </xsl:attribute>

                <xsl:element name="div">
                    <xsl:attribute name="id">
                        <xsl:text>main</xsl:text>
                    </xsl:attribute>                                    
                </xsl:element>              

                <xsl:include href="blob.xsl" />

            </xsl:element>

        </xsl:element>

    </xsl:template> 

</xsl:stylesheet>

blob.xsl

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="html" omit-xml-declaration="yes" />
    <xsl:output 
      doctype-public="-//WAPFORUM//DTD XHTML Mobile 1.0//EN" 
      doctype-system="http://www.wapforum.org/DTD/xhtml-mobile10.dtd"/>

    <xsl:template match="*">
            <xsl:element name="div">
                <xsl:attribute name="id">
                    <xsl:text>navoptions</xsl:text>
                </xsl:attribute>
            </xsl:element>
    </xsl:template> 
</xsl:stylesheet>

and the php code

<?php
    $xml = new DOMDocument();   
    $xml->load('file.xml');

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

    $proc = new XSLTProcessor();
    $proc->importStyleSheet($xsl);

    echo $proc->transformToXML($xml);
?>

Update: This is clearly not getting anywhere. As far as my research goes, you cannot import multiple xsl stylesheets when using PHP. On the other hand, does anyone know if this is possible using ColdFusion?

Upvotes: 1

Views: 1764

Answers (3)

Siebe Jongebloed
Siebe Jongebloed

Reputation: 4844

Besides what Michael Kay says, when I use php and include/import xslt you can help it to resolve the uri by setting a xml:base attribute to that element.

I.e. if the xslt's are in /var/www/html/views/ then the xsl:include looks something like:

<xsl:include href="blob.xsl" xml:base="/var/www/html/views/" />

See: https://www.w3.org/TR/xslt-30/#uri-references

Upvotes: 1

Michael Kay
Michael Kay

Reputation: 163322

xsl:include must be a direct child of xsl:stylesheet.

If your XSLT processor isn't giving you an error message for this then you need a better XSLT processor.

Upvotes: 3

Vincent
Vincent

Reputation: 22944

The XSLTProcessor probably doesn't know how to resolve the line. You have to include it manually or hint the XSLTProcessor.

http://php.net/manual/en/xsltprocessor.importstylesheet.php

Code from a comment that should work:

<?php 

# LOAD XML FILE 
$XML = new DOMDocument(); 
$XML->load( 'data.xml' ); 

# START XSLT 
$xslt = new XSLTProcessor(); 

# IMPORT STYLESHEET 1 
$XSL = new DOMDocument(); 
$XSL->load( 'template1.xsl' ); 
$xslt->importStylesheet( $XSL ); 

#IMPORT STYLESHEET 2 
$XSL = new DOMDocument(); 
$XSL->load( 'template2.xsl' ); 
$xslt->importStylesheet( $XSL ); 

#PRINT 
print $xslt->transformToXML( $XML ); 
?> 

Upvotes: 0

Related Questions