Subject: Re: [xsl] Can i use apply-templates to match a xsl:template element? (my solution :)) From: Chris <phatfish@xxxxxxxxx> Date: Sun, 20 Mar 2005 21:17:10 +0000 |
Hi, thanks again for the comments. I spent some time thinking about what i was trying to do, and realised i was going about it the wrong way. I wont try to explain what i was i was thinking ill just post what i came up with in the end. I think it should work ok for what i wanted (which.. was an easy way to template a website with XSLT+PHP5). Ignore this, read it, whatever. You will probably see me again when i realise this doesnt work :p <?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html"/> <!-- The root template, this would be included into individual page stylesheets. this contains a root layout for a page. More than on of these can be used if a major change in page layout is needed. --> <xsl:template match="/"> <html> <head> <title/> </head> <body> <!-- The apply-templates and select bellow matches the part of the xml document that should be applied into each section of the resulting html document. --> <div id="head"><xsl:apply-templates select="page/head"/></div> <div id="menu"><xsl:apply-templates select="page/menu"/></div> <div id="body"><xsl:apply-templates select="page/body"/></div> <div id="foot"><xsl:apply-templates select="page/foot"/></div> </body> </html> </xsl:template> <!-- This is a head template that would include stuff you wanted at the head of your html page this can also be included from a seperate stylesheet --> <xsl:template match="head"> <div>A HEADER</div> </xsl:template> <!-- Same as head really, just another part of the website that is repeated accross many pages --> <xsl:template match="menu"> <div>A MENU</div> </xsl:template> <!-- These two templates bellow (releaseList releaseEdit) form the main content of the page. The other elements of the page, eg. root/head/menu would be included into a stylesheet containing these. Only one of these templates is applied to the xml, the xml document would have either releaseList or releaseEdit elements in it. This means that more than one template can be stored in a stylesheet. If you have ever used templates for website development before, you will know that you can often end up with 100's of small template files for very simple pages. Allowing more than one template in a file should make organisation better. --> <xsl:template match="body/releaseList"> <div>An output from releaseList template</div> <table> <tbody> <xsl:for-each select="releases/row"> <tr> <xsl:for-each select="date"> <td> <xsl:apply-templates/> </td> </xsl:for-each> <xsl:for-each select="name"> <td> <xsl:apply-templates/> </td> </xsl:for-each> </tr> </xsl:for-each> </tbody> </table> </xsl:template> <xsl:template match="body/releaseEdit"> <div>An output from releaseEdit template</div> <table> <tbody> <xsl:for-each select="releases/row"> <tr> <xsl:for-each select="date"> <td> <xsl:apply-templates/> </td> </xsl:for-each> <xsl:for-each select="name"> <td> <a href="edit.html"> <xsl:apply-templates/> </a> </td> </xsl:for-each> </tr> </xsl:for-each> </tbody> </table> </xsl:template> <!-- Another repeating part of a webpage. This would be included like the others --> <xsl:template match="foot"> <div>A FOOTER</div> </xsl:template> </xsl:transform> AND, the xml document the above would transform. <?xml version="1.0"?> <page> <head/> <menu/> <body> <releaseList> <releases> <row iteration="0"> <date>2005-01-01</date> <name>Release 1</name> </row> <row iteration="1"> <date>2005-01-02</date> <name>Release 2</name> </row> <row iteration="2"> <date>2005-01-03</date> <name>Release 3</name> </row> <row iteration="3"> <date>2005-01-04</date> <name>Release 4</name> </row> <row iteration="4"> <date>2005-01-05</date> <name>Release 5</name> </row> <row iteration="5"> <date>2005-01-06</date> <name>Release 6</name> </row> <row iteration="6"> <date>2005-01-07</date> <name>Release 7</name> </row> <row iteration="7"> <date>2005-01-08</date> <name>Release 8</name> </row> <row iteration="8"> <date>2005-01-09</date> <name>Release 9</name> </row> <row iteration="9"> <date>2005-01-10</date> <name>Release 10</name> </row> </releases> </releaseList> </body> <foot/> </page> On Sat, 19 Mar 2005 16:17:51 +0000, Aron Bock <aronbock@xxxxxxxxxxx> wrote: > Chris, > > Going by the subjectline, if you want a template in an xstl stylesheet to > find other elements in the same stylesheet, do something like the following: > > <xsl:template match="/"> > <xsl:for-each select="document('')/*/xsl:template"> > <xsl:value-of select="@match"/> > </xsl:for-each> > </xsl:template> > > Here the document() function, with an empty-string, returns the containing > xslt document...which is an xml document, and which we navigate as usual. > > Needing to seach for an xsl:template within the same stylesheet can be > useful if one od the "templates" actually holds some soft of mapping or > lookup information which is best kept in-situ in the xslt document rather > than in a separate file. > > FYI, document('/') returns the main input xml document in similar fashion > (at least under xalan). > > Regards, > > --A
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] Slightly OT: Formal XSD schem, Eliot Kimber | Thread | [xsl] Run-time polymorphism in XSL , Andrey Basko |
Re: [xsl] implementation of xslt 2., Midsummer Sun | Date | [xsl] Run-time polymorphism in XSL , Andrey Basko |
Month |