xpath 中可以有条件字符串连接或连接吗?

问题描述 投票:0回答:1

我正在尝试执行条件字符串连接,想知道是否有比使用 if 语句更好的方法。

xml 看起来类似于

<office>
    <department>Math</department>
    <room>210</room>
</office>

房间标签是可选的。如果房间标签存在,我想获取像“Math/210”这样的值,否则只是“Math”。

我正在使用

string-join(('//office/department', '//office/room'), '/')
,但当房间标签不存在时,会选择“数学/”。

如果我这样做,我就能让它按照我想要的方式工作

if(exists('//office/room') then string-join(('//office/department', '//office/room'), '/') else '//office/department'

这是唯一的方法还是有更好(更干净)的解决方案?

xpath xpath-2.0
1个回答
2
投票

我正在使用 string-join(('//office/department', '//office/room'), '/') 但当房间标签不存在时会选择 'Math/'。

这里的引用显然是错误的。应该是

string-join((//office/department, //office/room), '/')

或更简洁地

//office/string-join((department, room), '/')

假设房间元素确实不存在,这应该会给你你想要的东西。如果“room”选择空序列,则不应有分隔符。当然,如果 room 确实存在并且包含零长度字符串,那就是另一回事了。

© www.soinside.com 2019 - 2024. All rights reserved.