我有一个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访问特定标记以便更改值的最佳方法是什么?你知道吗
这是我使用xpath访问特定元素并编辑它们的方法:
这里我们使用xpath来获取所有(3)个目标元素。你知道吗
这三个元素通过唯一的属性值来区分, 可以用一个简单的函数
hasattr
来检查目标[0]codeListValue/text节点:“publication”/“publication”
目标[1]codeListValue/text节点:“创建”/“创建”
目标[2]codeListValue/text节点:“revision”/“revision”
哪一个需要改变?你知道吗
最后,我们将结果保存到一个文件中
编辑1
这里我们导航到cousin1(gco:日期)已找到需要更改的目标[1]的:
相关问题 更多 >
编程相关推荐