Jump to content

Nesting XSLT transform in XML


Guest Andreas Warberg

Recommended Posts

Guest Andreas Warberg

HelloWe have a website which use XML and XSLT to ease the administration burden.Our XML files look something like this:

<?xml version="1.0" encoding="utf-8" ?><?xml-stylesheet type="text/xsl" href="transforms.xsl"?><page>  <title>Page title</title>  <caption>Page caption</caption>  <content>... and content</content></page>

The XSLT file looks like this:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">	<html>	  <head>		<title>		  <xsl:apply-templates select="/page/title"/>		</title>			  </head>	<body>	  	<h1><xsl:apply-templates select="/page/caption"/></h1>		  <xsl:apply-templates select="/page/content"/>	</body>	</html></xsl:template></xsl:stylesheet>

On the front page we have a list of the latest news updates. We would like to keep the news content in the RSS XML format and display it on the front page using another XSLT transformation.We could chose to extend our current XSLT template but the news items should only be displayed on the front page so we would rather insert the news items using the generic /page/content placeholder.Is it possible to use an XSLT transformation inside another XSLT transformed document?Best regardsAndreas

Link to comment
Share on other sites

Well, unfortunatly, XSLT doesn't provide a method for conditional includes. But when I need such a thing I do another thing which is pretty close. I like to call it "deductive include".The method is pretty simple and is easy to think of, if you just spend some time on it. When you process the RSS, call XSLT that contains RSS specific transformation. From that XSLT, include the common stlesheet like the one above. In the same fashion, when you call your main page, call an XSLT that will perform the main page specific transformations and include the common transformation in that specific stylesheet.However I gather the RSS doesn't contain a caption and content element nor are the title and all rest under a "page" element. So, in order to handle both cases, the easiest way is to add a mode and apply only the templates with that mode on. For example:

<?xml version="1.0" encoding="utf-8"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">	<html>	  <head>		<title>		  <xsl:choose>			<xsl:when test="rss">			  <xsl:apply-templates select="title" mode="rss" />			</xsl:when>			<xsl:otherwise>			  <xsl:apply-templates select="title" mode="main" />			</xsl:otherwise>		  </xsl:choose>		</title>			  </head>	<body>	  	<h1>		  <xsl:choose>			<xsl:when test="rss">			  <xsl:apply-templates select="channel/description"/>			</xsl:when>			<xsl:otherwise>			  <xsl:apply-templates  select="caption"/>			</xsl:otherwise>		  </xsl:choose>	</h1>		  <xsl:choose>			<xsl:when test="rss">			  <xsl:apply-templates select="item" />			</xsl:when>			<xsl:otherwise>			  <xsl:apply-templates select="content" />			</xsl:otherwise>		  </xsl:choose>	</body>	</html></xsl:template></xsl:stylesheet>

And the stylesheet that will have specific transformations, the templates will have to look for example:

<xsl:template match="rss/channel/title" mode="rss">

There are of course other variations, but they all involve deductive including.

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
×
×
  • Create New...