【问题标题】:Child nodes to comma delimited string with Xpath?子节点到 Xpath 的逗号分隔字符串?
【发布时间】:2016-01-22 22:42:49
【问题描述】:

我的 XML 看起来像这样:

<node1>
 <item>hello</item>
 <item>world</item>
</node1>

我希望它使用 XPath 表达式输出以下内容:

hello, world

这在 XPath 1.0 中是否可行?我一直在环顾四周,但找不到任何东西。

谢谢, 电影神

【问题讨论】:

  • XPath 1.0 还是 2.0?另外,您真的希望将讨论限制在 XPath 上,还是可以引入 XSLT 或 .NET 等上下文环境?
  • 好点。我对 XPath 1.0 很感兴趣。

标签: xml xpath


【解决方案1】:

XPath 2.0 可以做到string-join(/node1/item, ', ')。使用 XPath 1.0,您无法做到这一点,您需要使用 XSLT 之类的宿主语言或公开 XPath API 的过程语言来迭代节点和连接值。

【讨论】:

  • 感谢马丁的快速回复!
【解决方案2】:

在 XPath 1.0 中,您可以使用 concat() 来做到这一点,例如:

concat(node1/item[1], ", ", node1/item[2])

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多