格式化输出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><<<xsl:value-of select="name"></xsl:value-of>>></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>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值