我有一组要合并在一起的XML文件。主XML文档是一个完整的iso19139xml文档,另外两个XML文件可能包含<gmd:descriptiveKeywords>
元素。我需要从片段文件中提取这些<gmd:descriptiveKeywords>
元素中的任何一个并添加到主文件中。有数百个这样的文件集,所以我需要做一些匹配,以确保我合并的数据集是正确的。在
片段XML文件可能如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<ValueSupplyChain xmlns:gmd="http://www.isotc211.org/2005/gmd"
xmlns:gco="http://www.isotc211.org/2005/gco" xmlns:gmx="http://www.isotc211.org/2005/gmx"
xmlns:xlink="http://www.w3.org/1999/xlink" id="MICA_B1v-101"
title="MINERALS4EU-EU MINERALS KNOWLEDGE DATA PLATFORM (EU-MKDP)">
<gmd:descriptiveKeywords>
<gmd:MD_Keywords id="exploration">
<gmd:keyword>
<gco:CharacterString>Exploration</gco:CharacterString>
</gmd:keyword>
<gmd:thesaurusName>
<gmd:CI_Citation>
<gmd:title>
<gco:CharacterString>MICA ontology
(ValueSupplyChainScheme)</gco:CharacterString>
</gmd:title>
<gmd:date gco:nilReason="unknown"/>
<gmd:edition>
<gco:CharacterString>2</gco:CharacterString>
</gmd:edition>
<gmd:identifier>
<gmd:MD_Identifier>
<gmd:code>
<gmx:Anchor
xlink:href="https://w3id.org/mica/ontology/MicaOntology/7418a9ae1cd44847889c2c92408e1e71"
/>
</gmd:code>
</gmd:MD_Identifier>
</gmd:identifier>
</gmd:CI_Citation>
</gmd:thesaurusName>
</gmd:MD_Keywords>
</gmd:descriptiveKeywords>
</ValueSupplyChain>
主XML具有如下结构(使用图像,因为XML可能会变得非常大):
理想情况下,我希望在现有关键字部分下面附加相关的片段部分,并创建一个新的主控文档。在
我的问题是,尽管我似乎能够匹配正确的数据集,并找到相关的部分,但我认为我所做的更改永远不会写入到输出目标文件中。在
我的代码是:
^{pr2}$调试输出如下:
DEBUG:root:T title: BGR BOREHOLE MAP
DEBUG:root:T file has the following number of gmd:descriptiveKeywords sections: 7
DEBUG:root:D title: BGR BOREHOLE MAP
DEBUG:root:T and D titles are the same, we can continue...
DEBUG:root:D file has the following number of gmd:descriptiveKeywords sections: 5
DEBUG:root:Subtotal: 12
DEBUG:root:V title: BGR BOREHOLE MAP
DEBUG:root:T and V titles are the same, we can continue...
DEBUG:root:V file has the following number of gmd:descriptiveKeywords sections: 1
DEBUG:root:Subtotal: 13
DEBUG:root:T title: 3D, 4D AND PREDICTIVE MODELLING OF MAJOR MINERAL BELTS IN EUROPE
DEBUG:root:T file has the following number of gmd:descriptiveKeywords sections: 36
DEBUG:root:D title: 3D, 4D AND PREDICTIVE MODELLING OF MAJOR MINERAL BELTS IN EUROPE
DEBUG:root:T and D titles are the same, we can continue...
DEBUG:root:D file has the following number of gmd:descriptiveKeywords sections: 5
DEBUG:root:Subtotal: 41
从调试信息看来,我成功地添加到gmd:descriptiveKeywords元素,那么列表长度按预期增加,但正如我说的,当我写出XML时,我得到了原始主文件的内容。在
我也尝试过使用ElementTree,但是我遇到了同样的问题;而且输出不支持主目录中使用的名称空间前缀。在
我做错什么了?在
编辑
重现该问题的最小代码如下:
from lxml import etree as et
# Open the master file, which is a well-formed and schema valid ISO 19139 XML record
tree = et.parse('T1_0.xml')
root = tree.getroot()
ns_all = {'gmd': 'http://www.isotc211.org/2005/gmd',
'gco': 'http://www.isotc211.org/2005/gco',
'gmx': 'http://www.isotc211.org/2005/gmx',
'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
'gml': 'http://www.opengis.net/gml',
'xlink': 'http://www.w3.org/1999/xlink',
'geonet': 'http://www.fao.org/geonetwork'}
keywordList = root.findall('gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords', ns_all)
# Just a quick check that everything works as expected
print(len(keywordList)) # Should return 7 for the master file
# Open a well-formed XML file containing content we wish to add to the (or a copy of the) master record
dTree = et.parse('D1_0.xml')
dRoot = dTree.getroot()
DKeywords = dRoot.findall('gmd:descriptiveKeywords', ns_all)
# Just a quick check that everything works as expected
print(len(DKeywords)) # Should return 5 for the D file
# Add the keywords from the second file to the keywords of the master file
keywordList.extend(DKeywords)
# We've added 5 records so the result should be 12
print(len(keywordList)) # I get 12 here
# Write out the new file
tree.write('combinedTD1_0.xml')
# If all worked as expected the new file should have 12
ctree = et.parse('combinedTD1_0.xml')
croot = ctree.getroot()
CKeywords = croot.findall('gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords', ns_all)
print(len(CKeywords)) # I get 7 :(
文件包括:
主文件示例:T1_0.xml
片段文件示例:D1_0.xml
片段文件示例:V1_0.xml
{cda>只向列表添加元素。此操作不会对XML树执行任何操作。在
要将附加的
descriptiveKeywords
节点作为主控文档中的同级节点插入,可以执行以下操作:正如我多次回答的那样,考虑一下XSLT,这是一种专门用来转换XML文件的语言,当需要操作XML文件时,比如合并文档。Python的
lxml
模块可以运行xslt1.0脚本。在具体地说,XSLT维护
document()
函数,您可以通过一个文件名参数将片段节点附加到现有的主节点。另外,XSLT使用Identity Transform复制整个文档,就像使用Muenchian Grouping按不同的关键字对文档进行索引一样。使用这种方法,唯一需要的for
循环是遍历文件。在因为OP没有设置一个可复制的示例,下面是一个使用StackOverflow的前3个用户在python和{a5}标记中的示例演示。主文件从前1位开始。然后,Python脚本迭代以按
<tag1>
追加第2列,然后再追加第3列:掌握XML(排名前1的用户)
排名2的XML(即片段)
^{pr2}$排名3的XML(即片段)
XSLT(另存为.xsl文件,与.xml文件位于同一目录中)
Python(解析所有xml和xsl文件)
输出(每个标签排名前3)
操作XSLT
与OP的实际主文件和片段文件相匹配的相应XSLT可能看起来像这个未经测试的版本。下面假设关键字与发布的fragement在同一布局中(由于图像关闭了
<gmd:descriptiveKeywords>
节点,因此无法判断):相关问题 更多 >
编程相关推荐