一次查询中属性值和子节点值的 Simplexml xpath

发布于 2024-09-14 11:20:46 字数 491 浏览 3 评论 0原文

示例 XML:

<users> 
  <user id="fakeuserid">

    <password>fakeuserpassword</password>

    <username>fakeusername</username>

  </user>

  <user id="anotherfakeuserid">

    <password>anotherfakeuserpassword</password>

    <username>anotherfakeusername</username>

  </user>
</users>

我希望能够访问每个用户的 id 属性和用户名值。 我怎么能这么做呢? 目前我正在尝试: /*/user 和 //user 无济于事

提前致谢, 玩具

example XML:

<users> 
  <user id="fakeuserid">

    <password>fakeuserpassword</password>

    <username>fakeusername</username>

  </user>

  <user id="anotherfakeuserid">

    <password>anotherfakeuserpassword</password>

    <username>anotherfakeusername</username>

  </user>
</users>

I would like to be able to access the id attribute and username value of each user.
How could I do that?
At the moment I am trying it with: /*/user and //user to no avail

Thanks in advance,
Toy

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

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

发布评论

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

评论(2

沐歌 2024-09-21 11:20:46

使用

/users/user/@id | /users/user/username

请注意 XPath union 运算符 | 的使用

Use:

/users/user/@id | /users/user/username

Do note the use of the XPath union operator |

梦途 2024-09-21 11:20:46

在 xpath 中:

  • userId = '//user/@id'
  • username = '//user/username'

这是您要找的吗?

In xpath:

  • userId = '//user/@id'
  • username = '//user/username'

Is this what you're looking for?

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