xsl-list
[Top] [All Lists]

Re: XSL resources - Flat to hierarchy - Common ancestors

2004-07-26 09:06:16
Hey Ben,

Ben Simkins wrote:
Three questions in one mail:
1. Does anyone know of a public 'knowledgebase' type site where there
are lots of examples of solutions to different problems, classified in a
way which avoids spending hours trawling through threads?

Yep... Dave Pawson maintains such a site and can be found at http://www.dpawson.co.uk/xsl/index.html

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>

The xsl looks like this:
<xsl:template match="/">
        <xsl:apply-templates select="Menus"/>
</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:apply-templates select="../Menu[(_at_)MenuParentId =
current()/@MenuId]"/> </xsl:copy>
</xsl:template>

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?

Wrap the first apply-templates into a variable, use your processors node-set() function (see your processors documentation on how to implment this) to convert the result into a node-set and then apply-templates or for-each through the resulting node-set using <xsl:number/> to accomplish the task

<xsl:template match="/">
        <xsl:variable name="menus"
        <xsl:apply-templates select="Menus"/>
        </xsl:variable>
        <xsl:for-each select="exsl:node-set($menus)">
<!-- change the namespace and syntax for the node-set function above specific to the what the docs tell you for your processor - dont forget to declare the namespace in the xsl:stylesheet element! -->
                <xsl:number/>
                .. whatever else you want to output from each node
                .. you'll obviously need to modify this to fit your
                .. needs but this is the general idea
        </xsl:for-each>
</xsl:template>

3. Given the hierarchical output from 2, I would like to extract all
nodes which are children of any of the ancestors of the node with a
given MenuId (including that node itself)
Expressed otherwise: the children, siblings, ancestors, uncles and
great-uncles, great-great-uncles (etc) of the given node.
Expressed otherwise: all Menus where MenuParentId IN (MenuIds of a given
node and all of its ancestors)

<xsl:template match="/Menus">
        <xsl:apply-templates select="Menu"/>
</xsl:template>

<xsl:template match="Menu">
<xsl:variable name="MenuId" select="@MenuId"/>
<xsl:apply-templates select="ancestor-or-self::Menu[(_at_)MenuId = $MenuId]" mode="MenuId">
                <xsl:with-param name="MenuId" select="$MenuId" />
        </xsl:apply-templates>
</xsl:template>

<xsl:template match="Menu" mode="MenuId">
<xsl:param name="MenuId"/>
... output what you want here ...
<xsl:apply-templates select="*[(_at_)MenuId = $MenuId]" mode="MenuId">
        <xsl:with-param name="MenuId" select="$MenuId"/>
</xsl:apply-templates>
</xsl:template>

Untested, but this is the general idea...

Im in the middle of some other code but as soon as I have a chance I will test this and see if there are any blaring mistakes and respond back if yes...

Best of luck!

<M:D/>
  :: Saxon.NET general public early beta will be available Tuesday, July 27th ::
  :: DISCLAIMER:  All efforts have been made to ensure a quality beta release ::
  :: None-the-less, this is an early beta release that should only be used    ::
  :: in test environments for the purpose of testing performance and helping  ::
  :: to locate potential bugs and performance bottlenecks. With this in mind  ::
  :: PLEASE DO NOT CONSIDER THIS RELEASE READY FOR PRODUCTION IMPLEMENTATION! ::



Many thanks for any answers received

Ben Simkins


--+------------------------------------------------------------------
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(_at_)lists(_dot_)mulberrytech(_dot_)com>
--+--