On Saturday, 08. February 2003 14:59, Matt Sergeant wrote: > A strange XSLT thing is bugging me... > > In XPathScript because it's not XML I can create unbalanced component > pieces, so I can basically have: > > 1. TOP BIT > 2. CUSTOM Per-Page BIT > 3. content > 4. CUSTOM Per-Page BIT > 5. BOTTOM BIT > > I can't do that in XSLT because everything has to be balanced (xml tag > wise). So I was wondering what people use to create this effect? The > way I was thinking was a named template call in a standard template > implementing 1 and 5, and calling a named template for 2,3,4. The named > template is in the XSLT that includes the "/" template. > > So something like this: > > global.xsl: > <xsl:template match="/"> > <html> > <head>...</head> > <body> > <div something> > <xsl:call-template name="main-content"/> > </div> > </body> > </html> > </xsl:template> > > foo.xsl: > <xsl:include href="global.xsl"/> > <xsl:template name="main-content"> > <!-- custom header here --> > <xsl:apply-templates/> > <!-- custom footer here --> > </xsl:template> > > So the question is - does this work?
Yes. But this can be improved. The first stylesheet is one which provides new default rules - it is so incredibly useful that I have it an own file which I import or include often: passthrough.xsl: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" encoding="ISO-8859-15"/> <xsl:template match="/"> <xsl:apply-templates/> </xsl:template> <xsl:template match="*"> <xsl:copy select="."> <xsl:copy-of select="namespace::*"/> <xsl:apply-templates select="@*"/> <xsl:apply-templates/> </xsl:copy> </xsl:template> <xsl:template match="@*"> <xsl:attribute name="{name()}"><xsl:value-of select="."/></xsl:attribute> </xsl:template> <xsl:template match="text()"> <xsl:value-of select="."/> </xsl:template> </xsl:stylesheet> Now the global stylesheet, modified for the include: gobal.xsl: <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- import here. for this specific scenario, include would have sufficed --> <xsl:import href="passthrough.xsl"/> <xsl:output method="html" encoding="ISO-8859-15"/> <xsl:template match="/"> <!-- global stuff --> <xsl:apply-templates/> <!-- around here --> </xsl:template> </xsl:stylesheet> and here an excerpt from foo.xsl: <!-- import, not include - very important --> <xsl:import href="global.xsl"/> <!-- /* is below /, so it's inside the global stuff --> <xsl:template match="/*"> <!-- local header bits --> <!-- the next one will make the rules from passthrough.xsl apply to the current node, thus copying it outside --> <xsl:apply-imports/> <!-- local footer bits --> </xsl:template> The nice thing about this solution is: It can nest further - if you import foo.xsl, you can easily put stuff around the local header bits using apply-imports. The downside is: it is not easy to put more stuff between the local header bits and the content using import. In that case, if you have multiple nesting "bits that go around the content", the easiest solution is to stack stylesheets, not using import. -- CU Joerg PGP Public Key at http://ich.bin.kein.hoschi.de/~trouble/public_key.asc PGP Key fingerprint = D34F 57C4 99D8 8F16 E16E 7779 CDDC 41A4 4C48 6F94 --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
