dom4j 的 XPATH 问题

发布于 2024-10-07 19:39:30 字数 845 浏览 0 评论 0原文

我正在使用 dom4j 覆盖 XML 中的值。 XML 如下所示:

<start>
    <name color="blue" time="555555">
        <element1 param="1">
            <value>value1</value>
            <value>value2</value>
            <value>value3</value>
        <element1>
    </name>

    <name color="blue" time="888888">
        <element2 param="1">
            <value>value1</value>
            <value>value2</value>
            <value>value3</value>
        <element1>
    </name>
</start>

我试图通过以下方式来查找节点:

List list= document.selectNodes("//element1[@timetime='555555']" );

但列表返回 null。 我想更改 time="555555" 处的所有 3 个值。

有没有办法直接去那个节点。

请帮忙。

I am using dom4j to overwrite a value in the XML.
The XML looks like this:

<start>
    <name color="blue" time="555555">
        <element1 param="1">
            <value>value1</value>
            <value>value2</value>
            <value>value3</value>
        <element1>
    </name>

    <name color="blue" time="888888">
        <element2 param="1">
            <value>value1</value>
            <value>value2</value>
            <value>value3</value>
        <element1>
    </name>
</start>

I am trying to semect nodes by:

List list= document.selectNodes("//element1[@timetime='555555']" );

but the list returns null.
I wanted to change all the 3 values where time="555555".

Isn't there a way to directly go to that node.

please help.

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

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

发布评论

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

评论(2

有深☉意 2024-10-14 19:39:30

要选择三个值,请使用:

//name[@time='555555']/element1/value

如果也返回 null,则可能涉及默认名称空间,这意味着您需要显示整个 XML。

XPath 是灵活的,如果你愿意,你可以像这样表达:

//value[ancestor::name[1]/@time='555555']

to select the three values, use:

//name[@time='555555']/element1/value

If that returns null as well, there may be a default namespace involved and that means you need to show your entire XML.

XPath is flexible, if you want you can express the same like this:

//value[ancestor::name[1]/@time='555555']
谜兔 2024-10-14 19:39:30

您使用的 XPath 正在 element1 上查找等于 555555 的时间属性。但是,您的时间属性位于 name 节点上。

您可以按照 Tomalak 建议的方式进行操作,或者将其更改为:

//element1[../@time='555555']

这是查找一个 父节点 的 element1 节点,其时间属性等于 555555。

The XPath that you are using is looking for a time attribute equal to 555555 on element1. However, your time attributes are on the name nodes.

You could go either the way Tomalak suggested, or change it to:

//element1[../@time='555555']

This is looking for an element1 node with a parent who has a time attribute equal to 555555.

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