[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: XSL resources - Flat to hierarchy - Common ancesto

Subject: RE: XSL resources - Flat to hierarchy - Common ancestors
From: "Ben Simkins" <bsimkins@xxxxxxxxxx>
Date: Tue, 27 Jul 2004 12:40:51 +0200
xsl haschild
Eureka!
With the answers received, and (I admit) blindly playing around with
templates, I've got something which works for my second question:
I'm posting the entire stylesheet here in case anyone needs it, and also
in case anyone higher up the learning tree has any comments on how it
should be done.

> 2. Transforming 'flat' structures to hierarchies:
> I've adapted a stylesheet found in this list, for creating the
hierarchy
> of this:
> <Menus>
>   <Menu MenuId="58" MenuName="LeftMenu"/>
>   <Menu MenuId="1" MenuParentId="58" MenuName="Home"/>
>   <Menu MenuId="60" MenuParentId="1" MenuName="About us/>
>   ...etc
> </Menus>
>
> Question: is there any way of also adding the numbering (<xsl:number
> level="multiple" format="1.1"/>) directly, or do I have to run the
> output through a second stylesheet?
My answer is:
<?xml version="1.0"?>
<!--XSL to Transform 'flat' Menu structure into hierarchy (expects
format <Menus><Menu MenuId="1" MenuParentId="58"
MenuName="Home"/></Menus>)-->
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt">
<xsl:output method="xml" version="1.0" encoding="UTF-8"
omit-xml-declaration="no" standalone="no" indent="yes" />

<xsl:template match="/">
  <xsl:variable name="menus">
	<xsl:apply-templates select="Menus"/>
  </xsl:variable>
  <xsl:apply-templates select="msxsl:node-set($menus)/*" mode="addTOC"/>
</xsl:template>

<xsl:template match="Menus">
  <xsl:copy>
    <xsl:apply-templates select="Menu[not(@MenuParentId)]">
    </xsl:apply-templates>
  </xsl:copy>
</xsl:template>

<xsl:template match="Menu">
  <xsl:copy>
  	<xsl:copy-of select="@*"/>
    <xsl:if test="count(../Menu[@MenuParentId = current()/@MenuId])">
      <xsl:attribute name="hasChild">true</xsl:attribute>
    </xsl:if>
  	<xsl:apply-templates select="../Menu[@MenuParentId =
current()/@MenuId]"/>
  </xsl:copy>
</xsl:template>

<!--Last template to process results of others and add toc-->
<xsl:template match="Menu" mode="addTOC">
    <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:attribute name="TOC">
        <xsl:number level="multiple" format="1.1"/>
      </xsl:attribute>
      <xsl:apply-templates select="Menu" mode="addTOC"/>
    </xsl:copy>
</xsl:template>

</xsl:stylesheet>

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.