本文所描述的所有函数为XForms 1.1所提供的xpath函数,这些函数只能在GForms的xpath编辑器中可用,这样才能在运行时识别并执行。
不可在XMLUtil、标准XML DOM中使用这些函数。
GForms平台提供xpath编辑器,该编辑器可以协助开发者完成大多数xpath表达式开发:
node-set instance(string?)
An XForms Model can contain more than one instance. This functionallows access to instance data, within the same XForms Model, but outside theinstance data containing the context node.
If the argument is omitted or is equal to the empty string, thenthe root element node (also called the document element node) is returned forthe default instance in the model that contains the current context node.
Otherwise, the argument is converted to a string as if by a callto the string function. This string is treated as an IDREF, which is matchedagainst instance elements in the containing document. If a match is located, andthe matching instance data is associated with the same XForms Model as thecurrent context node, this function returns a node-set containing just the rootelement node (also called the document element node) of the referenced instancedata. In all other cases, an empty node-set is returned.
Example:
For instance data corresponding to this XML:
<xforms:instancexmlns="" id="orderform">
<orderForm>
<shipTo>
<firstName>John</firstName>
</shipTo>
</orderForm>
</xforms:instance>
The following expression selects the firstName node. Note that the instance function returns an elementnode, effectively replacing the leftmost location step from the path:
ref="instance('orderform')/shipTo/firstName"
string if(boolean, string, string)
Function if evaluates the first parameter as boolean, returning the secondparameter when true, otherwise the third parameter.
This function accepts a string identifying a property name. If theproperty name is not recognized, empty string is returned. The propertydefinitions 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) |
QNameButNotNCName |
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 thestring-values of each node in the argument node-set to a number. The sum iscomputed 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 nodesevaluate to NaN, the return value is NaN.
Function min returns the minimum value of the result of converting thestring-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 thenodes evaluate to NaN, the return value is NaN.
Function max returns the maximum value of the result of converting thestring-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 thenodes evaluate to NaN, the return value is NaN.
number count-non-empty(node-set)
Function count-non-empty returns the number ofnon-empty nodes in argument node-set. A node isconsidered non-empty if it is convertible into a string with a greater-thanzero 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 forthe identifiedrepeat—see 9.3.1 The repeat Element for details on repeat and its associated repeat index. If the specified argument doesnot identify a repeat, the function returns NaN.
Note:
The IDREF obtained from the function parameter may not uniquelyidentify the desired repeat if the repeat element bearing the matching ID resides in a repeating constructsuch 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 ShoppingCart</xforms:label>
<xforms:insertev: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 anddur>0 and rate>0, prin*rate/(1-power(1+rate, -dur)), 0)
returns a compounded interest payment value given a non-zeroprincipal (prin), duration (dur) and periodic interest rate(rate).
number random(boolean?)
This function generates and returns a uniformly distributed randomor pseudorandom number in the range from 0.0 up to but excluding 1.0. Thisfunction accepts an author-optional boolean parameter that is false by default. If true, the random number generatorfor this function is first seeded with a source of randomness before generatingthe return value. A typical implementation may seed the random number generatorwith the current system time in milliseconds when random(true) is invoked, and it may apply a linear congruential formula togenerate 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 valueof the first argument is respectively less than, equal to, or greater than thevalue of second argument based on lexicographic comparison using Unicode codepoint 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 whenreferencing a Schema xsd:boolean datatype in an XPath expression. If the parameter string matchesnone of the above strings, according to a case-insensitive comparison, thereturn 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
返回与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,即小时、分钟及秒被忽略掉。
把输入的天数转换为时间字符串,天数将从 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支持的有base64、hex,默认为base64。
digest('abc', 'SHA-1','hex')
将返回: a9993e364706816aba3e25717850c26c9cd0d89d.
根据正则表达式校验输入的模型实例节点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标准:
http://www.w3.org/TR/xpath20/
3. xpath使用手册
http://www.w3school.com.cn/xpath/