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

for-each(-group) - flattening a set of strings hierarc

Subject: for-each(-group) - flattening a set of strings hierarchy
From: Robert Koberg <rob@xxxxxxxxxx>
Date: Thu, 22 Jan 2009 23:10:18 -0500
 for-each(-group) - flattening a set of strings hierarc
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.

Here is what I am outputting so far with the XSL below (don't really care about the whitespace at this point):

var a.b = {};
a.b.c = {};
d.e = {};

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xsl:output method="text"/>


<xsl:template match="/">

    <xsl:variable name="resources" as="xs:string*">
      <xsl:sequence select="q/a//mapping/@resource"/>
    </xsl:variable>

<xsl:variable name="packages" as="xs:string*">
<xsl:for-each-group select="$resources"
group-by="
string-join(
remove(tokenize(., '/'), count(tokenize(., '/')))
, '/')
">
<xsl:sequence select="translate(current-grouping-key(), '/', '.')"/>
</xsl:for-each-group>
</xsl:variable>


    <xsl:for-each-group select="$packages" group-by=".">
      <xsl:if test="position()=1">var </xsl:if>
      <xsl:value-of select="current-grouping-key()"/> = {};<xsl:text>
</xsl:text>
    </xsl:for-each-group>
  </xsl:template>

</xsl:stylesheet>

thanks for any help,
-Rob

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.