xml学习笔记---2

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>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值