Subject: Re: [xsl] for-each(-group) - flattening a set of strings hierarchy From: Robert Koberg <rob@xxxxxxxxxx> Date: Thu, 22 Jan 2009 23:22:45 -0500 |
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 |
---|
|
<- Previous | Index | Next -> |
---|---|---|
[xsl] for-each(-group) - flattening, Robert Koberg | Thread | Re: [xsl] for-each(-group) - flatte, Andrew Welch |
[xsl] for-each(-group) - flattening, Robert Koberg | Date | [xsl] Re: Re-arranging an XML file , Mike Stroud |
Month |