The xsl:for-each element processes all the nodes in the same way, one after the other.
Subspecies</th><td>Region</th><td>Number</th><td>As Of</th> |
</table></source>
Every XSL stylesheet must start with xsl:stylesheet element
<source lang="xml">
The attribute version="1.0" specifies version of XSL(T) specification.
File: Data.xml
Hello, world
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet></source>
Insert html tags into template
<source lang="xml">
File: Data.xml
<employee>
<firstName>Joe</firstName>
<surname>Smith</surname>
</employee>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employee">
<xsl:apply-templates select="firstName"/>
<xsl:apply-templates select="surname"/>
</xsl:template>
<xsl:template match="surname">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>JoeSmith</source>
With XSL you can modify any source text and produce different output from the same source file
<source lang="xml">
"//title" matches any title element anywhere in the document.
"//author" matches any author element anywhere in the document.
"/" matches the root element.
File: Data.xml
<title>XSL</title>
<author>John Smith</author>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="//title"/>
<xsl:value-of select="//author"/>
</xsl:template>
<xsl:template match="/">
<xsl:value-of select="//author"/>
<xsl:value-of select="//title"/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>John Smith
XSL
</source>
"xsl:template xsl:value-of"
<source lang="xml">
An XSL processors parses an XML source and tries to find a matching template rule.
If it does, instructions inside matching template are evaluated.
File: Data.xml
<bold>Hello, world.</bold>
<red>I am </red>
<italic>fine.</italic>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="bold">
<paragraph>
<xsl:value-of select="."/>
</paragraph>
</xsl:template>
<xsl:template match="red">
<paragraph style="color:red">
<xsl:value-of select="."/>
</paragraph>
</xsl:template>
<xsl:template match="italic">
<paragraph>
<xsl:value-of select="."/>
</paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph>Hello, world.</paragraph>
<paragraph style="color:red">I am </paragraph>
<paragraph>fine.</paragraph></source>
"xsl:value-of xsl:apply-templates"
<source lang="xml">
File: Data.xml
<employee>
<firstName>Joe</firstName>
<surname>Smith</surname>
</employee>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="employee">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="surname">
<xsl:value-of select="."/>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
Joe
Smith
</source>