This is the mail archive of the xsl-list@mulberrytech.com mailing list .


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

RE: transforming from flat to hierarchical "grouping"


Hip hei!

> Nowhere are the possible values of the "category" attribute defined.  Each
> source XML is allowed to use any number of category values (and have any
> number of articles) but each article has only one category
> attribute value.

> I'm using XT.

How about

[c:\temp]type test.xml
<?xml version="1.0" encoding="ISO-8859-1"?>
<articles>
<article category="news">foo1</article>
<article category="news">foo2</article>
<article category="sports">foo3</article>
<article category="news">foo4</article>
<article category="sports">foo5</article>
<article category="local">foo6</article>
</articles>

[c:\temp]type test.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform";>

<xsl:output method="xml"
            indent="yes" />

<xsl:template match="/">
  <!-- Select every article which is not preceded by an article with of same
category, i.e. the first of every category -->
  <xsl:apply-templates select="descendant::article[not(@category =
preceding::article/@category)]" />
</xsl:template>

<xsl:template match="article">
  <xsl:element name="{@category}">
    <!-- Process the current article, which is the first of that category
and all following ones, too -->
    <xsl:for-each select=". | following::article[@category =
current()/@category]">
      <article>
        <xsl:apply-templates />
      </article>
    </xsl:for-each>
  </xsl:element>
</xsl:template>

</xsl:stylesheet>

[c:\temp]xt test.xml test.xsl
<?xml version="1.0" encoding="utf-8"?>
<news>
<article>foo1</article>
<article>foo2</article>
<article>foo4</article>
</news>
<sports>
<article>foo3</article>
<article>foo5</article>
</sports>
<local>
<article>foo6</article>
</local>

You should use preceding-sibling and following-sibling axis if you can,
faster that way. There are people on this list who will give you a better
solution.

Jarno - Assemblege 23: Love My Way (Psychadelic Furs cover)


 XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]