使用带有混合命名空间的 E4X 获取第一个节点?

发布于 2024-10-25 01:56:40 字数 747 浏览 6 评论 0原文

考虑一个带有一些命名空间的 XML 片段:

<meal xmlns="urn:hl7-org:v3">
    <veg id="7" />
    <lunch id="123">
        <veg id="990" />
    </lunch>
    <dinner id="324">
         <veg id="111" />
    </dinner>
 </meal>

使用 JavaScript E4X,如何显式选择第一个 veg 节点的 id 属性?

显式包含所有命名空间也很重要。这就是我使用下面的 ..*:: 语法的原因。我意识到我在这里使用了错误的运算符。

我已经尝试过这个,不幸的是它获取了所有 veg 节点 id 值:

var veg = meal..*::[email protected]()
//currently gets 7990111

How can I get the value of 7?

Consider an XML snippet with a handful of namespaces:

<meal xmlns="urn:hl7-org:v3">
    <veg id="7" />
    <lunch id="123">
        <veg id="990" />
    </lunch>
    <dinner id="324">
         <veg id="111" />
    </dinner>
 </meal>

Using JavaScript E4X, how can you explicitly select the first veg node's id property?

It's important to explicitly include all namespaces as well. This is the reason for my ..*:: syntax below. I realize I'm using the wrong operator here.

I've tried this, which unfortunately gets ALL veg node id values:

var veg = meal..*::[email protected]()
//currently gets 7990111

How can I get the value of 7?

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

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

发布评论

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

评论(3

梦亿 2024-11-01 01:56:40

var veg = 餐..*::veg[0].@id

var veg = meal..*::veg[0].@id.

如痴如狂 2024-11-01 01:56:40
var meal = // xml;
var veg = meal..*::veg.@id[0];
var meal = // xml;
var veg = meal..*::veg.@id[0];
流年已逝 2024-11-01 01:56:40

试试这个:
var veg = 餐..*::veg[position()=1][ email protected]()

veg[position()=1] 告诉 XPath 您希望 veg 节点位于第一个位置。
我刚刚完成你最初的 xpath 表达式。

Try this :
var veg = meal..*::veg[position()=1][email protected]()

veg[position()=1] tells XPath you want the veg node at the first position.
I just complete your initial xpath expression.

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