xml中的中文编码问题,要保证文件所存编码和文件指定编码相一致:
例如:<?xml version="1.0" encoding="utf-8"?>,那么对应的就要将文件保存为utf-8,当然也可已将它们都指定为gb2312
实现了在html里面调用xml文件,实现xml数据的显示:代码如下
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>无标题文档</title>
</head>
<body>
<xml src="xml2html.xml" id="xmldso" async="false"></xml>
<table width="100%" border="1" datasrc="#xmldso">
<tr>
<td>name</td>
<td>price</td>
<td>calories</td>
</tr>
<tr>
<td><span datafld="name" ></span></td>
<td><span datafld="price" ></span></td>
<td><span datafld="calories" ></span></td>
</tr>
</table>
</body>
</html>
实现了对xml属性的显示:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table width="100%" border="1" datasrc="#xmldso">
<tr>
<td>name</td>
<td>price</td>
<td>calories</td>
</tr>
<xsl:for-each select="breakfast/food">
<tr>
<xsl:attribute name="title"><xsl:value-of select="name" /> from <xsl:value-of select="@producingarea"/>
</xsl:attribute>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="calories"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
实现数据if,choose-when-otherwise选择,order-by=" "排序的xsl代码如下:
<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<html>
<body>
<table width="100%" border="1" datasrc="#xmldso">
<tr>
<td>name</td>
<td>price</td>
<td>calories</td>
</tr>
<xsl:for-each select="breakfast/food" order-by="price>
<tr>
<xsl:attribute name="title"><xsl:if test="name[.='jiaozi']"><xsl:value-of select="name" /> from <xsl:value-of select="@producingarea"/>
</xsl:if></xsl:attribute>
<xsl:attribute name="style">
color:
<xsl:choose>
<xsl:when test="calories[.$le$200]">red</xsl:when>
<xsl:when test="calories[.$le$1000]">green</xsl:when>
<xsl:otherwise test="calories[.$le$1100]">blue</xsl:otherwise>
</xsl:choose></xsl:attribute>
<td><xsl:value-of select="name"/></td>
<td><xsl:value-of select="price"/></td>
<td><xsl:value-of select="calories"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
需要注意的是test语句的写法:test="element[.$operater$'string']"
element前面加@表示属性,不加表示元素,如果比较的是字符串,则用单引号括起来
order-by="+-atrribute"加表示升序,减表示降序
xml代码:
<?xml version="1.0"?>
<!--File Name:XslDemo01.xml -->
<?xml-stylesheet type="text/xsl" href="make.xsl"?>
<breakfast>
<food producingarea="china">
<name>pizza</name>
<price>$5</price>
<calories>900</calories>
</food>
<food producingarea="japan">
<name>jiaozi</name>
<price>$100</price>
<calories>100</calories>
</food>
</breakfast>