XML Tutorial/XSLT stylesheet/html output — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 08:26, 26 мая 2010
Содержание
Format html output with CSS
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>
<h3>Alphabetical List of European States</h3>
<paragraph>
<b>Total Number of States:</b>
<xsl:text> </xsl:text>
<xsl:value-of select="count(state)" />
</paragraph>
<ul>
<xsl:apply-templates select="state">
<xsl:sort />
</xsl:apply-templates>
</ul>
</body>
</html>
</xsl:template>
<xsl:template match="state">
<li>
<xsl:apply-templates />
</li>
</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>
<h3>Alphabetical List of European States</h3>
<paragraph><b>Total Number of States:</b> 4
</paragraph>
<ul>
<li>Belgium</li>
<li>Russia</li>
<li>San Marino</li>
<li>Switzerland</li>
</ul>
</body>
</html>
Format xml with html
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>
Just output html tags
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>
Output entity
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>
Output one type of HTML tags per template
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">
<h1>
<xsl:apply-templates />
</h1>
</xsl:template>
<xsl:template match="emphasis">
<i>
<xsl:apply-templates />
</i>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?>2001
Output to a list
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">
<em>
<xsl:value-of select="title" />
</em>
<li>
<em>
<xsl:value-of select="book" />
</em>
</li>
<li>
<em>
<xsl:value-of select="cd" />
</em>
</li>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="UTF-8"?><em/><li><em/></li><li><em/></li>
Use different font style to format element
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>
<H2>Book Inventory</H2>
<xsl:apply-templates select="INVENTORY/BOOK" />
</BODY>
</HTML>
</xsl:template>
<xsl:template match="BOOK">
<SPAN STYLE="font-style:italic">Title: </SPAN>
<xsl:value-of select="TITLE"/><BR/>
<SPAN STYLE="font-style:italic">Author: </SPAN>
<xsl:value-of select="AUTHOR"/><BR/>
<SPAN STYLE="font-style:italic">Binding type: </SPAN>
<xsl:value-of select="BINDING"/><BR/>
<SPAN STYLE="font-style:italic">Number of pages: </SPAN>
<xsl:value-of select="PAGES"/><BR/>
<SPAN STYLE="font-style:italic">Price: </SPAN>
<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>
<H2>Book Inventory</H2>
</BODY>
</HTML>
Use html to format xml document
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">
<h1><xsl:apply-templates/></h1>
</xsl:template>
<xsl:template match="verse">
<paragraph><xsl:apply-templates/></p>
</xsl:template>
<xsl:template match="prop">
<i><xsl:apply-templates/></i>
</xsl:template>
</xsl:stylesheet>
Output:
<html>
<body>
<h1>"title 1" excerpt</h1>
<paragraph>
A
<i>B</i>
C
</paragraph>
<paragraph>line 1</paragraph>
<paragraph>line 2</paragraph>
<paragraph>line 3</paragraph>
<paragraph>line 4</paragraph>
</body>
</html>
Use tag to format xml element
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>
<H2>Book Inventory</H2>
<xsl:for-each select="INVENTORY/BOOK">
<SPAN STYLE="font-style:italic">Title: </SPAN>
<xsl:value-of select="TITLE"/><BR/>
<SPAN STYLE="font-style:italic">Author: </SPAN>
<xsl:value-of select="AUTHOR"/><BR/>
<SPAN STYLE="font-style:italic">Binding type: </SPAN>
<xsl:value-of select="BINDING"/><BR/>
<SPAN STYLE="font-style:italic">Number of pages: </SPAN>
<xsl:value-of select="PAGES"/><BR/>
<SPAN STYLE="font-style:italic">Price: </SPAN>
<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>
<H2>Book Inventory</H2>
</BODY>
</HTML>