跟我學XSL(9)XSL函式二(轉)

amyz發表於2007-08-12
跟我學XSL(9)XSL函式二(轉)[@more@]

  本期介紹多個XSL對於VBScript、JScript增加的方法、屬性,以充分發揮XML的優勢,用於

標記內表示式的編寫或的expr屬性。

  一、absoluteChildNumber

  含義:返回結點相對於它所有的兄弟(不論名字是否相同)的序號。

  語法:absoluteChildNumber(node)

  引數:node ── 物件,欲返回編號的結點。

  示例:

  1、假定文件結構為:

,其中document為頂層結點,下述表示式將輸出:

  absoluteChildNumber(this.selectNodes('/document/body').item(0))

  2、確定當前結點相對於其所有兄弟的序號:

  absoluteChildNumber(this)

  二、ancestorChildNumber

  含義:從給定結點出髮根據給定祖先結點名返回最近的祖先結點的序號(相對於同名結點)。如果找不祖先,則返回0。

  語法:ancestorChildNumber(bstrNodeName, pNode)

  引數:

  bstrNodeName ── 字串。被搜尋的祖先結點的名字。

  pNode ── 物件。搜尋開始位置的結點。

  示例查詢當前結點最近的名為report祖先結點:

  ancestorChildNumber('report',this)

  三、attributes

  含義:返回結點屬性的集合。

  語法:object.attributes

  引數:object ── 結點物件。

  示例:當前結點屬性的個數

  this.attributes.length

  當前結點第三個屬性的值

  this.attributs.item(2).value

  或this.attributes.item(2).text

  或this.attributes(2).text

  注意:如果給定的下標大於屬性總和減1將出錯,第一個屬性的下標是0。

  四、baseName

  含義:返回有名字空間限制的基本名,即不包括名字字首。

  語法:object.baseName

  引數:object ── 結點物件

  示例,當前結點的基本名:this.baseName

  五、childNumber

  含義:返回結點相對於同名同胞的序號。

  語法:childNumber(object)

  引數:object ── 結點物件

  示例,假定XML文件結構如下:

  

  如果當前結點是z,則childNumber(this)返回1,而absoluteChildNumber(this)返回3。

  六、dataType

  含義:設定或讀取結點的資料型別。

  語法:設定結點的資料型別 object.dataType=objValue     讀取結點的資料型別 objValue=object.dataType

  引數:object ── 結點物件。  示例,讀取當前結點的資料型別:

  dtType=this.dataType

  七、depth

  含義:指定結點出現在文件樹上的深度,即該結點位於文件第幾層,頂層結點位於第一層,根結點(即用"/"表示的結點)位於第0層。

  語法:depth(pNode)

  引數:pNode ── 結點物件

  示例,當前結點的深度:

  depth(this)

  八、firstChild、lastChild

  含義:返回結點的第一個子結點(或最後一個子結點)。

  語法:pNode.firstChild     pNode.lastChild

  引數:pNode ── 結點物件

  示例,當前結點的第一個結點的名字:

  this.firstChild.nodeName

  九、formatIndex

  含義:用指定的計數系統格式化提供的整數。

  語法:formatIndex(lIndex, bstrFormat)

  引數:

  lIndex ── 整型數值或變數

  bstrFormat ── 資料格式,可選值有a、A、i、I、1、01(以0打頭的數值形式,如果要求固定長度的編號如0001、0002則非常有用)。

  示例,當前結點的大寫羅馬數字編號:

  formatIndex(childNumber(this),'I')

  十、formatNumber

  含義:以指定格式輸出數值。

  語法:formatNumber(dblNumber, bstrFormat)

  引數:說明同formatNumber,不同之處在於格式化的可以是小數。

  示例,對變數a的值格式化為兩位小數:formatNumber(a,'#.00'):

  十一、hasChildNodes

  含義:如果結點有子結點則返回true(-1),否則為false(0)。

  語法:pNode.hasChildNodes()

  注意:與此前介紹的函式不同,此函式後必須帶一個空括號。

  示例,判斷當前結點是否有子結點:

  this.hasChildNodes

  十二、namespaceURI、prefix

  含義:返回結點名字空間的全域性資源識別符號(或字首)。

  語法:pNode.namespaceURI     pNode.prifix

  十三、nextSibling、previousSibling、parentNode

  含義:返回結點的下一個兄弟(或前一個兄弟、或結點的父結點)。

  語法:pNode.nextSibling     pNode.previousSibling     pNode.parentNode

  注意:對根結點(即"/")應用parentNode方法、對第一個孩子結點應用previousSibling方法、對最後一個孩子結點應用nextSibling方法均會導致錯誤,可透過此過關係運算子==(等於)和!=(不等於)來判斷一個結點是否某一指定結點,格式為pNode1 = pNode2或pNode2 != pNode2。

  十四、nodeName

  含義:返回元素、屬性、入口的名字或其他型別結點的一個特定字串。

  語法:pNode.nodeName

  示例,當前結點的名字:

  this.nodeName

  十五、nodeType、NodeTypeString

  含義:返回結點的型別的數值形式(或字串形式)。  語法:pNode.nodeType 或 pNode.nodeTypeString

  返回值:

 結點型別  結點型別值  結點的字元形式描述
 Element  1  'element'
 Element Attribute  2  'attribute'
 Markup-Delimited Region of Text  3  'text'
 Processing Instruction  7  'processing_instruction'
 Comment  8  'comment'
 Document Entity  9  'document'

  十六、nodeTypedValue

  含義:以結點預定義的資料型別返回結點的值。

  語法:pNode.nodeTypedValue

  示例,假定當前結點的資料型別是fixed.14.4,下例將以數值返回結點的值,而不是文字一個字串:

  this.nodeTypedValue

  十七、nodeValue

  含義:返回結點的文字。

  語法:pNode.nodeValue

  注意:該方法不用於元素類結點,可用於屬性、CDATA、註釋、文字等結點。

  示例,當前元素第一個屬性的值:

  this.attributes(0).nodeValue

  當前元素內的文字(假定該元素內只有文字,無其它元素,即text,建議多嘗幾次掌握其確切的用法)。

  this.firstChild.nodeValue

  十八、ownerDocument

  含義:返回包含該結點的文件的根。

  語法:pNode.ownerDocument

  注意:該方法用於文件的根結點將出錯。

  十九、selectNodes

  含義:給定的樣式匹配應用於當前結點並返回匹配的結點集合。

  語法:pNode.selectNodes('pattern')

  提示:pattern的編寫與的select屬性的值類似,其中以"/"開頭表示從文件的根出發搜尋;以"//"開頭表遍歷文件的所有結點;以".."開頭表示從當前結點的父結點開始;如果欲從當前結點向下搜尋則不能有以上特殊字元打頭。

  示例,與當前結點同名的元素在其父元素內的個數:

  childNumber(this.selectNodes("../"+this.nodeName+"[end()]").item(0))

  當前元素內名字為"skill"的元素的個數:

  childNumber(this.selectNodes("skill[end()]").item(0))

  二十、selectSingleNode

  含義:與selectNodes類似,不同的只返回匹配的第一個結點、而不是結點集合。

  語法:pNode.selectSingleNode('pattern')

  示例,與當前結點同名的元素在其父元素內的個數:

  childNumber(this.selectSingleNode("../"+this.nodeName+"[end()]"))

  當前元素內名字為"skill"的元素的個數:

  childNumber(this.selectSingleNode("skill[end()]"))

  二十一、text

  含義:返回結點與它的子樹內的文字內容。

  語法:pNode.text

  示例,整個文件內的文字內容:

  this.ownerDocument.text

  當前元素及其子樹的文字內容:

  this.text

  二十二、xml

  含義:返回結點及其後代的XML表示。

  語法:pNode.xml

  示例,當前文件的XML內容:

  this.ownerDocument.xml

  另有幾個函式不作介紹,列於其下以供參考,如感興趣,請訪問獲取詳細說明。

  formatTime(varTime, bstrFormat,varDestLocale)formatDate(varDate, bstrFormat,varDestLocale)apendChild(newChild)definitionCloneNodeinsertBefore(newChild, refChild)parsedremoveChild(oldChild)replaceChild(newChild, oldChild)specifiedtransformNode(stylesheet)transformNodeToObject(stylesheet,outputObject)uniqueID(pNode)


相關文章