【问题标题】:XPath Axes - Replace 'text' value of child nodes under a parent with parent node's attribute valueXPath Axes - 用父节点的属性值替换父节点下的子节点的“文本”值
【发布时间】:2013-05-22 10:07:12
【问题描述】:

我正在尝试将 XPath 应用于具有以下结构的 XML 文件:

XPath:/root/tour/round/event/parent::round/@name

预期的输出是:

final
semifinal
semifinal
quarterfinal
quarterfinal
quarterfinal
quarterfinal
round3

但我没有得到上面的输出,而是这样:

final
semifinal
quarterfinal
round3

对于获得预期输出的任何帮助,我将不胜感激。

XML 文件

<root>
    <tour>
        <round name="final">
            <event>Test</event>
        </round>
        <round name="semifinal">
            <event>Test</event>
            <event>Test</event>
        </round>
        <round name="quarterfinal">
            <event>Test</event>
            <event>Test</event>
            <event>Test</event>
            <event>Test</event>
        </round>
        <round name="round3">
            <event>Test</event>
            <event>Test</event>
            <event>Test</event>
            <event>Test</event>
        </round>
    </tour>
</root>

【问题讨论】:

  • 到目前为止你有什么?你在使用代码吗? XSLT?我们需要有这样的背景才能为您提供帮助。
  • 您不能使用单个 XPath 1.0 表达式来执行此操作。 XPath 1.0 表达式可以生成一个节点集,它是一组不同的节点。由于文档中只有 4 个 @name 节点,所以这就是选择的所有内容。

标签: xml xpath axes


【解决方案1】:

您需要在 XPATH 条件下放置循环:

for $eachEvent in //event return $eachEvent/parent::round/@name

【讨论】:

  • 谢谢@navin。我正在寻找纯 XPath,您认为使用纯 XPath 是否可行?
  • 这是有效且普通的 XPath 2.0。
  • 然后向我们提供有关您使用的查询处理器的更多信息。可能它不支持 XPath 2.0,但这是对您提出的问题的有效答案。但这在 XPath 1.0 中可能是不可能的,您必须循环事件并用周围使用的编程语言构造输出。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多