本文所描述的全部函數爲XForms 1.1所提供的xpath函數,這些函數只能在GForms的xpath編輯器中可用,這樣才能在運行時識別並執行。node
不可在XMLUtil、標準XML DOM中使用這些函數。web
GForms平臺提供xpath編輯器,該編輯器能夠協助開發者完成大多數xpath表達式開發:正則表達式
node-set instance(string?)算法
An XForms Model can contain more than one instance. This function allows access to instance data, within the same XForms Model, but outside the instance data containing the context node.express
If the argument is omitted or is equal to the empty string, then the root element node (also called the document element node) is returned for the default instance in the model that contains the current context node.編程
Otherwise, the argument is converted to a string as if by a call to the string function. This string is treated as an IDREF, which is matched against instance elements in the containing document. If a match is located, and the matching instance data is associated with the same XForms Model as the current context node, this function returns a node-set containing just the root element node (also called the document element node) of the referenced instance data. In all other cases, an empty node-set is returned.後端
Example:app
For instance data corresponding to this XML:less
1 <xforms:instance xmlns="" id="orderform"> 2 3 <orderForm> 4 5 <shipTo> 6 7 <firstName>John</firstName> 8 9 </shipTo>10 11 </orderForm>12 13 </xforms:instance>
The following expression selects the firstName node. Note that the instance function returns an element node, effectively replacing the leftmost location step from the path:dom
ref="instance('orderform')/shipTo/firstName"
string if(boolean, string, string)
Function if evaluates the first parameter as boolean, returning the second parameter when true, otherwise the third parameter.
string property(string)
This function accepts a string identifying a property name. If the property name is not recognized, empty string is returned. The property definitions for this function are as follows:
Property |
Return Value |
version |
1.1 |
conformance-level |
full, basic or a string beginning with full or basic |
Any other NCName |
Reserved. Their use results in an exception (see 7.5 The XForms Function Library for the exception type) |
An implementation-specific property value, such as a locale or timezone for the user agent. If the implementation does not support the property, then empty string is returned. |
Examples:
property('version')
returns 1.1
property('conformance-level')
may return full
number avg(node-set)
Function avg returns the arithmetic average of the result of converting the string-values of each node in the argument node-set to a number. The sum is computed with sum(), and divided with div by the value computed with count(). If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
number min(node-set)
Function min returns the minimum value of the result of converting the string-values of each node in argument node-set to a number. "Minimum" is determined with the < operator. If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
number max(node-set)
Function max returns the maximum value of the result of converting the string-values of each node in argument node-set to a number. "Maximum" is determined with the < operator. If the parameter is an empty node-set, or if any of the nodes evaluate to NaN, the return value is NaN.
number count-non-empty(node-set)
Function count-non-empty returns the number of non-empty nodes in argument node-set. A node is considered non-empty if it is convertible into a string with a greater-than zero length.
number index(string)
Function index takes a string argument that is the IDREF of a repeat and returns the current 1-based position of the repeat index for the identifiedrepeat—see 9.3.1 The repeat Element for details on repeat and its associated repeat index. If the specified argument does not identify a repeat, the function returns NaN.
Note:
The IDREF obtained from the function parameter may not uniquely identify the desired repeat if the repeat element bearing the matching ID resides in a repeating construct such as element repeat. The general method described in 4.7 Resolving ID References in XForms is used to determine the desired run-time repeat object.
index
<xforms:trigger><xforms:label>Add to Shopping Cart</xforms:label><xforms:insert ev:event="DOMActivate" position="after"nodeset="items/item" at="index('cartUI')"/></xforms:trigger>
number power(number, number)
Raises the first argument to the power of the second argument, returning the result. If the calculation does not result in a real number, then NaNis returned.
Examples:
power(2, 3)
returns 8
power(-1, 0.5)
returns NaN.
if (prin>0 and dur>0 and rate>0, prin*rate/(1-power(1+rate, -dur)), 0)
returns a compounded interest payment value given a non-zero principal (prin), duration (dur) and periodic interest rate (rate).
number random(boolean?)
This function generates and returns a uniformly distributed random or pseudorandom number in the range from 0.0 up to but excluding 1.0. This function accepts an author-optional boolean parameter that is false by default. If true, the random number generator for this function is first seeded with a source of randomness before generating the return value. A typical implementation may seed the random number generator with the current system time in milliseconds when random(true) is invoked, and it may apply a linear congruential formula to generate return values on successive invocations of the function.
Example:
random()
could return 0.14159265358979
number compare(string, string)
This function returns -1, 0, or 1, depending on whether the value of the first argument is respectively less than, equal to, or greater than the value of second argument based on lexicographic comparison using Unicode code point values [Unicode Collation Algorithm].
Example:
compare('apple', 'orange')
returns -1
boolean boolean-from-string(string)
Function boolean-from-string returns true if the required parameter string is "true" or "1", or false if parameter string is "false", or "0". This is useful when referencing a Schema xsd:boolean datatype in an XPath expression. If the parameter string matches none of the above strings, according to a case-insensitive comparison, the return value is false.
時間的格式處理,爲了簡化本地化時間處理,gforms提供對時間的格式進行了可配置處理,按照xsd:date、xsd:dateTime,時間的字符串中會包含T、z等字符串信息,而在大多數編程場景下,通常不須要處理T、z信息,因此爲了提供編程開發人員處理時間的效率,GForms對xsd:dateTime的合法格式進行可配置化。
系統默認時間格式:
格式類型 |
標準格式 |
GForms格式 |
xsd:date |
yyyy-MM-ddz |
yyyy-MM-dd |
xsd:dateTime |
yyyy-MM-ddThh:mm:ssz |
yyyy-MM-dd hh:mm:ss |
上述時間格式配置在GForms應用的<webContent>/ghorse/js/conf/config_xx.xsl文件中的date及dateTime。
string now()
獲得當前UTC時間字符串,字符串格式與XSD的數據類型xsd:dateTime格式一致,而在GForms中所返回的時間字符串是按照config_xx.xslt中的dateTime格式進行格式化。
now()
按標準應該返回:2014-03-14T01:04:17Z,但在GForms中將根據config_xx.xslt中的dateTime配置進行返回:2014-03-14 01:04:17以方便本地化編程處理。
string local-date()
將返回與XSD數據類型xsd:date格式一致的字符串,該時間爲本本地區時間,而非UTC時間,在GForms中將根據confgi_xx.xslt中的date配置格式進行格式化返回。
local-date()
返回格式 2006-10-13
string local-dateTime()
返回與xsd:daeTime格式一致的當前本地區時間,在gforms平臺中,該時間將根據config_xx.xslt中的dateTime進行格式化返回。
local-dateTime()
按標準應該返回: 2006-10-13T16:04:17-07:00
但在GForms中若是是中國地區則會返回: 2006-10-13 16:04:17。
number days-from-date(string)
返回輸入時間字符串所含有的天數,輸入的時間字符串必須符合xsd:date或xsd:dateTime格式,但在GForms中要求輸入字符串符合config_xx.xslt中的date、dateTime格式規範。
將把輸入的時間轉換爲UTC格式,而後計算與 1970-01-01時間之間的天數。
若是輸入的字符串參數不知足條件則返回NaN.
days-from-date("2002-01-01")
返回:11688
days-from-date("2002-01-01-07:00")
返回:11688,即小時、分鐘及秒被忽略掉。
string days-to-date(number)
把輸入的天數轉換爲時間字符串,天數將從 1970-01-01以後開始計算,若是輸入的天數非法,則返回空字符串。
days-to-date(11688)
返回 2002-01-01
days-to-date(-1)
返回 1969-12-31
string digest(string, Algorithm, encoding?)
對輸入的字符串按照加密算法Algorithm加密並使用編碼算法encoding進行編碼,並把計算結果返回。
其中Algorithm目前支持MD5、SHA-1兩種加密算法,encoding支持的有base6四、hex,默認爲base64。
digest('abc', 'SHA-1', 'hex')
將返回: a9993e364706816aba3e25717850c26c9cd0d89d.
boolean match(node, regex)
根據正則表達式校驗輸入的模型實例節點node是否知足regex。
這裏的regex須要進行轉義處理,如:
/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/
須要編寫爲:
match( node, '^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$' )
object event(propertyName)
獲得當前最新事件屬性值,如:
event( 'response-reason-phrase')
若是後端發生內部錯誤,則獲得的信息爲一個字符串值:Internal Server Error
具體事件的可用屬性信息是不同的,能夠參考相關控件。
boolean is-valid (nodeSet)
校驗所輸入的節點是否合法,若是合法則返回true,不然返回false。
根據數據節點的綁定、schema等進行校驗。
若是須要在xpath中輸出這些特殊字符必須在xpath中使用轉義字符。
特殊字符 |
特殊含義 |
實體編碼 |
> |
開始標記 |
> |
< |
結束標記 |
< |
" |
引號 |
" |
' |
撇號 |
' |
& |
"&"符 |
& |
1. XForms提供的XPath函數詳解請參考XForms標準:
http://www.w3.org/TR/2009/REC-xforms-20091020/#expr
2. XPath2.0標準:
3. xpath使用手冊
http://www.w3school.com.cn/xpath/