xslt函式詳解

醉面韋陀發表於2010-03-15

--------------------------------------------------------------------------------
 
定義與用法
 
current()函式返回一個僅包含當前節點的節點集。通常當前節點和上下文節點是一樣的。
 
<xsl:value-of select="current()"/>與<xsl:value-of select="."/>是一樣的。
 
然而,有一點不同。看下面的XPath表示式:"catalog/cd"。這個表示式選擇<catalog>作為當前節點,然後選擇<catalog>的子節點<cd>。這意味著在賦值的每一步,"."都有不同的意義。
 
下面這行程式碼:
將會處理所有title屬性的值與當前節點的ref屬性的值相等的所有cd元素。
<xsl:apply-templates select="//cd[@title=current()/@ref]"/>
 
這與下面有區別
下面這段程式碼將會處理所有title屬性的值與ref屬性的值相等的所有cd元素。
<xsl:apply-templates select="//cd[@title=./@ref]"/>
 
--------------------------------------------------------------------------------
 
語法
node-set current()
 
例子 1
<?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>
 <xsl:for-each select="catalog/cd/artist">
    Current node: <xsl:value-of select="current()"/>
    <br />
 </xsl:for-each>
 </body>
 </html>
</xsl:template>
</xsl:stylesheet>
 
 
document() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
document()函式用來訪問外部的XML文件。外部XML文件必須是正確可解析的。
 
<xsl:value-of select="document('celsius.xml')/celsius/result[@value=$value]"/>
 
 
--------------------------------------------------------------------------------
 
語法
node-set document(object,node-set?)
 
引數
引數 說明
object  必須。定義XML文件的URI。
node-set  可選。用來處理相關的URI。

 
 
例子1
Document.xml
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="document.xsl" ?>
<groups>
 <groupRef>hrGroup.xml</groupRef>
 <groupRef>myGroup.xml</groupRef>
</groups>
 
document.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="/">
 <groups>
   <xsl:apply-templates select="/groups/groupRef"/>
 </groups>
 </xsl:template>
 <xsl:template match="groups/groupRef">
 <xsl:copy-of select="document(.)//group"/>
 </xsl:template>
</xsl:stylesheet>
 
hrGroup.xml
<?xml version='1.0'?>
<group name="hr">
 <leader>mo</leader>
 <member>bo</member>
 <member>ko</member>
 <member>lo</member>
</group>
 
myGroup.xml
<?xml version='1.0'?>
<group name="my">
 <leader>john</leader>
 <member>jane</member>
 <member>jon</member>
 <member>jan</member>
</group>
 
例子2
下面這個例子,顯示如何使用兩個引數。
第二個引數必須是一個節點集,作為第一個引數的基準URI,當沒有第二個引數時,第一個引數的基準URI就是XSL的URI,如下,把a.xml放到subdir目錄下,而另外兩個在../,如果document('a.xml', .)的第二個引數不寫,將以../作為a.xml的目錄,就會報錯,您可以實驗一下。
document2.xsl
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
 
   <xsl:template match="/">
   <root>
      <xsl:comment>One Argument </xsl:comment>
      <xsl:for-each select="document('b.xml')//a">
         <xsl:copy-of select="."/>
      </xsl:for-each>
 
      <xsl:comment>Two Argument </xsl:comment>
      <xsl:for-each select="document('a.xml', .)//a">
         <xsl:copy-of select="."/>
      </xsl:for-each>
   </root>
   </xsl:template>
</xsl:stylesheet>
 
Subdir/a.xml
<?xml-stylesheet type="text/xsl" href="../document2.xsl" ?>
<doc>
<a> I </a>
<a> II </a>
<a> III </a>
</doc>
 
b.xml
<doc>
 <a> one </a>
 <a> two </a>
 <a> three </a>
</doc>
 
element-available() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
 
element-available()函式檢查XSLT處理器是否能處理指定的元素。
 
這個函式只能用來檢查在模板裡出現的元素,這些元素是:
xsl:apply-imports
xsl:apply-templates
xsl:attributes
xsl:call-template
xsl:choose 
xsl:comment
xsl:copy
xsl:copy-of
xsl:element
xsl:fallback
xsl:for-each
xsl:if
xsl:message
xsl:number
xsl:processing instruction
xsl:text
xsl:value-of
xsl:variable
 
--------------------------------------------------------------------------------
 
語法
boolean element-available(string)
 
引數
引數 說明
string  必須。指定要檢查元素。

 
 
例子 1
<?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>
<xsl:choose>
<xsl:when test="element-available('xsl:comment')">
<p>xsl:comment is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl:comment is not supported.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="element-available('xsl:delete')">
<p>xsl:delete is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>xsl:delete is not supported.</p>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template></xsl:stylesheet> 
 
 
 
format-number() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
 
format-number()函式用來格式化一個數字字串。
 
--------------------------------------------------------------------------------
 
語法
string format-number(number,format,[decimalformat])
 
引數
引數 說明
number  Required. Specifies the number to be formatted
必須。指定要格式化的數字
format 

必須。指定格式化的模式。
&S226;# (指示一個數字。比如: ####)
&S226;0 (Denotes leading and following zeros. Example: 0000.00)
&S226;. (指定小數點的位置。比如: ###.##)
&S226;, (The group separator for thousands. Example: ###,###.##)
&S226;% (Displays the number as a percentage. Example: ##%)
&S226;; (Pattern separator. The first pattern will be used for positive numbers and the second for negative numbers)
 
decimalformat 可選。用來指定使用的<xsl:decimal-format>元素的名稱。

 
 
例子 1
<?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>
<xsl:value-of select='format-number(500100, "#.00")' />
<br />
<xsl:value-of select='format-number(500100, "#.0")' />
<br />
<xsl:value-of select='format-number(500100, "###,###.00")' />
<br />
<xsl:value-of select='format-number(0.23456, "##%")' />
<br />
<xsl:value-of select='format-number(500100, "#######")' />
</body>
</html>
</xsl:template>
 
</xsl:stylesheet>
 
 
 
function-available() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
 
function-available()函式檢查指定的函式在XSLT處理器中是否支援。
You may test the XSLT functions and the inherited XPath functions.
您可以檢查XSLT函式和XPath函式。
 
--------------------------------------------------------------------------------
 
語法
boolean function-available(string)
 
引數
引數 說明
string  必須。指定要檢查的函式。

 
 
例子 1
<?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>
<xsl:choose>
<xsl:when test="function-available('sum')">
<p>sum() is supported.</p>
</xsl:when>
<xsl:otherwise>
<p>sum() is not supported.</p>
</xsl:otherwise>
</xsl:choose>
<xsl:choose>
<xsl:when test="function-available('current')">
<p>current() is supported.</p>
</xsl:when>
<xsl:otherwise>
</xsl:otherwise>
</xsl:choose>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
 
 
generate-id() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
 
generate-id()函式返回一個唯一標示指定節點的字串。
 
如果指定的節點集是空的,將返回一個空字串。如果您忽略了節點集的引數,預設是當前節點。
 
--------------------------------------------------------------------------------
 
語法
string generate-id(node-set?)
 
引數
引數 說明
node-set  可選。指定要生成唯一id的節點集

 
 
例子 1
<?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>
<h3>Artists:</h3>
<ul>
<xsl:for-each select="catalog/cd">
<li>
<a href="#{generate-id(artist)}">
<xsl:value-of select="artist" /></a>
</li>
</xsl:for-each>
</ul>
<hr />
<xsl:for-each select="catalog/cd">
Artist: <a name="{generate-id(artist)}">
<xsl:value-of select="artist" /></a>
<br />
Title: <xsl:value-of select="title" />
<br />
Price: <xsl:value-of select="price" />
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
 
 
key() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
key()函式使用<xsl:key>元素指定的索引返回文件中的一個節點集。
 
--------------------------------------------------------------------------------
 
語法
node-set key(string, object)
 
引數
引數 說明
string  必須。指定xsl:key元素的名稱。
object  必須。要查詢的字串。

 
 
例子 1
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:key name="cdlist" match="cd" use="title" />
 
<xsl:template match="/">
<html>
<body>
<xsl:for-each select="key('cdlist', 'Empire Burlesque')">
 <p>
 Title: <xsl:value-of select="title" />
 <br />
 Artist: <xsl:value-of select="artist" />
 <br />
 Price: <xsl:value-of select="price" />
 </p>
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
 
 
system-property() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
The system-property() function returns the value of the system property specified by the name.
system-property()函式返回指定名稱的系統屬性。
System properties in the XSLT namespace:
在XSLT名稱空間裡的系統屬性:
xsl:version - 指出XSLT的版本
xsl:vendor - 指出XSLT處理器(比如James Clark)
xsl:vendor-url - 指出處理器的URL(比如http://www.jclark.com/)
(注:當用XT解析時xsl:vendor是James Clark,xsl:vendor-url是http://www.jclark.com/,用IE就為Microsoft,http://www.microsoft.com)
--------------------------------------------------------------------------------
 
語法
object system-property(string)
 
引數
引數 說明
string  必須。指定返回的系統屬性

 
 
例子 1
<?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>
<p>
Version:
<xsl:value-of select="system-property('xsl:version')" />
<br />
Vendor:
<xsl:value-of select="system-property('xsl:vendor')" />
<br />
Vendor URL:
<xsl:value-of select="system-property('xsl:vendor-url')" />
</p>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
 
 
 
unparsed-entity-uri() 函式
 
--------------------------------------------------------------------------------
 
定義與用法
 
unparsed-entity-uri()函式返回未解析的實體的URI。實體名稱必須與傳遞的引數匹配。如果沒有這樣的實體,那麼返回空串。
 
如果DTD包含下面的宣告:
<!ENTITY pic SYSTEM "http://www.w3schools.com/picture.jpg" NDATA JPEG>
 
下面的表示式
unparsed-entity-uri('pic')
 
將會返回"picture.jpg"的URI。
 
--------------------------------------------------------------------------------
 
語法
string unparsed-entity-uri(string)
 
引數
引數 說明
string  必須。指定未解析實體

 
 
 
例子 1
XML檔案
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="sample.xsl"?>
<!DOCTYPE catalog [
 <!ELEMENT catalog ANY>
 <!ELEMENT book ANY>
 <!ELEMENT title ANY>
 <!NOTATION JPEG SYSTEM "urn:foo">
 <!ENTITY pic SYSTEM "http://www.w3schools.com/picture.jpg" NDATA JPEG>
]>
<catalog>
 <book>
 <title>XML Developer's Guide</title>
 </book>
 <book>
 <title>Midnight Rain</title>
 </book>
</catalog>
 
XSL檔案
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"/>
 <xsl:template match="/">
 <html>
   <body>
    <h3>unparsed-entity-uri()</h3>
    <ul>
     <li>
      <b>unparsed-entity-uri('pic')</b> =
               <xsl:value-of select="unparsed-entity-uri('pic')"/>
     </li>
    </ul>
   </body>
 </html>
 </xsl:template>
</xsl:stylesheet>
 
 
運算子和特殊字元
!= 不等於
"" (literal) 文字
'' (literal) 文字
() (grouping) 分組
* (all nodes) 所有節點
* (multiplication) 萬用字元
+ 加
- 減
- (unary minus)
. (self axis short form) 當前元素
.. (parent axis short form) 父元素
/ (step separator) 選擇子元素
// (descendant-or-self axis short form) 選擇子元素,不論多深
:: (axis specifier) (不知道怎麼翻譯合適,沒用過)
< 小於
<= 小於等於
= 等於
> 大於
>= 大於等於
@ (attribute axis short form) 選擇屬性
@* (all attributes) 選擇所有屬性
[] (predicate) 斷言,指定過濾樣式
And 與
axis nodetest predicate (step)
div The div operator performs floating-point division according to IEEE 754.(原文放上來,不知道怎麼翻譯合適,沒用過)
func() (function call) 呼叫函式
mod 取餘
name (node test)
or 或
| (union) 集合
 
 
附錄
 如果沒有特殊說明,將使用下面的XML檔案
<?xml version="1.0" encoding="ISO-8859-1"?>
<!—在這裡引入您的XSL檔案 -->
<?xml-stylesheet type="text/xsl" href="cdcatalog_element.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>
 <cd>
 <title>Greatest Hits</title>
 <artist>Dolly Parton</artist>
 <country>USA</country>
 <company>RCA</company>
 <price>9.90</price>
 <year>1982</year>
 </cd>
 <cd>
  <title>Still got the blues</title>
 <artist>Gary Moore</artist>
 <country>UK</country>
 <company>Virgin records</company>
 <price>10.20</price>
 <year>1990</year>
 </cd>
 <cd>
 <title>Eros</title>
 <artist>Eros Ramazzotti</artist>
 <country>EU</country>
 <company>BMG</company>
 <price>9.90</price>
 <year>1997</year>
 </cd>
 <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>Maggie May</title>
 <artist>Rod Stewart</artist>
 <country>UK</country>
 <company>Pickwick</company>
 <price>8.50</price>
 <year>1990</year>
 </cd>
 <cd>
 <title>Romanza</title>
 <artist>Andrea Bocelli</artist>
 <country>EU</country>
 <company>Polydor</company>
 <price>10.80</price>
  <year>1996</year>
 </cd>
 <cd>
 <title>When a man loves a woman</title>
 <artist>Percy Sledge</artist>
 <country>USA</country>
 <company>Atlantic</company>
 <price>8.70</price>
 <year>1987</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>
 <cd>
 <title>Soulsville</title>
 <artist>Jorn Hoel</artist>
 <country>Norway</country>
 <company>WEA</company>
 <price>7.90</price>
 <year>1996</year>
 </cd>
 <cd>
 <title>The very best of</title>
 <artist>Cat Stevens</artist>
 <country>UK</country>
 <company>Island</company>
 <price>8.90</price>
 <year>1990</year>
 </cd>
 <cd>
 <title>Stop</title>
 <artist>Sam Brown</artist>
  <country>UK</country>
 <company>A and M</company>
 <price>8.90</price>
 <year>1988</year>
 </cd>
 <cd>
 <title>Bridge of Spies</title>
 <artist>T`Pau</artist>
 <country>UK</country>
 <company>Siren</company>
 <price>7.90</price>
 <year>1987</year>
 </cd>
 <cd>
 <title>Private Dancer</title>
 <artist>Tina Turner</artist>
 <country>UK</country>
 <company>Capitol</company>
 <price>8.90</price>
 <year>1983</year>
 </cd>
 <cd>
 <title>Midt om natten</title>
 <artist>Kim Larsen</artist>
 <country>EU</country>
 <company>Medley</company>
 <price>7.80</price>
 <year>1983</year>
 </cd>
 <cd>
 <title>Pavarotti Gala Concert</title>
 <artist>Luciano Pavarotti</artist>
 <country>UK</country>
 <company>DECCA</company>
 <price>9.90</price>
 <year>1991</year>
 </cd>
 <cd>
 <title>The dock of the bay</title>
 <artist>Otis Redding</artist>
 <country>USA</country>
 <company>Atlantic</company>
 <price>7.90</price>
 <year>1987</year>
 </cd>
 <cd>
 <title>Picture book</title>
 <artist>Simply Red</artist>
 <country>EU</country>
 <company>Elektra</company>
 <price>7.20</price>
 <year>1985</year>
 </cd>
 <cd>
 <title>Red</title>
 <artist>The Communards</artist>
 <country>UK</country>
 <company>London</company>
 <price>7.80</price>
 <year>1987</year>
 </cd>
 <cd>
 <title>Unchain my heart</title>
 <artist>Joe Cocker</artist>
 <country>USA</country>
 <company>EMI</company>
 <price>8.20</price>
 <year>1987</year>
 </cd>
</catalog>

相關文章