XPath 1.0对字符串操作的支持相当有限,所以拆分起来相当烦人。使用substring($string, $start[, $length])
,substring-before($string, $needle)
和substring-after($string, $needle)
:
substring(...)
如果字符串是固定长度的(例如,没有4.2
出现只有三个字符的长度),那就没问题了:
substring(//@hsdl-percent, 1, 4)
substring(//@hsdl-percent, 6, 4)
substring(//@hsdl-percent, 11, 4)
如果长度可以改变,则需要在空格字符处拆分:
substring-before(//@hsdl-percent, ' ')
substring-before(substring-after(//@hsdl-percent, ' '), ' ')
substring-after(substring-after(//@hsdl-percent, ' '), ' ')
如果您支持XPath 2.0(或更好),请使用tokenize($string, $needle)
:
tokenize(//@hsdl-percent, ' ') (: returns sequence of individual values :)
tokenize(//@hsdl-percent, ' ')[1]
tokenize(//@hsdl-percent, ' ')[2]
tokenize(//@hsdl-percent, ' ')[3]