#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2006
    Posts
    2
    Rep Power
    0

    My attempt to include XSL not working


    Hi,

    I'm revisiting XSLT after a long time, I'm trying to include a header XSL file but getting errors. Below are my files:

    music.xml

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <?xml-stylesheet type="text/xsl" href="xsl/music.xsl"?>
    <catalog>
    	<cd>
    		<title>Empire Burlesque</title>
    		<artist>Bob Dylan</artist>
    		<country>USA</country>
    		<company>Columbia</company>
    		<price>10.90</price>
    		<year>1985</year>
    	</cd>
    	<cd>
    		<title>Hide your heart</title>
    		<artist>Bonnie Tyler</artist>
    		<country>UK</country>
    		<company>CBS Records</company>
    		<price>9.90</price>
    		<year>1988</year>
    	</cd>
    </catalog>
    music.xsl

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="/">
      <html>
      <body>
      	
      	<xsl:include href="header.xsl" />
      	
      	<h2>Music</h2>
      	
        <table border="1">
          <tr bgcolor="#9acd32">
            <th>Title</th>
            <th>Artist</th>
          </tr>
          <xsl:for-each select="catalog/cd">
          <tr>
            <td><xsl:value-of select="title"/></td>
            <td><xsl:value-of select="artist"/></td>
          </tr>
          </xsl:for-each>
        </table>
      	
      </body>
      </html>
    </xsl:template>
    </xsl:stylesheet>
    header.xsl

    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/"> 
    	Header
    </xsl:template>
    </xsl:stylesheet>
    The error only really occurs when I try to include the file. In firefox the error is "Error loading stylesheet: Parsing an XSLT stylesheet failed". Any help?

    Thanks
    Bizt
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    13,958
    Rep Power
    9397
    Code:
    <xsl:template match="/">
      <html>
      <body>
      	
      	<xsl:include href="header.xsl" />
    You can't "include" XSLT like that. They are rules for how to interpret a source, not some kind of static content you can insert in.
    (The error is because xsl:include must be an immediate child of xsl:stylesheet.)

    Make header.xsl be the one that handles the root node and add an xsl:apply-templates.
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/"> 
    	Header
    	<xsl:apply-templates />
    </xsl:template>
    </xsl:stylesheet>
    Then change music.xsl so that it matches against /catalog and not just /. And move the xsl:include.
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:include href="header.xsl" />
    
    <xsl:template match="/catalog">
      <html>
      <body>
      	
      	
      	<h2>Music</h2>

IMN logo majestic logo threadwatch logo seochat tools logo