XSLT stands for Extensible Stylesheet Language Transformation and is used to convert XML documents to other formats such as XML, HTML or text, to extract individual parts of an XML document, and to create an XML document from different XML documents
XSLT uses the <xsl:template> element which has a macth attribute with a XPath expression to navigate through the XML document, for example, match=’/’ would select all elements that are direct children of the root element
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?xml version="1.0" encoding="UTF-8"?> <xsl:transform version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <body> <h2>List of citizens</h2> <table> <tr> <th>Surname</th> <th>Name</th> <th>Year</th> <th>Occupation</th> </tr> <xsl:for-each select="citizens/citizen"> <tr> <td><xsl:value-of select="surname"/></td> <td><xsl:value-of select="name"/></td> <td><xsl:value-of select="born"/></td> <td><xsl:value-of select="occupation"/></td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:transform> |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="myxslt.xsl"?> <citizens> <citizen> <name>Belinda</name> <surname>Wells</surname> <born>1969</born> <occupation>Firefighter</occupation> </citizen> <citizen> <name>Tess</name> <surname>Gibson</surname> <born>1945</born> <occupation>Architect</occupation> </citizen> <citizen> <name>Fiona</name> <surname>Allen</surname> <born>1979</born> <occupation>Journalist</occupation> </citizen> <citizen> <name>Jessica</name> <surname>Gray</surname> <born>1986</born> <occupation>Cook</occupation> </citizen> <citizen> <name>Isabella</name> <surname>Watson</surname> <born>1955</born> <occupation>Physicist</occupation> </citizen> <citizen> <name>Richard</name> <surname>Adams</surname> <born>1984</born> <occupation>Salesman</occupation> </citizen> <citizen> <name>Robert</name> <surname>Warren</surname> <born>1972</born> <occupation>Historian</occupation> </citizen> <citizen> <name>Arnold</name> <surname>Barnes</surname> <born>1974</born> <occupation>Baker</occupation> </citizen> <citizen> <name>Edwin</name> <surname>Perkins</surname> <born>1978</born> <occupation>Jeweller</occupation> </citizen> <citizen> <name>Stuart</name> <surname>Hill</surname> <born>1966</born> <occupation>Astronomer</occupation> </citizen> </citizens> |