I'm trying to create a site which (among other things) will display data which is contained in xml files. I'm using xsl stylesheets to format everything, but some of the pages have similar content. Rather than have to make multiple xml sheets with duplicate data, is there a way to tell the xsl where the data is being displayed and have it determine which layout to use.
Example:
<?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="/">
<xsl:choose>
<xsl:if test="something">
<!-- Format data one way -->
</xsl:if>
<xsl:otherwise>
<!-- Format data another way -->
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
The site is being hosted on a larger site which doesn't allow its microsites to use any server side scripting so my options are severely limited here.
In such situation I use layouts, each contained in a separate XML document.
The (filename of the) layout to use can be passed as a parameter to the transformation, or it can be dynamically determined within the transformation.
From this moment on, the Layout XML document can be accessed using the XSLT document() function:
Then you can issue:
This is the "
fill in the blanks
" XSLT design pattern.You could use Client Side XSLT. Provide a PI into your XML documents and in the specific stylesheet include the master layout stylesheet.
Be free to check and use http://www.aranedabienesraices.com.ar as example.
EDIT 3: Almost full example with recursion.
XML document "layoutA.xml":
Input XML document:
Stylesheet:
Output:
Passing param
pLayout
as'layoutB.xml'
, and this "layoutB.xml":Output:
Note: The main problem with your requeriment is the same document restriction (so, same document URI, no diferent PI, no diferent layout URI metadata) wich leaves you only to javascript to pass the layout URI param. Until browser support XPath 2.0
fn:document-uri()
so you can parse URL query. Of course, you could use some extension (MSXSLscript
, as example) but it would be dificult to make it work cross-browser.