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 代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!--引用xslt转换xml-->  
  3. <?xml-stylesheet type="text/xsl" href="cdcatalog.xsl"?>  
  4. <catalog>  
  5.     <cd>  
  6.         <title>One night only</title>  
  7.         <artist>Bee Gees</artist>  
  8.         <country>UK</country>  
  9.         <company>Polydor</company>  
  10.         <price>10.90</price>  
  11.         <year>1998</year>  
  12.     </cd>  
  13.     <cd>  
  14.         <title>Sylvias Mother</title>  
  15.         <artist>Dr.Hook</artist>  
  16.         <country>UK</country>  
  17.         <company>CBS</company>  
  18.         <price>8.10</price>  
  19.         <year>1973</year>  
  20.     </cd>  
  21.     <cd>  
  22.         <title>Black angel</title>  
  23.         <artist>Savage Rose</artist>  
  24.         <country>EU</country>  
  25.         <company>Mega</company>  
  26.         <price>10.90</price>  
  27.         <year>1995</year>  
  28.     </cd>  
  29.     <cd>  
  30.         <title>1999 Grammy Nominees</title>  
  31.         <artist>Many</artist>  
  32.         <country>USA</country>  
  33.         <company>Grammy</company>  
  34.         <price>10.20</price>  
  35.         <year>1999</year>  
  36.     </cd>  
  37.     <cd>  
  38.         <title>For the good times</title>  
  39.         <artist>Kenny Rogers</artist>  
  40.         <country>UK</country>  
  41.         <company>Mucik Master</company>  
  42.         <price>8.70</price>  
  43.         <year>1995</year>  
  44.     </cd>  
  45.     <cd>  
  46.         <title>Big Willie style</title>  
  47.         <artist>Will Smith</artist>  
  48.         <country>USA</country>  
  49.         <company>Columbia</company>  
  50.         <price>9.90</price>  
  51.         <year>1997</year>  
  52.     </cd>  
  53.     <cd>  
  54.         <title>Tupelo Honey</title>  
  55.         <artist>Van Morrison</artist>  
  56.         <country>UK</country>  
  57.         <company>Polydor</company>  
  58.         <price>8.20</price>  
  59.         <year>1971</year>  
  60.     </cd>  
  61. </catalog>  

(2)cdcatalog.xsl

xml 代码
  1.   
  2. <?xml version="1.0" encoding="utf-8" ?>  
  3. <!-- 声明一个 XSL 样式表 -->  
  4. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">  
  5. <!--match 属性的值是 XPath 表达式(match="/" 表示匹配整个文档)-->  
  6. <xsl:template match="/">  
  7. <html>  
  8. <body>  
  9. <h2>My CD Collection</h2>  
  10. <table border="1">  
  11. <tr bgcolor="#9acd32">  
  12. <th align="left">标题</th>  
  13. <th align="left">作者</th>  
  14. <th align="left">国家</th>  
  15. <th align="left">价格</th>  
  16. <th align="left">年份</th>  
  17. </tr>  
  18. <!-- select="catalog/cd[country='UK'] 选取catalog根节点下cd节点下country元素值为"UK"的CD节点集合-->  
  19. <xsl:for-each select="catalog/cd[country='UK']">  
  20. <!-- 按artist排序-->  
  21. <xsl:sort select="artist"/>  
  22. <!--判定条件 price值>8 执行xsl:if体 -->  
  23. <xsl:if test="price &gt; 8">  
  24. <tr>  
  25. <!--多重条件判定,结合when,otherwise使用-->  
  26. <xsl:choose>  
  27.     <xsl:when test="price &gt; 9">  
  28.     <!-- xsl:value 输出元素值-->  
  29.     <td bgcolor="#ff00ff"><xsl:value-of select="title"/></td>  
  30.     </xsl:when>  
  31.     <xsl:otherwise>  
  32. <td><xsl:value-of select="title"/></td>  
  33.     </xsl:otherwise>  
  34. </xsl:choose>  
  35. <td><xsl:value-of select="artist"/></td>  
  36. <td><xsl:value-of select="country"/></td>  
  37. <td><xsl:value-of select="price"/></td>  
  38. <td><xsl:value-of select="year"/></td>  
  39. </tr>  
  40. </xsl:if>  
  41. </xsl:for-each>  
  42. </table>  
  43. </body>  
  44. </html>  
  45. </xsl:template>  
  46. </xsl:stylesheet>  

你可能感兴趣的:(html,xml,浏览器,XHTML,XSL)