如何使用xpath处理添加元素及其父母 [英] How to handle adding elements and their parents using xpath

查看:114
本文介绍了如何使用xpath处理添加元素及其父母的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好吧,在某些情况下,我需要在给定xpath的情况下将标签添加到其他某个标签.

Ok, I have a case where I need to add a tag to a certain other tag given an xpath.

示例xml:

<?xml version="1.0" encoding="UTF-8"?>
<Assets>
 <asset name="Adham">
  <general>>
   <services>
    <land/>
    <refuel/>
   </services>
  </general>
 </asset>
 <asset name="Test">
  <general>
   <Something/>
  </general>
 </asset>
</Assets>

我想为两个资产添加一个<missions>标签.但是,第二项资产缺少我要添加的父<services>标记.每个资产标签都存储在一个变量中(例如node1,node2).

I want to add a <missions> tag to both assets. However, the second asset is missing the parent <services> tag, which I want to add. Each asset tag is stored in a variable (say node1, node2).

我有以下xpath:xpath1 = services/missions,由于我的程序的工作方式,我不能简单地以其他形式存储(即我没有地方只存储services)

I have the following xpath: xpath1 = services/missions, which, due to the way my program works, I cannot simply store in a different form (i.e. i don't have a place to store just services)

我需要检查Missions标签是否已经存在,如果存在,则什么也不做.如果标签不存在,则需要创建它.如果它的父级不存在,我也需要创建它.

I need to check and see if the missions tag already exists, and, if so, do nothing. If the tag does not exist, I need to create it. If its parent does not exist, I need to create that too.

如何仅通过使用xpath字符串来做到这一点?

How can I do this simply by using the xpath string?

我想将所有这些都基于布尔值:即val = true,然后根据需要创建标记(和父标记).如果为false,则删除标签.

I want to base all this on a boolean value: i.e. val = true, then create tag (and parent) if neccesary. If false, then delete tag.

(我没有其他方式来引用我需要的标签(由于我在功能层上具有层次,可以大规模地自动执行此过程,因此您可以在Python Lxml:添加和删除标签)).

(I have no other way of referring to the tag I need (since I have layers on layers of functions to automate this process on a large scale, you can check out my previous question here Python Lxml: Adding and deleting tags)).

编辑另一个问题:

我没有包含要添加元素的父元素的变量,只有包含<asset>对象的变量.我正在尝试使用xpath和指向`标记的变量来获取想要的节点的父节点.

I don't have a variable containing the parent of the element to add, just a variable containing the <asset> object. I am trying to get the parent of the node I want using the xpath and a variable pointing to the ` tag.

编辑编辑没关系,我将通过缩短xpath指向父级并使用变量名来引用每个项目来解决此问题.

Edit edit edit: Never mind the above, I will fix the issue by shorting the xpath to point to the parent, and using a variable name to refer to each item.

推荐答案

def to_xml(parent, xpath, value):
    """
    parent: lxml.etree.Element
    xpath: string like 'x/y/z', anything more complex is likely to break
    value: anything, if is False - means delete node
    """        
    # find the node to proceed further        
    nodes = parent.xpath(xpath)        
    if nodes:
        node = nodes[0]
    else:
        parts = xpath.split('/')
        p = parent
        for part in parts:
            nodes = p.xpath(part)
            if not nodes:
                n = etree.XML("<%s/>" % part)
                p.append(n)
                p = n
            else:
                p = nodes[0]
        node = p
    # do whatever is specified vy value
    if value is False:
        node.getparent().remove(node)
    else:
        node.text = str(value)

尽管我不确定将add&在1个功能中删除功能是个好主意,但是无论如何,这很可能会如您所愿.

Although I'm not sure that combining add & remove functionality in 1 function is good idea, but anyway this is likely to work as you're expecting.

这篇关于如何使用xpath处理添加元素及其父母的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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