match an element and get text with text() function
File: Data.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="Transform.xslt" ?>
<neighbours>
<planet name="Venus">
<description>
Venus
</description>
<positionFromSun>2</positionFromSun>
<diameter> 12104 km (7505 miles)</diameter>
<moons> 0</moons>
<meanTemp> 482C (900F)</meanTemp>
<oneDay> 243.01 earth days</oneDay>
<oneYear> 224.7 earth days</oneYear>
</planet>
</neighbours>
File: Transform.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="neighbours">
<table border="1" id="planetsTable">
<tr>
<th>Name</th>
<th>Position from sun</th>
<th>Diameter</th>
<th>Moons</th>
<th>Mean temp</th>
</tr>
<xsl:apply-templates>
<xsl:sort select="@name" order="ascending"/>
</xsl:apply-templates>
</table>
</xsl:template>
<xsl:template match="planet">
<tr><td><xsl:value-of select="@name"/></td><xsl:apply-templates/></tr>
</xsl:template>
<xsl:template match="positionFromSun">
<td><xsl:value-of select="text()"/></td>
</xsl:template>
<xsl:template match="diameter">
<td><xsl:value-of select="text()"/></td>
</xsl:template>
<xsl:template match="moons">
<td><xsl:value-of select="text()"/></td>
</xsl:template>
<xsl:template match="meanTemp">
<td><xsl:value-of select="text()"/></td>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><table border="1" id="planetsTable"><tr><th>Name</th><th>Position from sun</th><th>Diameter</th><th>Moons</th><th>Mean temp</th></tr><tr><td>Venus</td><td>2</td><td> 12104 km (7505 miles)</td><td> 0</td><td> 482C (900F)</td></tr></table>
Related examples in the same category