[prev in list] [next in list] [prev in thread] [next in thread]
List: xsl-list
Subject: Re: [xsl] for-each(-group) - flattening a set of strings hierarchy
From: Andrew Welch <andrew.j.welch () gmail ! com>
Date: 2009-01-23 10:04:08
Message-ID: 74a894af0901230204w2f93a62dw206b71dea0ade00a () mail ! gmail ! com
[Download RAW message or body]
2009/1/23 Robert Koberg <rob@koberg.com>:
> Hi, (didn't really know how to phrase the subject... :) )
>
> Given an XML instance like:
>
> <q>
> <a>
> <mapping resource="a/b/A.xml"/>
> <mapping resource="a/b/B.xml"/>
> <mapping resource="a/b/c/A.xml"/>
> <mapping resource="a/b/c/B.xml"/>
> <mapping resource="d/e/A.xml"/>
> <mapping resource="d/e/B.xml"/>
> </a>
> </q>
>
> I want to get output as (javascript namespaces):
>
> var a = {};
> a.b = {};
> a.b.c = {};
>
> var d = {};
> d.e = {};
>
> I am clumsily getting about half of the way there, but I feel like I am
> approaching it wrong. I am imagining a bunch of for loops.
>
Hi Rob,
I'd be tempted to converted
<mapping resource="a/b/A.xml"/>
<mapping resource="a/b/B.xml"/>
<mapping resource="a/b/c/A.xml"/>
<mapping resource="a/b/c/B.xml"/>
into (something like)
<a>
<b>
<c/>
</b>
</a>
...and then it's straight forward to work out
a
a.b
a.b.c
Easier to debug and maintain etc
To do the grouping part you could use:
<xsl:call-template name="grouper">
<xsl:with-param name="seq" select="//@resource"/>
<xsl:with-param name="pos" select="1"/>
</xsl:call-template>
<xsl:template name="grouper">
<xsl:param name="seq"/>
<xsl:param name="pos"/>
<xsl:for-each-group select="for $x in $seq return tokenize($x,
'/')[$pos][not(contains(., '.xml'))]" group-by=".">
<xsl:element name="{.}">
<xsl:call-template name="grouper">
<xsl:with-param name="seq" select="$seq"/>
<xsl:with-param name="pos" select="$pos + 1"/>
</xsl:call-template>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
cheers
--
Andrew Welch
http://andrewjwelch.com
Kernow: http://kernowforsaxon.sf.net/
--~------------------------------------------------------------------
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@lists.mulberrytech.com>
--~--
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic