XSLT入门


1.什么是XSLT
XSLT 指 XSL 转换(EXtensible Stylesheet Language Transformations)
XSLT 是 XSL 中最重要的部分。
XSLT 可将一种 XML 文档转换为另外一种 XML 文档。
XSLT 使用 XPath 在 XML 文档中进行导航

2.XSLT简介
XSLT 是 XSL 中最重要的部分。
XSLT 用于将一种 XML 文档转换为另外一种 XML 文档,或者可被浏览器识别的其他类型的文档,
比如 HTML 和 XHTML。通常,XSLT 是通过把每个 XML 元素转换为 (X)HTML 元素来完成这项工作的。
通过 XSLT,您可以向或者从输出文件添加或移除元素和属性。您也可重新排列元素,执行测试并决定隐藏或显示哪个元素,等等。
描述转化过程的一种通常的说法是,XSLT 把 XML 源树转换为 XML 结果树。

3.XSLT转换过程
XSLT 使用 XPath 来定义源文档中可匹配一个或多个预定义模板的部分。一旦匹配被找到,
XSLT 就会把源文档的匹配部分转换为结果文档

4.DEMO

(1)cdcatalog.xml

xml 代码
<?xml version="1.0" encoding="utf-8"?>   <!--引用xslt转换xml-->   <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>   <catalog>       <cd>           <title>One night only</title>           <artist>Bee Gees</artist>           <country>UK</country>           <company>Polydor</company>           <price>10.90</price>           <year>1998</year>       </cd>       <cd>           <title>Sylvias Mother</title>           <artist>Dr.Hook</artist>           <country>UK</country>           <company>CBS</company>           <price>8.10</price>           <year>1973</year>       </cd>       <cd>           <title>Black angel</title>           <artist>Savage Rose</artist>           <country>EU</country>           <company>Mega</company>           <price>10.90</price>           <year>1995</year>       </cd>       <cd>           <title>1999 Grammy Nominees</title>           <artist>Many</artist>           <country>USA</country>           <company>Grammy</company>           <price>10.20</price>           <year>1999</year>       </cd>       <cd>           <title>For the good times</title>           <artist>Kenny Rogers</artist>           <country>UK</country>           <company>Mucik Master</company>           <price>8.70</price>           <year>1995</year>       </cd>       <cd>           <title>Big Willie style</title>           <artist>Will Smith</artist>           <country>USA</country>           <company>Columbia</company>           <price>9.90</price>           <year>1997</year>       </cd>       <cd>           <title>Tupelo Honey</title>           <artist>Van Morrison</artist>           <country>UK</country>           <company>Polydor</company>           <price>8.20</price>           <year>1971</year>       </cd>   </catalog>  

(2)cdcatalog.xsl

xml 代码
   <?xml version="1.0" encoding="utf-8" ?>   <!-- 声明一个 XSL 样式表 -->   <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">   <!--match 属性的值是 XPath 表达式(match="/" 表示匹配整个文档)-->   <xsl:template match="/">   <html>   <body>   <h2>My CD Collection</h2>   <table border="1">   <tr bgcolor="#9acd32">   <th align="left">标题</th>   <th align="left">作者</th>   <th align="left">国家</th>   <th align="left">价格</th>   <th align="left">年份</th>   </tr>   <!-- select="catalog/cd[country='UK'] 选取catalog根节点下cd节点下country元素值为"UK"的CD节点集合-->   <xsl:for-each select="catalog/cd[country='UK']">   <!-- 按artist排序-->   <xsl:sort select="artist"/>   <!--判定条件 price值>8 执行xsl:if体 -->   <xsl:if test="price &gt; 8">   <tr>   <!--多重条件判定,结合when,otherwise使用-->   <xsl:choose>       <xsl:when test="price &gt; 9">       <!-- xsl:value 输出元素值-->       <td bgcolor="#ff00ff"><xsl:value-of select="title"/></td>       </xsl:when>       <xsl:otherwise>   <td><xsl:value-of select="title"/></td>       </xsl:otherwise>   </xsl:choose>   <td><xsl:value-of select="artist"/></td>   <td><xsl:value-of select="country"/></td>   <td><xsl:value-of select="price"/></td>   <td><xsl:value-of select="year"/></td>   </tr>   </xsl:if>   </xsl:for-each>   </table>   </body>   </html>   </xsl:template>   </xsl:stylesheet>  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值