引用 XML 中的另一个元素 [英] Referring to another element in XML

查看:50
本文介绍了引用 XML 中的另一个元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为我的应用程序设计 XML 文档结构.

I'm trying to design an XML document structure for my application.

我想存储一个像下面这样的孔列表

I want to store a list of holes like following

<Holes>
 <Hole id='1' dia='0.1' depth='2'/>
 <Hole id='2' dia='0.2' depth='1.67'/>
 <Hole id='3' dia='0.3' depth='0.44'/>
</Holes>

在我的文档的另一部分中,我想通过 ID 引用一个孔.例如

In another part of my document I want to refer to a hole by its id. e.g.

<Drill useHoleWithId='1'/>

当我的代码在上面找到时 <Drill>元素我希望它检索 <Hole> 中 'dia' 和 'depth' 属性的值.id='1' 的元素.

When my code finds above <Drill> element I want it to retrieve the values of 'dia' and 'depth' attributes in the <Hole> element that has id='1'.

当然我可以搜索一个<洞>id 等于 'useHoleWithId' 的值的元素,然后获取属性的值,但我想也许有更好的方法来使用一些 XML 技巧来做到这一点.有吗?

Of course I can search for a <Hole> element with id equal to the value of 'useHoleWithId' and then get the values of the attributes, but I thought maybe there's a better way to do this using some XML trick. Is there?

PS - 虽然我对它们一无所知,但 XPath、XLink、XQuery 或 XPointer 中的任何一个可能会有所帮助.

PS - Though I don't have any idea about them, may be any of XPath, XLink, XQuery or XPointer could help.

推荐答案

XPath 当然是一种方法.查找 ID 为 1 的保持的 Xpath 查询类似于 Holes/Hole[@id="1"]

XPath is certainly one way to do it. An Xpath query to find the hold with id 1 would be something like Holes/Hole[@id="1"]

这篇关于引用 XML 中的另一个元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆