FloD
FloD

Reputation: 27

XSLT split into multiple HTML files

I'm quite a beginner with XSLT and I would like to split an XML file into multiple HTML files according to a specific attribute that is in the XML file.

Here is a sample of my XML file :

<article>
<div>
<paragraph>text text text</paragraph>
</div>
<div class="zoologie">
<def>this is a definition</def>
<def>this is another definition</def>
<example>this is a first example</example>
<example>this is a second example</example>
</div>
</article>

<article>
<div>
<paragraph>text text text</paragraph>
</div>
<div class="médecine">
<def>this is a definition</def>
<def>this is another definition</def>
<example>this is a first example</example>
<example>this is a second example</example>
</div>
</article>

<article>
<div>
<paragraph>text text text</paragraph>
</div>
<div class="zoologie">
<def>this is a definition</def>
<def>this is another definition</def>
<example>this is a first example</example>
<example>this is a second example</example>
</div>
</article>

What I would like to obtain is one HTML file that group togheter all articles with the same attribute class. For instance, one HTML file named zoologie.html that gather all the articles containing the attribute class="zoologie". And another HTML file named médecine.html that gather all articles with the attribute class="médecine". I have tried something with the split function but it won't work. I'd be grateful if you could help me with this. Thanks, Flo

Upvotes: 1

Views: 204

Answers (1)

Michael Kay
Michael Kay

Reputation: 163262

This is a classic XSLT 2.0 construct:

<xsl:for-each-group select="article" group-by="div/@class">
  <xsl:result-document href="{current-grouping-key()}.xml">
    <articles>
      <xsl:copy-of select="current-group()"/>
    </articles>
  </xsl:result-document>
</xsl:for-each-group>

XSLT 1.0 doesn't have any built-in ability to do grouping or to generate multiple output files, but some 1.0 processors may have vendor extensions that help.

Upvotes: 1

Related Questions