XForms xpath使用手册

本文所描述的所有函数为XForms 1.1所提供的xpath函数,这些函数只能在GForms的xpath编辑器中可用,这样才能在运行时识别并执行。

不可在XMLUtil、标准XML DOM中使用这些函数。

GForms平台提供xpath编辑器,该编辑器可以协助开发者完成大多数xpath表达式开发:

XForms xpath使用手册_第1张图片


1、节点函数

1.1、instance函数

node-setinstance(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 thestringfunction. This string is treated as an IDREF, which is matchedagainstinstanceelements 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 thefirstNamenode. Note that theinstancefunction returns an elementnode, effectively replacing the leftmost location step from the path:

ref="instance('orderform')/shipTo/firstName"


2、字符串函数

2.1、if函数

stringif(boolean,string,string)

Functionifevaluates the first parameter as boolean, returning the secondparameter whentrue, otherwise the third parameter.

2.2、property函数

stringproperty(string)

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,basicor a string beginning withfullorbasic

Any otherNCName

Reserved. Their use results in an exception (see7.5 The XForms Function Libraryfor 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')

returns1.1

property('conformance-level')

may returnfull

3、数值函数

3.1、avg

numberavg(node-set)

Functionavgreturns the arithmetic average of the result of converting thestring-values of each node in the argument node-set to a number. The sum iscomputed withsum(), and divided withdivby the value computed withcount(). If the parameter is an empty node-set, or if any of the nodesevaluate toNaN, the return value isNaN.

3.2、number

numbermin(node-set)

Functionminreturns the minimum value of the result of converting thestring-values of each node in argumentnode-setto a number."Minimum" is determined with the<operator. If the parameter is an empty node-set, or if any of thenodes evaluate toNaN, the return value isNaN.

3.3、max

numbermax(node-set)

Functionmaxreturns the maximum value of the result of converting thestring-values of each node in argumentnode-setto a number."Maximum" is determined with the<operator. If the parameter is an empty node-set, or if any of thenodes evaluate toNaN, the return value isNaN.

3.4. count-non-empty

numbercount-non-empty(node-set)

Functioncount-non-emptyreturns the number ofnon-empty nodes in argumentnode-set. A node isconsidered non-empty if it is convertible into a string with a greater-thanzero length.

3.5. index

numberindex(string)

Functionindextakes a string argument that is theIDREFof arepeatand returns the current 1-based position of the repeat index forthe identifiedrepeat—see9.3.1 The repeat Elementfor details onrepeatand its associated repeat index. If the specified argument doesnot identify arepeat, the function returnsNaN.

Note:

The IDREF obtained from the function parameter may not uniquelyidentify the desiredrepeatif therepeatelement bearing the matching ID resides in a repeating constructsuch as elementrepeat. The general method described in4.7 Resolving ID References in XFormsis 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>

3.6. power

numberpower(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, thenNaNis returned.

Examples:

power(2, 3)

returns8

power(-1, 0.5)

returnsNaN.

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).

3.7. random

numberrandom(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 isfalseby default. Iftrue, 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 whenrandom(true)is invoked, and it may apply a linear congruential formula togenerate return values on successive invocations of the function.

Example:

random()

could return0.14159265358979

3.8. compare

numbercompare(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

4、布尔函数

booleanboolean-from-string(string)

Functionboolean-from-stringreturnstrueif the required parameterstringis "true" or "1", orfalseif parameterstringis "false", or "0". This is useful whenreferencing a Schemaxsd:booleandatatype in an XPath expression. If the parameter string matchesnone of the above strings, according to a case-insensitive comparison, thereturn value isfalse.

5、时间函数

时间的格式处理,为了简化本地化时间处理,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。

5.1、now函数

stringnow()

得到当前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以方便本地化编程处理。

5.2、local-date函数

stringlocal-date()

将返回与XSD数据类型xsd:date格式一致的字符串,该时间为本本地区时间,而非UTC时间,在GForms中将根据confgi_xx.xslt中的date配置格式进行格式化返回。

local-date()

返回格式2006-10-13

5.3、local-dateTime

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。

5.4、days-from-date

numberdays-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,即小时、分钟及秒被忽略掉。

5.5、days-to-date

string days-to-date( number)

把输入的天数转换为时间字符串,天数将从1970-01-01之后开始计算,如果输入的天数非法,则返回空字符串。

days-to-date(11688)

返回2002-01-01

days-to-date(-1)

返回1969-12-31

6、工具函数

6.1、加密函数digest

stringdigest(string,Algorithm,encoding?)

对输入的字符串按照加密算法Algorithm加密并使用编码算法encoding进行编码,并把计算结果返回。

其中Algorithm目前支持MD5、SHA-1两种加密算法,encoding支持的有base64、hex,默认为base64。

digest('abc', 'SHA-1','hex')

将返回:a9993e364706816aba3e25717850c26c9cd0d89d.

6.2、匹配函数match

boolean match( node, regex)

根据正则表达式校验输入的模型实例节点node是否满足regex。

这里的regex需要进行转义处理,如:

/^[\w-]+(\.[\w-]+)*@[\w-]+(\.[\w-]+)+$/

需要编写为:

match( node, '^[\\w-]+(\\.[\\w-]+)*@[\\w-]+(\\.[\\w-]+)+$')

6.3、事件函数event

objectevent(propertyName)

得到当前最新事件属性值,如:

event('response-reason-phrase')

如果后端发生内部错误,则得到的信息为一个字符串值:Internal Server Error

具体事件的可用属性信息是不一样的,可以参考相关控件。

6.4、合法性校验is-valid

booleanis-valid(nodeSet)

校验所输入的节点是否合法,如果合法则返回true,否则返回false。

根据数据节点的绑定、schema等进行校验。

7.xpath特殊字符

如果需要在xpath中输出这些特殊字符必须在xpath中使用转义字符。

特殊字符

特殊含义

实体编码

>

开始标记

&gt;

<

结束标记

&lt;

"

引号

&quot;

'

撇号

&apos;

&

"&"

&amp;

8.参考手册

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/


你可能感兴趣的:(xpath)