XSLT学习(一)

来源:互联网 发布:金魔方软件下载 编辑:程序博客网 时间:2024/05/16 07:05

cms系统要用到xslt+xml进行动态输出。现在学习一下xslt。

下面是一个转贴 http://dev.csdn.net/develop/article/59/59298.shtm

   

《Java与XSLT》读书笔记
一,所有的XSLT处理器必须包括四个内置的模版规则,它们的优先级要低于任何其他规则,所以只要编写一个新的模版规则来匹配相同的式样,就可以覆盖它们。理解内置规则的最好方法就是架设它们总是位于后台,如果没有找到其他匹配一个节点的规则,就应用这些内置规则。
<xsl:template match="*|/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="*|/" mode="m">
<xsl:apply-templates mode="m"/>
</xsl:template>
<xsl:template match="text()|@*">
<xsl:value-of select="."/>
</xsl:template>
<xsl:template match="processing-instruction()|comment()">
二,XPath
<message>
<header>
<subject>Hello,World</subject>
<date mm="03" dd="01" yy="2002">
<sender>pres@whitehouse.gov</sender>
<recipient>burke_e@ociWeb.com</recipient>
<recipient>burke_e@yahoo.com</recipient>
<recipient>aidan@burke.com</recipient>
</header>
<body>
       …
</body>
</message>
如果<header>为上下文节点,那么child::sbject将选择<subject>节点,而child::recipient将选择所有的<recipient>节点集,child::*将选择<header>的所有子节点。星号(*)字符是一个通配符,表示主要节点类型的所有节点。每个轴都有一个主要节点类型,它始终为元素,除非该轴为属性或命名空间。如果<date>为上下文节点,那么attribute::yy将选择yy属性,而attribute::*将选择<date>元素的所有属性。
简单增加一个谓词过滤结果节点集,通常会减少结果集的大小。增加额外的谓词可进行额外的过滤。例如child::recipient[position()=1]最初将从前一个实例中选择所有的<recipient>元素,然后向下过滤(减少)这个列表直到第一个:burke_e@ociWeb.com位置从1开始,而不是0。谓词可以包含任何XPath表达式,并且变得相当复杂。
如:
<xsl:apply-templates select="presidents/president[position()=3]/name"/>
<xsl:apply-templates select="presidents/president[count(vicePersident)=0]/name"/>
<xsl:apply-templates select="presidents/president[term/@from &lt; 1800]/name"/>
<xsl:apply-templates select="descendant::president[count(vicePresident)>1]/name"/>
<xsl:apply-templates select="presidents/president/name[child::first=’John’]"/>
<xsl: apply-templates
select="presidents/president[(term/@from > 1800) and (term/@from &lt; 1850)]/name">
三,排序
排序可以应用于数据驱动,也可以应用于模板驱动方法。在任一种情况中<xsl:sort>均作为子元素添加到其他部分。通过增加几个连续的<xsl:sort>元素,就可以完成多字段排序。每种排序可以按照升序和降序进行,用于排序的数据类型可以是"数字"或"文本"。排序默认为升序。
<xsl:sort select="first"/>
<xsl:sort select="last" order="descending"/>
<xsl:sort select="term/@from" order="descending" data-type="number"/>
<xsl:sort select="name/first" data-type="text" case-order="upper-first"/>
 
四,属性值模板(AVT)
只要我们需要将一个属性值视为一个XPath表达式而不是静态文本,可能就需要使用一个AVT。但是对于标准XSLT元素,例如<xsl:template match="pattern"/>,就不必使用AVT语法。对于非XSLT元素,例如任意HTML标签,就需要AVT语法。
五,<xsl:attribute-set>
<xsl:element name="body" use-attribute-sets="body-style"></xsl:element>
六,<xsl:if>
<xsl:if test="boolean-expression">
       <!--@@@-->
</xsl:if>
<xsl:if test="middle">检查是否存在一个节点集而不是布尔值
<xsl:if test="@someAttribute"
<xsl:choose>
<xsl:when test="party=’Democratic’">
<xsl text>blue</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:text>red</xsl:text>
</xsl:otherwise>
<xsl:choose>
 
八,参数和变量
<xsl:variable name="lastPresident" select="president[position() = last()]/name"/>
<xsl:value-of select="$lastPresident"/>
九,命名模板
<xsl:template name="formatSSN">
<xsl:param name="ssn"/>
</xsl:template>
<xsl:call-template name="formatSSN">
<xsl:with-param name="ssn" select="@ssn"/>
</xsl:call-template>
 
十,递增变量
<!--- familyTree.xml ->
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="familyTree.xslt"?>
<person name="Otto">
       <person name="Sandra">
              <person name="Jeremy">
                     <person name="Eliana"/>
              </person>
              <person name="Eric">
                     <person name="Aidan"/>
              </person>
              <person name="Philip">
                     <person name="Alex"/>
                     <person name="Andy"/>
              </person>
       </person>
</person>
 
<!--- familyTree.xslt ->
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
       <xsl:output method="html"/>
      
       <!-- processing begins here -->
       <xsl:template match="/">
              <html>
                     <body>
                            <!-- select the top level person -->
                            <xsl:apply-templates select="person">
                                   <xsl:with-param name="level" select="'0'"/>
                            </xsl:apply-templates>
                     </body>
              </html>
       </xsl:template>
      
       <!-- Output information for a person and recursively select
            all children. -->
       <xsl:template match="person">
              <xsl:param name="level"/>
      
              <!-- indent according to the level -->
              <div style="text-indent:{$level}em">
                     <xsl:value-of select="@name"/>
              </div>
      
              <!-- recursively select children, incrementing the level -->
              <xsl:apply-templates select="person">
                     <xsl:with-param name="level" select="$level + 1"/>
              </xsl:apply-templates>
       </xsl:template>
</xsl:stylesheet>
十一,模板模式
<xsl:template match="name" mode="verbose">
</xsl:template>
 
十二,<xsl:include>与<xsl:import>
<xsl:include>只能作为顶层元素,即它与<xsl:template>是兄弟关系
<xsl:include href="url-reference"/>
当包含两个冲突模板规则时,就会出现问题
<xsl:import>必须出现在<xsl:stylesheet>的任何其他顶层元素之前
 
十三,格式化文本和数值
string format-number(number, string, string?)
美元$#.##0.00 #和0是数字占位符并严格符合java.text.DecimalFromat指定的行为
0%输出一个百分数

 

原创粉丝点击