格式化输出xml工具----xsl

books.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<books>
	<book category="sport">
		<name lang="en">Play Basketball with me</name>
		<author>Jack Robinson</author>
		<year>2007</year>
		<price>30.00</price>
	</book>
	<book category="it">
		<name lang="cn">Ajax从入门到精通</name>
		<author>Robin Chen</author>
		<year>2007</year>
		<price>60.00</price>
	</book>
	<book category="cooking">
		<name lang="en">Nice Cookie</name>
		<author>Susan Smith</author>
		<year>2007</year>
		<price>15.00</price>
	</book>	
	<book category="sport">
		<name lang="en">Running like Forest Gump</name>
		<author>Jim Jackson</author>
		<year>2007</year>
		<price>35.00</price>
	</book>	
	<book category="cooking">
		<name lang="en">The Best Chicken</name>
		<author>Susan Smith</author>
		<year>2006</year>
		<price>20.00</price>
	</book>	
</books>


books.xsl的代码如下:
<!-- DWXMLSource="books.xml" -->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html" encoding="utf-8" doctype-public="-//W3C//DTD XHTML 1.0 Transitional//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"/>
<xsl:template match="/">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>Book List</title>
</head>

<body style="font-family:Tahoma;">
<h1>Book List</h1>
<xsl:element name="form">
	<xsl:attribute name="name">order</xsl:attribute>
    <xsl:attribute name="id">order</xsl:attribute>
    <xsl:attribute name="action">http://www.robchen.cn</xsl:attribute>
    <xsl:attribute name="method">post</xsl:attribute>
    <xsl:attribute name="target">_blank</xsl:attribute>
    <xsl:apply-templates select="books"></xsl:apply-templates>
    <xsl:element name="label">
    	<xsl:attribute name="for">quantity</xsl:attribute>
    	Quantity
    </xsl:element>
    <xsl:element name="input">
    	<xsl:attribute name="type">text</xsl:attribute>
        <xsl:attribute name="name">quantity</xsl:attribute>
        <xsl:attribute name="id">quantity</xsl:attribute>
        <xsl:attribute name="value">1</xsl:attribute>
        <xsl:attribute name="size">2</xsl:attribute>
        <xsl:attribute name="maxlength">2</xsl:attribute>
        <xsl:attribute name="onchange"><![CDATA[if(!(/\D/.test(this.value)))this.value=1;]]></xsl:attribute>
    </xsl:element>
    <xsl:element name="input">
    	<xsl:attribute name="type">submit</xsl:attribute>
        <xsl:attribute name="value">submit</xsl:attribute>
    </xsl:element>
</xsl:element>
</body>
</html>
</xsl:template>
<xsl:template match="books">
    <ul style="list-style:none;">
        <xsl:apply-templates select="book"></xsl:apply-templates>
    </ul>
</xsl:template>
<xsl:template match="book">
    <li>
        <div>
        	<xsl:element name="input">
            	<xsl:attribute name="type">radio</xsl:attribute>
                <xsl:attribute name="name">book</xsl:attribute>
                <xsl:attribute name="value"><xsl:value-of select="name"/></xsl:attribute>
                <xsl:attribute name="id">book<xsl:value-of select="position()"/></xsl:attribute>
                <xsl:if test="position()=1">
                	<xsl:attribute name="checked">checked</xsl:attribute>
                </xsl:if>
            </xsl:element>
            <xsl:element name="label">
            	<xsl:attribute name="for">book<xsl:value-of select="position()"/></xsl:attribute>
                <strong>&lt;&lt;<xsl:value-of select="name"></xsl:value-of>&gt;&gt;</strong>(<xsl:value-of select="year"/>/<xsl:value-of select="name/@lang"/>)
            </xsl:element>
        </div>
        <ul>
            <li>Category:<xsl:value-of select="@category"/></li>
            <li>Author:<xsl:value-of select="author"></xsl:value-of></li>
            <li>Price:$<xsl:value-of select="price"></xsl:value-of></li>
        </ul>
    </li>
</xsl:template>
</xsl:stylesheet>

你可能感兴趣的:(xml)