在具有相同标记的不同位置更改xml值

2024-05-29 05:28:17 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python脚本,它使用lxml来更改特定标记的值。我有以下xml

                    <gmd:CI_Citation>
                    <gmd:date>
                        <gmd:CI_Date>
                            <gmd:date>
                                <gco:Date>**1900-01-01**</gco:Date>
                            </gmd:date>
                            <gmd:dateType>
                                <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="publication">Publication</gmd:CI_DateTypeCode>
                            </gmd:dateType>
                        </gmd:CI_Date>
                    </gmd:date>
                    <gmd:date>
                        <gmd:CI_Date>
                            <gmd:date>
                                <gco:Date>**1900-01-01**</gco:Date>
                            </gmd:date>
                            <gmd:dateType>
                                <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="creation">Creation</gmd:CI_DateTypeCode>
                            </gmd:dateType>
                        </gmd:CI_Date>
                    </gmd:date>
                    <gmd:date>
                        <gmd:CI_Date>
                            <gmd:date>
                                <gco:Date>**1900-01-01**</gco:Date>
                            </gmd:date>
                            <gmd:dateType>
                                <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="revision">Revision</gmd:CI_DateTypeCode>
                            </gmd:dateType>
                        </gmd:CI_Date>
                    </gmd:date>
                </gmd:CI_Citation>

对于每个不同的日期类型(发布、创建和修订),我想将日期更改为特定的日期,但是所有3个日期的标记都是相同的-

//:gmd_citation/:gmd_CI:Citation/:gmd_date/:gmd_CI_Date/:gmd_date/:gco_Date

我使用以下函数来更改值

def updateXMLTag (tag, value):
  xmlValue = root.xpath(tag)
  xmlValue[0].text = str(value)

使用xpath访问特定标记以便更改值的最佳方法是什么?你知道吗


Tags: 标记orgcihttpdateisoxmlstandards
1条回答
网友
1楼 · 发布于 2024-05-29 05:28:17

这是我使用xpath访问特定元素并编辑它们的方法:

# Find the best implementation available on the platform

try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

from lxml import etree

# proper namespaces added to get valid xml
xmlstr = StringIO("""<gmd:CI_Citation xmlns:gmd="http://gmd.example.com" xmlns:gco="http://gco.example.com">
        <gmd:date>
        <gmd:CI_Date>
            <gmd:date>
                <gco:Date>1900-01-01</gco:Date>
            </gmd:date>
            <gmd:dateType>
                <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="publication">Publication</gmd:CI_DateTypeCode>
            </gmd:dateType>
        </gmd:CI_Date>
    </gmd:date>
    <gmd:date>
        <gmd:CI_Date>
            <gmd:date>
                <gco:Date>1900-01-01</gco:Date>
            </gmd:date>
            <gmd:dateType>
                <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="creation">Creation</gmd:CI_DateTypeCode>
            </gmd:dateType>
        </gmd:CI_Date>
    </gmd:date>
    <gmd:date>
        <gmd:CI_Date>
            <gmd:date>
                <gco:Date>1900-01-01</gco:Date>
            </gmd:date>
        <gmd:dateType>
            <gmd:CI_DateTypeCode codeList="http://standards.iso.org/ittf/PubliclyAvailableStandards/ISO_19139_Schemas/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode" codeListValue="revision">Revision</gmd:CI_DateTypeCode>
            </gmd:dateType>
        </gmd:CI_Date>
    </gmd:date>
</gmd:CI_Citation>""")

tree = etree.parse(xmlstr)

这里我们使用xpath来获取所有(3)个目标元素。你知道吗

targets = tree.xpath('/gmd:CI_Citation/gmd:date/gmd:CI_Date/gmd:dateType/gmd:CI_DateTypeCode', \
           namespaces={'gmd': "http://gmd.example.com", 'gco': "http://gco.example.com"})

这三个元素通过唯一的属性值来区分, 可以用一个简单的函数hasattr来检查

def hasattr(elem, att, val):
    try:
        return elem.attrib[att] == val
    except:
        return False

目标[0]codeListValue/text节点:“publication”/“publication”

目标[1]codeListValue/text节点:“创建”/“创建”

目标[2]codeListValue/text节点:“revision”/“revision”

哪一个需要改变?你知道吗

hasattr(targets[0], 'codeListValue', 'publication')  # True
hasattr(targets[1], 'codeListValue', 'creation')  # True
hasattr(targets[2], 'codeListValue', 'publication')  # False

# Let's change one of them
t1 = targets[1]
t1.text = 'New Creation'  # change text node

# and/or change attribute
t1.attrib['codeListValue'] = 'Latest Creation'

最后,我们将结果保存到一个文件中

tree.write("output1.xml")

编辑1

这里我们导航到cousin1(gco:日期)已找到需要更改的目标[1]的:

t1 = targets[1]
parent1 = t1.getparent()
date1 = parent1.getprevious()
cousin1 = date1.getchildren()
len(cousin1)     #1
cousin1[0].text  #'1900-01-01'

# change the date
cousin1[0].text = '2017-5-3'
# again, write the result

tree.write("out456.xml")

相关问题 更多 >

    热门问题