Re: [xsl] transform html h1 with a div

Subject: Re: [xsl] transform html h1 with a div
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Thu, 25 Oct 2012 11:31:36 +0200
Giuseppe Briotti wrote:
The question is how to create a hierarchical div structure, based on
h1, h2, h*... html tags

As example transform this:

<?xml version="1.0" encoding="UTF-8"?>
<radice>
     <h1>titolo 1A</h1>
     <h2>titolo 2A</h2>
     <p>questo h il testo dopo il titolo 2A</p>
     <h1>titolo 1B</h1>
     <p>questo h il 10 testo dopo il titolo 1B</p>
     <p>questo h il 20 testo dopo il titolo 1B</p>
     <p>questo h il 30 testo dopo il titolo 1B</p>
</radice>

to this

<?xml version="1.0" encoding="UTF-8"?>
<radice>
     <div class="h1">
         <h1>titolo 1A</h1>
         <div class="h2">
             <h2>titolo 2A</h2>
             <p>questo h il testo dopo il titolo 2A</p>
          </div>
      </div>

     <div class="h1">
          <h1>titolo 1B</h1>
          <p>questo h il 10 testo dopo il titolo 1B</p>
          <p>questo h il 20 testo dopo il titolo 1B</p>
          <p>questo h il 30 testo dopo il titolo 1B</p>
      </div>
</radice>

I'm usign XSLT 2.0 and Saxon HE 9.4

So, what I'm missing? Can you suggest a smarter approach?

Use for-each-group group-starting-with, see http://stackoverflow.com/questions/11693413/using-xslfor-each-group for an example (there the input is a bit more complicated and the level is not in h[n] elements but rather in 'name' attributes of 'p' elements but you then simply need to adjust the group-starting-with pattern to say e.g.
group-starting-with="*[local-name() eq concat('h', $level)]"



--


	Martin Honnen --- MVP Data Platform Development
	http://msmvps.com/blogs/martin_honnen/

Current Thread