I have an HTML file like this:
<root>
<br>
<h3>The first H3 text</h3><br>
<p>para1 content in first H3</p><br>
<p>para2 content in first H3</p><br>
<h3>The second H3 text</h3><br>
<p>para1 content in second H3</p><br>
<p>para2 content in second H3</p><br>
<p>para3 content in second H3</p><br>
<p>para4 content in second H3</p><br>
</root>
I want to write an XSL which gives output like this one:
<sec>
<br />
<secHead>The first H3 text</secHead><br />
<para>para1 content in first H3</para><br />
<para>para2 content in first H3</para><br />
</sec>
<br />
<sec>
<br/>
<secHead>The second H3 text</secHead><br/>
<para>para1 content in second H3</para><br/>
<para>para2 content in second H3</para><br/>
<para>para3 content in second H3</para><br/>
<para>para4 content in second H3</para><br/>
</sec>
How can we get this output writing XSL?
Good question, +1.
This transformation:
when applied on the provided source (the html must be converted to wellformed XML!!!):
produces the wanted, correct result:
Explanation:
The key point in this solution is that we define an
xsl:keythat captures the relation between anh3element and all its immediate following siblings elements (such that they aren’th3themselves and thish3is their firsth3preceding sibling).