如何检索属性为 X 或 Y 的 XML 元素?

发布于 2024-08-07 20:37:40 字数 665 浏览 1 评论 0原文

考虑这个 xml:

<parent>
    <child name="alpha" />
</parent>

以及这个 xml

<parent>
    <child name="beta" />
</parent>

/parent/child 处应该只有一个 sinlge 节点,其名称值为 alpha 或 beeta。

澄清一下......永远不会有 2 个子节点,每个子节点都命名为 alphabeta

我正在尝试创建一个单一的 xpath 查询,它将返回 的值>name 在这两种情况下。

我尝试过这个......

/parent/child[@name='alpha' | @name='beta']/@name

但它不起作用。

我应该如何返回这个值..?

更新:我修改了示例以确保它们格式良好,因为一个答案表明它们不是。

Consider this xml:

<parent>
    <child name="alpha" />
</parent>

and also this xml

<parent>
    <child name="beta" />
</parent>

There should be only a sinlge node at /parent/child with either alpha or beeta as it's name value.

To clarrify... there will never be 2 child nodes one each named alpha and beta

I'm trying to create a single xpath query which will return the value of name in each of these 2 cases.

I tried this...

/parent/child[@name='alpha' | @name='beta']/@name

...but it does not work.

How should I return this value..?

Update: I ammendedd the samples to ensure they were well formed as one answer indicated they were not.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷心人i 2024-08-14 20:37:40

尝试使用:

/parent/child[(@name='alpha') or (@name='beta')]/@name

它也应该在没有括号的情况下工作。

Try with:

/parent/child[(@name='alpha') or (@name='beta')]/@name

It should also work without the parentheses.

长伴 2024-08-14 20:37:40

XML 似乎格式不正确,它应该是:

<parent>
    <child name="alpha"/>
</parent>

<parent>
    <child name="beta"/>
</parent>

如果 XML 格式不正确,我不会期望有太多其他功能。

The XML doesn't appear to be well-formed, it should be:

<parent>
    <child name="alpha"/>
</parent>

<parent>
    <child name="beta"/>
</parent>

If the XML isn't well-formed, I wouldn't expect much else to work..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文