Jump to content

<xsl:include hfer="something.xsl" /> example


boen_robot
 Share

Recommended Posts

I've recently started experimenting with XSLT and some it's more... complicated elements. The <xsl:include> element is kind of causing trouble. The combined XSLT is well formated according to Dreamwaver 8. I know it's syntax after all, but I don't know how the fragment file must look like. Could someone please give me an example of how the fragment file must look like. Here's what I currently have:test.xml - the source

<?xml version="1.0" encoding="windows-1251"?><?xml-stylesheet type="text/xsl" href="test.xsl"?><site>	<images>  <image> 	 <location>http://www.w3schools.com/images/pic_keybmouse_2.jpg</location> 	 <description>Mouse</description>  </image>  <image> 	 <location>http://www.w3schools.com/images/pic_keyb_2.jpg</location> 	 <description>Keyboard</description>  </image>	</images>	<links>  <menu> 	 <item>    <link>http://www.w3schools.com</link>    <title>w3schools</title>    <logo>http://www.w3schools.com/images/logo_new2.jpg</logo> 	 </item> 	 <item>    <link>http://www.microsoft.com/</link>    <title>Microsoft</title>    <logo>http://i.microsoft.com/h/all/i/ms_masthead_10x7a_ltr.jpg</logo> 	 </item>  </menu>	</links></site>

test.xsl - The main XSLT.

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><xsl:template match="/"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251" /><title>test</title><link rel="stylesheet" type="text/css" href="test.css" /></head><body><xsl:include href="image.xsl" /><xsl:for-each select="/site/links/menu/item"><xsl:variable name="link" select="link" /><xsl:variable name="logo" select="logo" /><xsl:variable name="title" select="title" /><a href="{$link}"><img src="{$logo}" alt="{$title}" /></a><br /></xsl:for-each></body></html></xsl:template></xsl:stylesheet>

image.xsl - The fragment XSLT that is included in the main one.

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><xsl:template match="/"><xsl:for-each select="/site/images/image"><xsl:variable name="location" select="location" /><xsl:variable name="description" select="description" /><img src="{$location}" alt="{$description}" /><br /></xsl:for-each></xsl:template></xsl:stylesheet>

The fragment template works when it's directly in the main XSLT but not when it's on another file. A code which works is this one:

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" /><xsl:template match="/"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1251" /><title>test</title><link rel="stylesheet" type="text/css" href="test.css" /></head><body><xsl:for-each select="/site/images/image"><xsl:variable name="location" select="location" /><xsl:variable name="description" select="description" /><img src="{$location}" alt="{$description}" /><br /></xsl:for-each><xsl:for-each select="/site/links/menu/item"><xsl:variable name="link" select="link" /><xsl:variable name="logo" select="logo" /><xsl:variable name="title" select="title" /><a href="{$link}"><img src="{$logo}" alt="{$title}" /></a><br /></xsl:for-each></body></html></xsl:template></xsl:stylesheet>

Could someone please tell me what I'm doing wrong and how should the fragment file look.

Link to comment
Share on other sites

Thank you for not giving any reply on my question(that's sarcasm).I somehow managed to solve my issue. It's something that for some reason is ot mentioned in the XSLT referance nor Dreamwaver's:An include element can't exist inside a template. It could be before a template, after a template, and between two templates but never inside a template.I also discovered something really odd for the template<->include relationships which I would like someone to explain me in detail please... If you place include element BEFORE a template, the template will be executed but not the include element. If you place the include element AFTER the template you'll get the opposite effect- the template is not executed but the include element is.The "output" element from the main XSLT had to be removed as well. OK. I have an absolutely clean code. But here comes the weirdest part of it all.When I include more that one file one after another, only the latest one is displayed. Why?Here are the new versions of the codes:test.xsl

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:include href="links.xsl" /><xsl:include href="image.xsl" /></xsl:stylesheet>

links.xsl

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"/><xsl:template match="/"><xsl:for-each select="/site/links/menu/item"><xsl:variable name="link" select="link" /><xsl:variable name="logo" select="logo" /><xsl:variable name="title" select="title" /><a href="{$link}"><img src="{$logo}" alt="{$title}" /></a><br /></xsl:for-each></xsl:template></xsl:stylesheet>

image.xsl

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><!DOCTYPE xsl:stylesheet  [	<!ENTITY nbsp   " ">	<!ENTITY copy   "©">	<!ENTITY reg    "®">	<!ENTITY trade  "™">	<!ENTITY mdash  "—">	<!ENTITY ldquo  "“">	<!ENTITY rdquo  "”"> 	<!ENTITY pound  "£">	<!ENTITY yen    "¥">	<!ENTITY euro   "€">]><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"/><xsl:template match="/"><xsl:for-each select="/site/images/image"><xsl:variable name="location" select="location" /><xsl:variable name="description" select="description" /><img src="{$location}" alt="{$description}" /><br /></xsl:for-each></xsl:template></xsl:stylesheet>

Any ideas why is it like that and what should I do to include more than one file in a single document?

Link to comment
Share on other sites

  • 2 weeks later...

Here's a reference that you may find helpful, some of the reading I've already done indicates that you can have multiple "includes":Chapter 17 of the XML Bible

The xsl:import and xsl:include elements enable you to merge multiple style sheets so that you can organize and reuse style sheets for different vocabularies and purposes.
Apologies if this is no help, but looks like you are venturing into new territory.As I complete the tutorials here at W3, maybe I'll be a little more helpful.Good Luck,
Link to comment
Share on other sites

That thing was not helpful, but it was kind'a inspiring for which I was able to find a solution on my own. I saw that topic about templates and I realized I used 2 template with a match of "/". Scince they are both included, they are considered to be one and a same template. Therefore XSLT select and renders only the last one with a template match="/". To avoid this problem I had to adjust my XPath expressions to fit with different matches. The chages were in the fragment codes:image.xsl

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"/><xsl:template match="/site/images">	<xsl:for-each select="image">	<xsl:variable name="location" select="location" />	<xsl:variable name="description" select="description" />	<img src="{$location}" alt="{$description}" /><br /></xsl:for-each></xsl:template></xsl:stylesheet>

links.xsl

<?xml version="1.0" encoding="windows-1251"?><!-- DWXMLSource="test.xml" --><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:output method="html" encoding="windows-1251"/><xsl:template match="/site/links"><xsl:for-each select="menu/item">	<xsl:variable name="link" select="link" />	<xsl:variable name="logo" select="logo" />	<xsl:variable name="title" select="title" />	<a href="{$link}"><img src="{$logo}" alt="{$title}" /></a><br /></xsl:for-each></xsl:template></xsl:stylesheet>

I'm posting this for 2 reasons.1. If I ever forget it.2. If anyone ever has the same issue. :)

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...