XML Tutorial/XSLT stylesheet/html output
Содержание
Format html output with CSS
<source lang="xml">
File: Data.xml <?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl"?> <europe>
<state>Belgium</state> <state>Russia</state> <state>San Marino</state> <state>Switzerland</state>
</europe>
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="europe"> <html> <head> <title>European States</title> </head> <style type="text/css"> body {font-family: sans-serif} </style> <body>
Alphabetical List of European States
<paragraph> Total Number of States: <xsl:text> </xsl:text> <xsl:value-of select="count(state)" /> </paragraph>
-
<xsl:apply-templates select="state">
<xsl:sort />
</xsl:apply-templates>
</body> </html> </xsl:template> <xsl:template match="state">
</xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>European States</title> </head><style type="text/css"> body {font-family: sans-serif} </style><body>
Alphabetical List of European States
<paragraph>Total Number of States: 4 </paragraph>
- Belgium
- Russia
- San Marino
- Switzerland
</body>
</html></source>
Format xml with html
<source lang="xml">
File: Data.xml <name>
<last>A</last> <first>B</first>
</name> File: Transform.xslt <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes" encoding="UTF-8" /> <xsl:output doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" /> <xsl:output doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" /> <xsl:template match="name"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> <xsl:value-of select="name()" /> </title> </head> <body> <paragraph> <xsl:apply-templates select="last" /> </paragraph> <paragraph> <xsl:apply-templates select="first" /> </paragraph> </body> </html> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html
PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head> <title>name</title> </head> <body> <paragraph>A</paragraph> <paragraph>B</paragraph> </body>
</html></source>
Just output html tags
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <fragment>2001</fragment> File: Transform.xslt <?xml version="1.0" encoding="ISO-8859-1"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output indent="yes" /> <xsl:template match="/"> <html> <head> <title>Contact Info</title> </head> <body style="font-size:12px; font-family: Verdana, Arial, Helvetica;"> contact me </body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Contact Info</title> </head> <body style="font-size:12px; font-family: Verdana, Arial, Helvetica;"> contact me </body>
</html></source>
Output entity
<source lang="xml">
File: Data.xml
<?xml version="1.0"?> <fragment>2001</fragment> File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output indent="yes" /> <xsl:template match="/"> <html> <head> <title>Contact Info</title> </head> <body style="font-size:12px; font-family: Verdana, Arial, Helvetica;"> © test </body> </html> </xsl:template>
</xsl:stylesheet> Output: <html>
<head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Contact Info</title> </head> <body style="font-size:12px; font-family: Verdana, Arial, Helvetica;"> ?est </body>
</html></source>
Output one type of HTML tags per template
<source lang="xml">
File: Data.xml
File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:template match="chapter"> <html> <xsl:apply-templates /> </html> </xsl:template> <xsl:template match="para"> <paragraph> <xsl:apply-templates /> </paragraph> </xsl:template> <xsl:template match="chapter/title">
<xsl:apply-templates />
</xsl:template> <xsl:template match="emphasis"> <xsl:apply-templates /> </xsl:template>
</xsl:stylesheet> Output: <?xml version="1.0" encoding="UTF-8"?>2001</source>
Output to a list
<source lang="xml">
File: Data.xml <?xml version = "1.0"?> <product>
<books> <book>Java</book> <book>C How to Program</book> </books>
</product> File: Transform.xslt <?xml version = "1.0"?> <xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/product"> <xsl:value-of select="title" />
</xsl:template>
</xsl:stylesheet> Output:
<?xml version="1.0" encoding="UTF-8"?><em/>
Use different font style to format element
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <BOOK>
<TITLE>Java</TITLE> <AUTHOR> <FIRSTNAME>Doris</FIRSTNAME> <LASTNAME>Smith</LASTNAME> </AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE>
</BOOK> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Book Inventory</TITLE> </HEAD> <BODY>
Book Inventory
<xsl:apply-templates select="INVENTORY/BOOK" /> </BODY> </HTML> </xsl:template> <xsl:template match="BOOK"> Title: <xsl:value-of select="TITLE"/>
Author: <xsl:value-of select="AUTHOR"/>
Binding type: <xsl:value-of select="BINDING"/>
Number of pages: <xsl:value-of select="PAGES"/>
Price: <xsl:value-of select="PRICE"/><P/> </xsl:template>
</xsl:stylesheet> Output: <HTML>
<HEAD> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>Book Inventory</TITLE> </HEAD> <BODY>
Book Inventory
</BODY>
</HTML></source>
Use html to format xml document
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet href="Transform.xslt" type="text/xsl" ?> <poem>
<title>"title 1" excerpt</title> <verse> A <prop>B</prop> C </verse> <verse>line 1</verse> <verse>line 2</verse> <verse>line 3</verse> <verse>line 4</verse>
</poem>
File: Transform.xslt <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"> <xsl:output method="html"/> <xsl:template match="poem"> <html><body> <xsl:apply-templates/> </body></html> </xsl:template> <xsl:template match="title">
<xsl:apply-templates/>
</xsl:template> <xsl:template match="verse"> <paragraph><xsl:apply-templates/></p> </xsl:template> <xsl:template match="prop"> <xsl:apply-templates/> </xsl:template>
</xsl:stylesheet> Output: <html>
<body>
"title 1" excerpt
<paragraph> A B C </paragraph> <paragraph>line 1</paragraph> <paragraph>line 2</paragraph> <paragraph>line 3</paragraph> <paragraph>line 4</paragraph> </body>
</html></source>
Use tag to format xml element
<source lang="xml">
File: Data.xml <?xml version="1.0"?> <?xml-stylesheet type="text/xsl" href="Transform.xslt"?> <BOOK>
<TITLE>title 1</TITLE> <AUTHOR> <FIRSTNAME>A</FIRSTNAME> <LASTNAME>B</LASTNAME> </AUTHOR> <BINDING>hardcover</BINDING> <PAGES>724</PAGES> <PRICE>$9.95</PRICE>
</BOOK> File: Transform.xslt <?xml version="1.0"?> <xsl:stylesheet
version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <HTML> <HEAD> <TITLE>Book Inventory</TITLE> </HEAD> <BODY>
Book Inventory
<xsl:for-each select="INVENTORY/BOOK"> Title: <xsl:value-of select="TITLE"/>
Author: <xsl:value-of select="AUTHOR"/>
Binding type: <xsl:value-of select="BINDING"/>
Number of pages: <xsl:value-of select="PAGES"/>
Price: <xsl:value-of select="PRICE"/><P/> </xsl:for-each> </BODY> </HTML> </xsl:template>
</xsl:stylesheet> Output: <HTML>
<HEAD> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <TITLE>Book Inventory</TITLE> </HEAD> <BODY>
Book Inventory
</BODY>
</HTML></source>