split()
XPath 中是否有某种功能?假设我有这个 XML:
<root>
<path>C:\folder\filename</path>
</root>
我想检索filename
,我该怎么做?我知道我可以像这样获得节点值:
//path/text()
我怎样才能只得到文件名?(我知道有一个concat()
功能,所以也许有一个split()
功能?)
如果你有一个支持 xpath-2.0 的 API,你可以通过两种方式解决这个问题:
替换技术
尝试使用:
fn:replace(string,pattern,replace)
例如
fn:replace(//path/text(),".*/","")
标记技术
您可能会从 tokenize 中获得一些好处:
fn:tokenize(string,pattern)
例如(感谢马丁)
tokenize(/root/path, '\\')[last()]
虽然我会使用:
tokenize(/*/*, '\\')[last()]
还有许多其他方法可以获得所需的字符串:
codepoints-to-string
(reverse
(string-to-codepoints
(substring-before
(codepoints-to-string
(reverse
(string-to-codepoints(/*/*)
)
),
'\'
)
)
)
)
或者:
substring(/*/*,
index-of(string-to-codepoints(/*/*),
string-to-codepoints('\')
)
[last()]
+ 1
)