XML Tutorial/XSLT stylesheet/Introduction
Версия от 18:22, 25 мая 2010; (обсуждение)
Содержание
Batch-Processing Nodes
The xsl:for-each element processes all the nodes in the same way, one after the other.
File: Data.xml
<?xml version="1.0"?>
<employees>
<animal>
<name language="English">T1</name>
<name language="Latin">T2</name>
<projects>
<project>project1</project>
</projects>
</animal>
</employees>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="animal">
<paragraph align="center">
<br />
<font size="+3">
<xsl:apply-templates select="name" />
</font>
</paragraph>
<table width="100%" border="2">
<tr>
<td>Subspecies</td>
<td>Region</td>
<td>Number</td>
<td>As Of</td>
</tr>
<xsl:for-each select="subspecies">
<tr>
<td>
<xsl:apply-templates select="name" />
</td>
<td>
<xsl:value-of select="region" />
</td>
<td>
<xsl:value-of select="population" />
</td>
<td>
<xsl:value-of select="population/@year" />
</td>
</tr>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph align="center"><br/><font size="+3">T1T2</font></paragraph><table width="100%" border="2"><tr><td>Subspecies</th><td>Region</th><td>Number</th><td>As Of</th></tr></table>
Every XSL stylesheet must start with xsl:stylesheet element
The attribute version="1.0" specifies version of XSL(T) specification.
File: Data.xml
<em>Hello, world</em>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
</xsl:stylesheet>
Insert html tags into template
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">
<b>
<xsl:apply-templates select="firstName"/>
</b>
<b>
<xsl:apply-templates select="surname"/>
</b>
</xsl:template>
<xsl:template match="surname">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><b>Joe</b><b><i>Smith</i></b>
With XSL you can modify any source text and produce different output from the same source file
"//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
<data>
<title>XSL</title>
<author>John Smith</author>
</data>
File: Transform.xslt
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<h1>
<xsl:value-of select="//title"/>
</h1>
<h2>
<xsl:value-of select="//author"/>
</h2>
</xsl:template>
<xsl:template match="/">
<h2>
<xsl:value-of select="//author"/>
</h2>
<h1>
<xsl:value-of select="//title"/>
</h1>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><h2>John Smith</h2><h1>XSL</h1>
"xsl:template xsl:value-of"
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
<data>
<bold>Hello, world.</bold>
<red>I am </red>
<italic>fine.</italic>
</data>
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>
<b>
<xsl:value-of select="."/>
</b>
</paragraph>
</xsl:template>
<xsl:template match="red">
<paragraph style="color:red">
<xsl:value-of select="."/>
</paragraph>
</xsl:template>
<xsl:template match="italic">
<paragraph>
<i>
<xsl:value-of select="."/>
</i>
</paragraph>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>
<paragraph><b>Hello, world.</b></paragraph>
<paragraph style="color:red">I am </paragraph>
<paragraph><i>fine.</i></paragraph>
"xsl:value-of xsl:apply-templates"
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">
<b>
<xsl:value-of select="."/>
</b>
</xsl:template>
<xsl:template match="surname">
<i>
<xsl:value-of select="."/>
</i>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><b>
Joe
Smith
</b>