xslt 简介

Posted by 灵芝

 相当厉害,这篇blog不能修改,因为文本框里已经不是俺的blog了,而是——xslt生成的页面。

最近做的项目,因为要用到xslt,所以就简单地学习总结一下。
XSL 指扩展样式表语言
(EXtensible Stylesheet Language)

XSLT是扩展样式表转换语言
(Extensible Stylesheet Language Transformations)


因为是与xml配合使用的,所有名称都必须是绝对路径或相对路径,所以路径名称问题也是个比较麻烦的事情。
<xsl:template match="template_name"> 模块元素
<xsl:apply-templates select="template_name"> 应用模块元素
<xsl:value-of select="value"> 取值元素
<xsl:for-each select="iterator"> 循环元素
<xsl:if test="condition"> 条件判断元素
<xsl:choose> 多个条件判断元素
<xsl:when test="condition"> 条件判断元素,与choose配合使用
<xsl:otherwise> 条件判断元素,与choose、when配合使用
<xsl:variable name="var">var_value</xsl:variable> 申明变量元素,使用该变量时<xsl:value-of select='$var'/>即可。

实例:
1、ex.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="ex.xsl"?>
<catalog>
    <cd>
        <title>Empire Burlesque</title>
        <artist>Bob Dylan</artist>
        <country>USA</country>
        <company>Columbia</company>
        <price>10.90</price>
        <year>1985</year>
    </cd>
    <cd>
        <title>Hide your heart</title>
        <artist>Bonnie Tyler</artist>
        <country>UK</country>
        <company>CBS Records</company>
        <price>9.90</price>
        <year>1988</year>
    </cd>
    <List>
      <P name="id">105</P>
      <P name="name">English</P>
      <P name="priceUnit" value="121">
        <Values>
          <Value id="199">元/年</Value>
          <Value id="200">元/月</Value>
        </Values>
      </P>
</List>
</catalog>
2、ex.xsl
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="/">
        <html>
            <body>
                <h2>My CD Collection</h2>
                <table border="1">
                    <tr bgcolor="#9acd32">
                        <th>Title</th>
                        <th>Artist</th>
                    </tr>   
                    <tr><td colspan="2">templates + for-each</td></tr>
                    <xsl:apply-templates select="catalog/cd"></xsl:apply-templates>
                </table>
                <span>templates + List</span><br/>
                <xsl:apply-templates select="catalog/List"></xsl:apply-templates>
            </body>
        </html>
    </xsl:template>
   
    <xsl:template match="catalog/cd">                   
        <xsl:for-each select="/catalog/cd">
            <tr>
                <td>
                    <xsl:value-of select="title"/>
                </td>
                <xsl:choose>
                    <xsl:when test="price &gt; 10">

                        <td bgcolor="#ff00ff">
                            <xsl:value-of select="artist"/>
                        </td>
                    </xsl:when>
                    <xsl:otherwise>
                        <td>
                            <xsl:value-of select="artist"/>
                        </td>
                    </xsl:otherwise>
                </xsl:choose>
            </tr>
        </xsl:for-each>
    </xsl:template>
   
    <xsl:template match="catalog/List">                  
                id:<xsl:value-of select="P[@name='id']"/><br/>
                name:<xsl:value-of select="P[@name='name']"/><br/>
                priceUnit:value=<xsl:value-of select="P[@name='priceUnit'][@value]"/>-
                <xsl:for-each select="P[@name='priceUnit']/Values/Value">
                        <xsl:value-of select='@id'/>=<xsl:value-of select="."/>-
                </xsl:for-each>

    </xsl:template>
</xsl:stylesheet>

官方网站:http://www.w3school.com.cn/xsl/
转自无码团队blog:wuma.koubei.com

标签:xslt

1条回复

  1. 红烧肉 说到:。。。

留言

用户:

标题:

内容: