XML/XSLT stylesheet/apply templates
Содержание
apply template
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <people>
<person born="1912" died="1954"> <name> <first_name>A</first_name> <last_name>B</last_name> </name> <profession>C</profession> <profession>D</profession> <profession>E</profession> </person> <person born="2008" died="2008"> <name> <first_name>F</first_name> <middle_initial>G</middle_initial> <last_name>H</last_name> </name> <profession>I</profession> <hobby>J</hobby> </person>
</people>
File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="name"> <xsl:value-of select="last_name"/>, <xsl:value-of select="first_name"/> </xsl:template> <xsl:template match="person"> <xsl:apply-templates select="name"/> </xsl:template>
</xsl:stylesheet>
Output: <?xml version="1.0" encoding="UTF-8"?>
B, A H, F </source>
Apply template, select from a list
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <books>
<book category="reference"> <author>author1</author> <title>title 1</title> <price>8.95</price> </book> <book category="fiction"> <author>author 2</author> <title>title 2</title> <price>12.99</price> </book> <book category="fiction"> <author>author 3</author> <title>title 3</title> <price>8.99</price> </book>
</books>
File: Transform.xslt <?xml version="1.0" encoding="iso-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="2.0"> <xsl:template match="books"> <html> <body>
A list of books
</body> </html> </xsl:template> <xsl:template match="book"> <tr> <td> <xsl:number /> </td> <xsl:apply-templates select="author, title, price" /> </tr> </xsl:template>
<xsl:template match="author | title | price"> <td> <xsl:value-of select="." /> </td> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
A list of books
1 | author1 | title 1 | 8.95 |
2 | author 2 | title 2 | 12.99 |
3 | author 3 | title 3 | 8.99 |
</body>
</html>
</source>
apply-templates select="tag name"
<source lang="xml">
File: Data.xml <employees>
<employee hireDate="04/23/1999"> <last>A</last> <first>B</first> <salary>1000</salary> </employee> <employee hireDate="09/01/1998"> <last>C</last> <first>D</first> <salary>9500</salary> </employee> <employee hireDate="08/20/2000"> <last>F</last> <first>G</first> <salary>8900</salary> </employee>
</employees> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="text" /> <xsl:template match="employees"> <xsl:apply-templates> <xsl:sort select="salary" /> </xsl:apply-templates> </xsl:template> <xsl:template match="employee"> Last: <xsl:apply-templates select="last" /> First: <xsl:apply-templates select="first" /> Salary: <xsl:apply-templates select="salary" /> Hire Date: <xsl:apply-templates select="@hireDate" /> <xsl:text></xsl:text> </xsl:template>
</xsl:stylesheet>
Output:
Last: A First: B Salary: 1000 Hire Date: 04/23/1999 Last: F First: G Salary: 8900 Hire Date: 08/20/2000 Last: C First: D Salary: 9500 Hire Date: 09/01/1998 </source>
apply-templates with mode=toc
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <people>
<person born="1912" died="1954"> <name> <first_name>A</first_name> <last_name>B</last_name> </name> <profession>C</profession> <profession>D</profession> <profession>E</profession> </person> <person born="2008" died="2008"> <name> <first_name>F</first_name> <middle_initial>G</middle_initial> <last_name>H</last_name> </name> <profession>I</profession> <hobby>J</hobby> </person>
</people>
File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="people"> <html> <head><title>Famous Scientists</title></head> <body>
- <xsl:apply-templates select="person" mode="toc"/>
<xsl:apply-templates select="person"/> </body> </html> </xsl:template> <xsl:template match="person" mode="toc"> <xsl:apply-templates select="name" mode="toc"/> </xsl:template> <xsl:template match="name" mode="toc">
</xsl:template> <xsl:template match="person">
<xsl:apply-templates/>
</xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Famous Scientists</title> </head> <body>
- B, A
- H, F
A B C D E
F G H I J
</body>
</html>
</source>
copy the result of apply-templates
<source lang="xml">
File: Data.xml <colors>
<color> red: <shade>A</shade> <shade>B</shade> <shade>C</shade> </color> <color>yellow</color> <color> blue: <shade>F</shade> <shade>G</shade> <shade>H</shade> </color>
</colors>
File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="xml" omit-xml-declaration="yes" indent="yes" /> <xsl:template match="@*|node()"> <xsl:copy> <xsl:apply-templates select="@*|node()" /> </xsl:copy> </xsl:template>
</xsl:stylesheet> Output: <colors>
<color> red: <shade>A</shade> <shade>B</shade> <shade>C</shade> </color> <color>yellow</color> <color> blue: <shade>F</shade> <shade>G</shade> <shade>H</shade> </color>
</colors>
</source>