Subject: Re: [xsl] Recursive selection, elegantly From: Marcus Andersson <marcus@xxxxxxxxxx> Date: Sun, 06 Jun 2004 13:48:29 +0200 |
<xsl:template match="/root"> <html> <head> <title/> </head> <body> <table> <tr> <xsl:apply-templates select="menu/item"/> </tr> </table> </body> </html> </xsl:template>
<xsl:template match="item"> <xsl:variable name="resource" select="/root/*[@about = current()/@resource]"/> <td><a href="{$resource/@about}"><xsl:value-of select="$resource/name"/></a></td> </xsl:template>
Thanks Mukul for your response (and showing me how to use {} - that's new to me). Unfortunately your solution doesn't cater for instances where:
a) an item in the menu is not a 'page', or b) only a selection of resources constitute the menu
...which is why I want to build the menu from the menu node.
My apologies for providing an insufficient sample, here's alternative xml:
<?xml version="1.0" encoding="UTF-8"?> <root> <menu ref="main_menu"> <item resource="/"/> <item resource="/who_we_are"/> <item resource="/contact_us"/> <item resource="/mulberries"/> </menu> <page about="/"> <name>Home</name> </page> <page about="/who_we_are"> <name>About us</name> </page> <page about="/contact_us"> <name>Contact us</name> </page> <page about="/disclaimer"> <name>Disclaimer</name> </page> <category about="/mulberries"> <name>Top fruit</name> </category> </root>
Note that in this sample I don't want 'Disclaimer' in the menu.
This XSL below works but... I think it's clunky and I don't understand why the simpler version I provided in my prior example doesn't work.
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/"> <xsl:apply-templates select="//menu[@ref='main_menu'" mode="main_menu" /> </xsl:template>
<xsl:template match="menu" mode="main_menu"> <table> <tr> <xsl:apply-templates select="item" /> </tr> </table> </xsl:template>
<xsl:template match="item"> <xsl:param name="resource"><xsl:value-of select="@resource" /></xsl:param> <xsl:apply-templates select="/root/*[@about = $resource]" mode="menu_item" /> </xsl:template>
<xsl:template match="*" mode="menu_item"> <td> <a href="{@about}"> <xsl:value-of select="name"/> </a> </td> </xsl:template> </xsl:stylesheet>
Regard, Stephen
-----Original Message----- From: Mukul Gandhi [mailto:mukul_gandhi@xxxxxxxxx] Sent: 06 June 2004 10:02 AM To: xsl-list@xxxxxxxxxxxxxxxxxxxxxx Subject: Re: [xsl] Recursive selection, elegantly
Hi Stephen, Given the XML -
<?xml version="1.0" encoding="UTF-8"?> <root> <menu ref="main_menu"> <item resource="/"/> <item resource="/who_we_are"/> <item resource="/contact_us"/> <item resource="/disclaimer"/> </menu> <page about="/"> <name>Home</name> </page> <page about="/who_we_are"> <name>About us</name> </page> <page about="/contact_us"> <name>Contact us</name> </page> <page about="/disclaimer"> <name>Disclaimer</name> </page> </root>
The following XSL -
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="html" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/root"> <html> <head> <title/> </head> <body> <table> <tr> <xsl:apply-templates select="page"/> </tr> </table> </body> </html> </xsl:template>
<xsl:template match="page"> <td> <a href="{@about}"> <xsl:value-of select="name"/> </a> </td> </xsl:template>
</xsl:stylesheet>
produces output -
<html> <head> <title></title> </head> <body> <table> <tr> <td><a href="/">Home</a></td> <td><a href="/who_we_are">About us</a></td> <td><a href="/contact_us">Contact us</a></td> <td><a href="/disclaimer">Disclaimer</a></td> </tr> </table> </body> </html>
Regards, Mukul
--- Stephen Tredrea <stephen.tredrea@xxxxxxxxxxxxx> wrote:
--+------------------------------------------------------------------Any suggestion on getting this to work? I can do it in four steps but not in three as attempted here!
Thanks, Stephen
XML (snippet):
<menu ref="main_menu"> <item resource="/" /> <item resource="/who_we_are" /> <item resource="/contact_us" /> <item resource="/disclaimer" /> </menu>
<page about="/"> <name>Home</name> </page>
<page about="/who_we_are"> <name>About us</name> </page>
...etc...
XSL (snippet):
<xsl:template match="/"> <xsl:apply-templates select="//menu[@ref='main_menu'" mode="main_menu" /> </xsl:template>
<xsl:template match="menu" mode="main_menu"> <table> <tr> <xsl:apply-templates select="//*[@about = item/resource]" mode="menu_item" /> </tr> </table> </xsl:template>
<xsl:template match="*" mode="menu_item"> <td> <xsl:element name="a"> <xsl:attribute name="href"><xsl:value-of select="@about" /></xsl:attribute> <xsl:value-of select="name" /> </xsl:element> </td> </xsl:template>
Required HTML:
<table> <tr> <td><a href="/">Home</a></td> <td><a href="/who_we_are">About us</a></td> ...etc... </tr> </table>
XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx> --+--
__________________________________ Do you Yahoo!? Friends. Fun. Try the all-new Yahoo! Messenger. http://messenger.yahoo.com/
--+------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx> --+--
--+------------------------------------------------------------------ XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/ or e-mail: <mailto:xsl-list-unsubscribe@xxxxxxxxxxxxxxxxxxxxxx> --+--
Current Thread |
---|
|
<- Previous | Index | Next -> |
---|---|---|
RE: [xsl] Recursive selection, eleg, Mukul Gandhi | Thread | RE: [xsl] Recursive selection, eleg, Stephen Tredrea |
RE: [xsl] Recursive selection, eleg, Mukul Gandhi | Date | RE: [xsl] xsl-list@lists.mulberryte, Michael Kay |
Month |