使用XML :: libxml获取XML元素

发布于 2025-01-26 20:00:00 字数 724 浏览 4 评论 0原文

我需要根据 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 技术交流群。

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

发布评论

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

评论(1

丶情人眼里出诗心の 2025-02-02 20:00:00

您可以使用parent轴。

my ( $elementB ) = $xpc->findnodes(
   "parent::*/element[ def/type='B' ]",
   $elementA,
);

demo 在xpather.com [1] xpath上,


xpath提供了方便的..快捷方式。

my ( $elementB ) = $xpc->findnodes(
   "../element[ def/type='B' ]",
   $elementA,
);

demo 在xpather.com [1]


另外,您可以使用该组合的组合先前的兄弟姐妹swert-sibling轴。

my ( $elementB ) = $xpc->findnodes(
   " preceding-sibling::*[ def/type='B' ]
   | following-sibling::*[ def/type='B' ]
   ",
   $elementA,
);

demo 在xpather.com [1]


xpath,$ node - &gt; parentnode将获得父节点。该答案的重点是XPATH,因为OP提到的findnodes,并且由于它更简单。


  1. 我认为这支持XPATH 2,而Libxml2(XML :: Libxml使用)仅支持XPATH 1,但是这些路径应对两者有效。

You can use the parent axis.

my ( $elementB ) = $xpc->findnodes(
   "parent::*/element[ def/type='B' ]",
   $elementA,
);

Demo on xpather.com[1]


XPath provides the convenient .. shortcut.

my ( $elementB ) = $xpc->findnodes(
   "../element[ def/type='B' ]",
   $elementA,
);

Demo on xpather.com[1]


Alternatively, you can use a combination of the preceding-sibling and following-sibling axes.

my ( $elementB ) = $xpc->findnodes(
   " preceding-sibling::*[ def/type='B' ]
   | following-sibling::*[ def/type='B' ]
   ",
   $elementA,
);

Demo on xpather.com[1]


Outside of XPath, $node->parentNode will get the parent node. This answer focused on XPath since the OP mentioned findnodes, and because it's terser and simpler.


  1. I think this supports XPath 2, while libxml2 (used by XML::LibXML) only supports XPath 1, but these paths should be valid for both.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文