I need to arrange the nodes in XHTML in particular predefined order using XSL, retaining all the other transformation within the nodes. The input file is:
<html xmlns="http://www.w3.org/1999/xhtml" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
</head>
<body class="hresume">
<div id="sec 1">
<div>
text 1
<span class="summary">position 1</span>
</div>
</div>
<div id="sec 2">
<div>
text 2
<span class="summary">position 2</span>
</div>
</div>
</body>
</html>
the XML transformation:
<?xml version="1.0"?>
<xsl:stylesheet
version="1.0"
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- the identity template -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<!-- remove summary -->
<xsl:template match="xhtml:span[@class='summary']"/>
<!-- arrange nodes -->
<do-something match="id('sec 2')"/>
<do-something match="id('sec 1')"/>
</xsl:stylesheet>
The expected result is just swapping “sec 1” and “sec 2” nodes and removing “summary” positions. What should be used instead of do-something?
This transformation:
when applied on the provided XML document:
produces exactly the wanted, correct result:
Do note:
The wanted special effect is achieved by having two separate
<xsl:apply-templates>instructions specified in the desired order.The wanted switching of places between the two
divelements is achieved even if the second one were preceding the first one — in the result always the one that was second (of the two) in document order is now first (of the two) in document order.