[轉]XSLT教程

 

XSLT教程

XML文檔樹

1) XML可以轉化文檔樹

2) XSLTXML的轉化過程

內建模板規則

調用<xsl:apply-templates>處理根節點的兒子。處理時,使用調用時相同的模式

元素

調用<xsl:apply-templates>處理該節點的兒子。處理時,使用調用時相同的模式

屬性

拷貝屬性值到結果樹,結果作爲文本而不是屬性節點

文本

拷貝文本到結果樹

註釋

不做任何事

處理指令

不做任何事

命名空間

不做任何事

XSLT 1.0支持的五種數據類型

numberbooleanstringnode-settree

相互之間的轉化

     

boolean

number

string

node-set

tree

boolean

/

false -> 0

true -> 1

false -> ‘false’

true -> ‘true’

不允許

不允許

number

0 -> false

0-> true

/

轉化爲十進制數

不允許

不允許

string

null-> false

其他 -> true

解析爲十進制

/

不允許

不允許

node-set

empty -> false

其他 -> true

通過string轉化

文檔順序中第一個節點的字符串值

/

不允許

tree

通過string轉化

通過string轉化

連接所有的文本節點

不允許

/

說明:

Tree很少見,只有在xsl:variable中才出現。xslt2.0已經取消該類型。另外,轉化爲node-set,標準xslt1.0不提供,但是擴展接口通常都是提供的。比如微軟提供了msxsl:node-set

XPath

基本概念

絕對路徑的寫法

上下文節點(context node

XPath中直接包含的,和當前節點等同。上下文節點可以通過“.”得到。

position()可以得到上下文節點的當前值。

last()可以得到上下文節點的大小

可以修改上下文節點的xslt標籤

xsl:apply-templates xsl:for-each

xsl:for-each一個常用的特色就是修改context node

Axes

參見P282

Axes

Description

ancestor::

The ancestors of the context node.

The ancestors of the context node consist of the parent of the context node and the parent's parent and so on; thus the ancestor:: axis always includes the root node unless the context node is the root node.

ancestor-or-self::

The context node and its ancestors.

The ancestor-or-self:: axis always includes the root node.

attribute::

The attributes of the context node.

This axis will be empty unless the context node is an element.

child::

The children of the context node.

A child is any node immediately below the context node in the tree. However, neither attribute or namespace nodes are considered children of the context node.

descendant::

The descendants of the context node.

A descendant is a child or a child of a child and so on; thus the descendant:: axis never contains attribute or namespace nodes.

descendant-or-self::

The context node and its descendants.

following::

All nodes that are after the context node in the tree, excluding any descendants, attribute nodes, and namespace nodes.

following-sibling::

All the following siblings of the context node.

The following-sibling:: axis identifies just those children of a parent node who appear in the tree after the context node. This axis excludes all other children that appear before the context node.

If the context node is an attribute node or namespace node, the following-sibling:: axis is empty.

namespace::

The namespace nodes of the context node.

There is a namespace node for every namespace which is in scope for the context node.

This axis will be empty unless the context node is an element.

parent::

The parent of the context node, if there is one.

The parent is the node immediately above the context node in the tree.

preceding::

All nodes that are before the context node in the tree, excluding any ancestors, attribute nodes, and namespace nodes.

One way to think of the preceding axis is all nodes whose content occurs in their entirety before the start of the context node.

preceding-sibling::

All the preceding siblings of the context node.

The preceding-sibling:: axis identifies just those children of a parent node who appear in the tree before the context node. This axis excludes all other children that appear after the context node.

If the context node is an attribute node or namespace node, the preceding-sibling:: axis is empty.

self::

Just the context node itself.

幾種簡寫方式

self::

.

attribute::

@

parent::

..

child::

 

/descendant-or-self::node()

//

Filter

[] 裏面的值是boolean類型,如果不是(數值除外)就會發生強制類型轉化成boolean

數值的特性

其他

NameTest *

Union |

XPath部分函數

Node-Set函數

count()local-name()name()position()last()

String函數

concat()contains()starts-with()string()string-length()substring ()substring-after()substring-before()translate()

    <xsl:variable name="UpCaseHttp" select="translate($ItemValue, 'htp', 'HTP')"/>

Boolean函數

boolean()false()not()true()

Number函數

ceiling()floor()number()round()sum()

XSLT標籤

xsl:template xsl:call-template xsl:apply-templates xsl:param xsl:with-param

 <xsl:template name="JSButton">

    <xsl:param name="Name"/>

    <xsl:param name="JS"/>

    <img src="../images/empty.gif" width="5" height="1"/>

    <span class="clsButtonFace">

      <a onclick="javascript:{$JS};">

        <xsl:value-of select="$Name"/>

      </a>

    </span>

 </xsl:template>

 <xsl:template name="ea:SaveNoNewButton">

    <xsl:call-template name="JSButton">

      <xsl:with-param name="Name" select="' 保存'"/>

      <xsl:with-param name="JS" select="'verify(&quot;Save&quot;)'"/>

    </xsl:call-template>

 </xsl:template>

 <xsl:apply-templates select="Ebanswers/PAGE">

 <xsl:template match="PAGE">

 </xsl:template>

xsl:foreach

          <xsl:for-each select="$CodeTable">

            <option value="{CODE_VALUE}">

              <xsl:if test="CODE_VALUE = $AValue">

                <xsl:attribute name="selected"/>

              </xsl:if>

              <xsl:value-of select="CODE_NAME"/>

            </option>

          </xsl:for-each>

xsl:sort

<xsl:sort

 select = string-Expression

 lang = { nmtoken }

 data-type = { "text" | "number" | QName }

 order = { "ascending" | "descending" }

 case-order = { "upper-first" | "lower-first" }

 />

緊隨xsl:apply-templatesxsl:for-each

    <xsl:apply-templates select="/Toolkit/tk:Table/tk:Field[tk:List]">

      <xsl:sort select="tk:List/@Order" data-type="number"/>

    </xsl:apply-templates>

xsl:variable

注意字符串變量

 <xsl:variable name="Test" select="'title'"/>

xsl:value-of xsl:text

<xsl:value-of

 select = Expression

 disable-output-escaping = "yes" | "no"

</xsl:value-of>

<xsl:text

 disable-output-escaping = "yes" | "no">

</xsl:text>

 <xsl:template name="ea:WhiteSpace">

    <xsl:text disable-output-escaping="yes">&amp;nbsp;</xsl:text>

 </xsl:template>

xsl:element xsl:attribute xsl:attribute-set

XML File (item.xml)

<?xml version="1.0"?>

<?xml-stylesheet type="text/xsl" href="element.xsl" ?>

<root>

   <item>My Item</item>

</root>

XSLT File (element.xsl)

<?xml version="1.0"?>

<xsl:stylesheet version="1.0"

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="item">

   <xsl:element name="xsl:template">

      <xsl:attribute name="match">cost</xsl:attribute>

      <xsl:attribute name="xml:space">preserve</xsl:attribute>

      <xsl:apply-templates/>

   </xsl:element>

</xsl:template>

</xsl:stylesheet>

Output

This is the formatted output:

My Item

The following is the processor output, with line breaks added for clarity.

<?xml version="1.0"?>

<xsl:template match="cost"

      xml:space="preserve"

      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

My Item</xsl:template>

 <xsl:attribute-set name="ea:WrapAttrs">

    <xsl:attribute name="nowrap"/>

 </xsl:attribute-set>

 <!--不能爲空的字段的顯示名稱的td的attribute-set-->

 <xsl:attribute-set name="ea:NonEmptyAttrs" use-attribute-sets="ea:WrapAttrs">

    <xsl:attribute name="align">right</xsl:attribute>

    <xsl:attribute name="bgcolor">#E2EBF6</xsl:attribute>

 </xsl:attribute-set>

xsl:if xsl:choose xsl:when xsl:otherwise

        <a href="{$HttpRef}">

          <xsl:if test="$Target != ''">

            <xsl:attribute name="target"><xsl:value-of select="$Target"/></xsl:attribute>

          </xsl:if>

          <xsl:value-of select="$ItemValue"/>

        </a>

      <xsl:choose>

        <xsl:when test="$BigHrefType='HREF'">

          <a href="{$HrefAddress}">

            <xsl:value-of select="$Value"/>

          </a>

        </xsl:when>

        <xsl:when test="$BigHrefType='EMAIL'">

          <a href="mailto:{$Value}">

            <xsl:value-of select="$Value"/>

          </a>

        </xsl:when>

        <xsl:otherwise>

          <xsl:call-template name="ea:ProcessText">

            <xsl:with-param name="Text" select="$Value"/>

          </xsl:call-template>

        </xsl:otherwise>

      </xsl:choose>

xsl:import xsl:include

 <xsl:import href="PublicUtil.xslt"/>

xsl:copy-of xsl:output

 <xsl:output method="html" indent="no"/>

 <xsl:variable name="ea:NoRecord">

    <tr>

      <td colspan="20" class="pad5">沒有內容</td>

    </tr>

 </xsl:variable>

 <xsl:copy-of select="$ea:NoRecord"/>

XSLT函數

current()

通常情況下,和“.”返回的值相同。只有在filter情況下,纔不同。

<xsl:apply-templates select="//glossary/item[@name=current()/@ref]"/

document()

 <xsl:variable name="ea:FieldList" select="document('')/xsl:stylesheet/ea:FieldList"/>

format-number()

format-number(53.51, "#.0000") // "53.5100"

其他

遞歸:

 <xsl:template name="ea:ProcessText">

    <xsl:param name="Text"/>

    <xsl:choose>

      <xsl:when test="contains($Text,' ')">

        <xsl:value-of select="substring-before($Text,' ')"/>

        <br/>

        <xsl:call-template name="ea:ProcessText">

          <xsl:with-param name="Text" select="substring-after($Text,' ')"/>

        </xsl:call-template>

      </xsl:when>

      <xsl:otherwise>

        <p style="word-break:break-all">

          <xsl:value-of select="$Text"/>

        </p>

      </xsl:otherwise>

    </xsl:choose>

 </xsl:template>

 <xsl:template name="ShowRow">

    <xsl:param name="CurIndex" select="0"/>

    <xsl:param name="Data"/>

    <xsl:param name="CurRow"/>

    <xsl:if test="$CurIndex=0">

      <td bgcolor="#6D8CAB">

        <a href="{concat($WeekLink, 'Date=', $Data/Date)}">

          <img src="{concat('../images/link_', $CurRow + 1, '.gif')}" width="18" height="40" border="0" alt="周曆"/>

        </a>

      </td>

    </xsl:if>

    <xsl:if test="$CurIndex &lt; 7">

      <xsl:call-template name="ShowData">

        <xsl:with-param name="Data" select="$Data"/>

      </xsl:call-template>

      <xsl:for-each select="$Data">

        <xsl:call-template name="ShowRow">

          <xsl:with-param name="CurIndex" select="$CurIndex + 1"/>

          <xsl:with-param name="Data" select="following-sibling::DateDescription[1]"/>

        </xsl:call-template>

      </xsl:for-each>

    </xsl:if>

 </xsl:template>

優先級問題

<xsl:import>存在優先級問題

模糊匹配的,例如*,優先級比較低

屬性值模板

        <a href="{concat($WeekLink, 'Date=', $Data/Date)}">

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章