使用XML :: libxml获取XML元素
我需要根据 type 元素以不同的方式处理XML文件。 我使用findnodes($ xpath,$ contextNode)
方法来查找是一个结构的一部分的元素。 元素可以具有两种类型< type>/type>
and < type> b</type type>
。 有什么办法,当我用类型B处理节点以“跳”到元素的元素以获取ID值?
基本上我需要能够发现 具有ID 2和类型B的元素与ID 1和类型A的元素处于相同的结构。 有什么想法如何向上跳入结构?
<structure>
<element>
<def>
<id>1</id>
<type>A</type>
</def>
</element>
<element>
<def>
<id>2</id>
<type>B</type>
</def>
</element>
</structure>
I need to process xml-file in a different way depending on type element.
I use findnodes($xpath,$contextNode)
method to find elements that are part of one structure.
Elements can have two types <type>A</type>
and <type>B</type>
.
Is there any way, when I process node with type B to "jump" to element with type A in order to get ID-value?
Basically I need to be able to find out that
element with id 2 and type B is in the same structure as element with id 1 and type A.
Any ideas how can I jump in structure upwards?
<structure>
<element>
<def>
<id>1</id>
<type>A</type>
</def>
</element>
<element>
<def>
<id>2</id>
<type>B</type>
</def>
</element>
</structure>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用
parent
轴。demo 在xpather.com [1] xpath上,
xpath提供了方便的
..
快捷方式。demo 在xpather.com [1]
另外,您可以使用该组合的组合
先前的兄弟姐妹
和swert-sibling
轴。demo 在xpather.com [1]
xpath,
$ node - &gt; parentnode
将获得父节点。该答案的重点是XPATH,因为OP提到的findnodes
,并且由于它更简单。You can use the
parent
axis.Demo on xpather.com[1]
XPath provides the convenient
..
shortcut.Demo on xpather.com[1]
Alternatively, you can use a combination of the
preceding-sibling
andfollowing-sibling
axes.Demo on xpather.com[1]
Outside of XPath,
$node->parentNode
will get the parent node. This answer focused on XPath since the OP mentionedfindnodes
, and because it's terser and simpler.