Xpath选择功能?

问题描述:

我在Drupal 7 cms中使用了feed导入模块,它接受xpath指令来映射导入数据。是否有可能只选择部分原子值或用xpath分解它? 例如:Xpath选择功能?

<book> 
... 
<categories>1,4,88</categories> 
... 
</book> 

是否有可能与XPath语句只得到1或4或88分开?

可以使用substring-before()substring-after()功能:

  • 要选择1:substring-before(/book/categories, ',')
  • 要选择4:substring-before(substring-after(/book/categories, ','), ',')
  • 要选择88:substring-after(substring-after(/book/categories, ','), ',')

在XPath 2.0你可以使用tokenize()功能:

  • 要选择1:tokenize(/book/categories, ',')[1]
  • 选择4:tokenize(/book/categories, ',')[2]
  • 要选择88:tokenize(/book/categories, ',')[3]

或者,使用XPath 2.0,可以通过访问N th组件:

tokenize(/*/categories[1], ',')[$pN] 

其中$pN是所需的基于1的索引。