xsl-list
[Top] [All Lists]

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

2009-01-22 23:23:07
OK, I am getting basically what I want, but looking for a more elegant way:

<?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="current-grouping-key()"/>
      </xsl:for-each-group>
    </xsl:variable>
<xsl:for-each-group select="$packages" group-by="substring- before(., '.')">
var <xsl:value-of select="current-grouping-key()"/> = {};<xsl:text>
</xsl:text>
    </xsl:for-each-group>
    <xsl:for-each select="$packages">
      <xsl:value-of select="."/> = {};<xsl:text>
</xsl:text>
    </xsl:for-each>
  </xsl:template>
</xsl:stylesheet>


On Jan 22, 2009, at 11:10 PM, Robert Koberg wrote:

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

--~------------------------------------------------------------------
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>
--~--



--~------------------------------------------------------------------
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>