xsl-list
[Top] [All Lists]

Re: [xsl] for-each(-group) - flattening a set of strings hierarchy

2009-01-23 05:13:04
2009/1/23 Robert Koberg <rob(_at_)koberg(_dot_)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(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>