【发布时间】:2017-05-03 11:04:41
【问题描述】:
我有一个 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 获取特定标签的最佳方法是什么,以便可以更改值?
【问题讨论】:
-
XML 输入格式不正确,因为未声明
gmd和gco前缀。 -
xml就是这样。 (虽然我只发布了 xml 文档的一部分)。
-
我已经多次评论 XML 问题,任何带有命名空间(以冒号分隔的前缀)的 XML,始终包含根标记或定义命名空间 URI 的任何位置(搜索
xmlns=)。请更新示例帖子。 -
@MapMan:您不能将 XML 文档的一部分简单地声称“xml 就是这样”。您发布的不是 XML。 lxml 会窒息它。命名空间和它们的前缀可能看起来像一个小细节,但它们的存在是有原因的,不管你喜欢与否。
-
我同意你的观点,但不确定发布 400 行 xml 文档是否是个好主意,因此我只发布了相关位。