xsl-list
[Top] [All Lists]

Re: [xsl] building a hierarchical classification out of flat and redundant data

2006-07-24 04:04:40


This is a grouping problem, in XSLT2 I think you just want

<xsl:stylesheet version="2.0"  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

  <xsl:strip-space elements="*"/>
  <xsl:output indent="yes"/>

  <xsl:template match="x" name="group">
    <xsl:param name="level" select="1"/>
    <xsl:param name="items" select="document"/>
    <xsl:for-each-group select="$items" 
group-by="*[name()=concat('tag',$level)]">
      <node id="{current-grouping-key()}" 
name="{*[name()=concat('tag',$level,'a')]}">
        <xsl:call-template name="group">
          <xsl:with-param name="level" select="$level+1"/>
          <xsl:with-param name="items" select="current-group()"/>
        </xsl:call-template>
      </node>
    </xsl:for-each-group>
  </xsl:template>
</xsl:stylesheet>


In XSLT1 you can do the same thing, but just replace for-each-group by
one of the standard XSLT 1 grouping idioms (see faq or Jeni's site)

<x>
<document>
        <tag1>3</tag1>
        <tag1a>Social Sciences</tag1a>
</document>
<document>
        <tag1>3</tag1>
        <tag1a>Social Sciences</tag1a>
        <tag2>32</tag2>
        <tag2a>Politics</tag2a>
</document>
<document>
        <tag1>3</tag1>
        <tag1a>Social Sciences</tag1a>
        <tag2>32</tag2>
        <tag2a>Politics</tag2a>
        <tag3>326</tag3>
        <tag3a>Slavery</tag3a>
</document>
<document>
        <tag1>3</tag1>
        <tag1a>Social Sciences</tag1a>
        <tag2>33</tag2>
        <tag2a>Economics</tag2a>
</document>
</x>


$ saxon8 grp2.xml grp2.xsl

<?xml version="1.0" encoding="UTF-8"?>
<node id="3" name="Social Sciences">
   <node id="32" name="Politics">
      <node id="326" name="Slavery"/>
   </node>
   <node id="33" name="Economics"/>
</node>

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