gpt4 book ai didi

python - 使用 lxml 将 XML 片段插入到 XML 文档中

转载 作者:太空宇宙 更新时间:2023-11-04 02:40:50 26 4
gpt4 key购买 nike

我有一组要合并在一起的 XML 文件。主 XML 文档是完整的 ISO 19139 XML 文档,另外两个 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 可能会变得非常大):

ISO XML document structure

理想情况下,我想将相关片段部分附加到现有关键字部分下方,并创建一个新的主文档。

我的问题是,尽管我似乎能够匹配正确的数据集并找到相关部分,但我认为我所做的更改永远不会写入输出目标文件。

我的代码是:

import logging
import platform
import glob
import os
from lxml import etree as et

logging.getLogger().setLevel(logging.DEBUG)

PC_name = platform.node()
if PC_name == 'blah ':
root_directory = "blah\\blah\\outputs\\"
dir_sep = "\\"
else:
root_directory = "C:\\Temp\\"
dir_sep = "\\"

batch_directory_name = "Batch1"
batch_number = "1"
in_directory = root_directory + batch_directory_name
out_directory_name = "splodge"
out_directory = in_directory + dir_sep + out_directory_name

if not os.path.exists(out_directory):
os.makedirs(out_directory)

os.chdir(in_directory)

fileSuffix = ".xml"
globDirSep = "/"
fileTStem = "T" + batch_number + "_"
fileDStem = "D" + batch_number + "_"
fileVStem = "V" + batch_number + "_"
fileTPattern = fileTStem + "[0-9]*" + fileSuffix
globTPattern = in_directory + globDirSep + fileTPattern
stem = in_directory + dir_sep + fileTStem

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'}

record_title = \
'gmd:identificationInfo/gmd:MD_DataIdentification/gmd:citation/gmd:CI_Citation/gmd:title/gco:CharacterString'
record_keywords = 'gmd:identificationInfo/gmd:MD_DataIdentification/gmd:descriptiveKeywords'

for file in glob.glob(globTPattern):
'Get the record number of the current T file'
fnum = file.replace(stem, "").replace(fileSuffix, "")
tree = et.parse(file)
root = tree.getroot()
recordT = root.find(record_title, ns_all)
'We want to use the UPPER case version to compare with D and V file titles'
RecordTitle = recordT.text.upper()
logging.debug("T title: " + RecordTitle)
dFile = in_directory + dir_sep + fileDStem + fnum + fileSuffix
vFile = in_directory + dir_sep + fileVStem + fnum + fileSuffix
'Find keyword sections in T file (and how many for interest...)'
keywordList = root.findall(record_keywords, ns_all)
knum = len(keywordList)
logging.debug("T file has the following number of gmd:descriptiveKeywords sections: " + str(knum))

try:
dTree = et.parse(dFile)
dRoot = dTree.getroot()
recordDT = dRoot.attrib['title']
logging.debug("D title: " + recordDT)

if RecordTitle == recordDT:
logging.debug("T and D titles are the same, we can continue...")
'If the titles match then we can insert the D keywords fragment'
DKeywords = dRoot.findall('gmd:descriptiveKeywords', ns_all)
dnum = len(DKeywords)
logging.debug("D file has the following number of gmd:descriptiveKeywords sections: " + str(dnum))
keywordList.extend(DKeywords)
logging.debug("Subtotal: " + str(len(keywordList)))
else:
logging.debug("T and D titles don't match")

except:
logging.debug("Cannot parse: " + dFile)

try:
vTree = et.parse(vFile)
vRoot = vTree.getroot()
recordVT = vRoot.attrib['title']
logging.debug("V title: " + recordVT)

if RecordTitle == recordVT:
logging.debug("T and V titles are the same, we can continue...")
'If the titles match then we can insert the V keywords fragment'

VKeywords = vRoot.findall('gmd:descriptiveKeywords', ns_all)
vnum = len(VKeywords)
logging.debug("V file has the following number of gmd:descriptiveKeywords sections: " + str(vnum))
keywordList.extend(VKeywords)
logging.debug("Subtotal: " + str(len(keywordList)))

else:
logging.debug("T and V titles don't match")

except:
logging.debug("Cannot parse: " + vFile)

newFile = "out" + batch_number + "_" + fnum + fileSuffix
writeTo = out_directory_name + dir_sep + newFile
tree.write(writeTo)

调试输出如下:

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

最佳答案

keywordList.extend(DKeywords) 只是将元素添加到列表中。此操作不对 XML 树执行任何操作。

要插入额外的 descriptiveKeywords 节点作为主文档中节点的兄弟节点,您可以执行以下操作:

# Get the last of the descriptiveKeywords nodes in the master document
last_kw = keywordList[-1]
# Get the node's parent and its position (index) within the parent
kw_parent = last_kw.getparent()
ix = kw_parent.index(last_kw)

# Insert the descriptiveKeyword nodes from the fragment file as successive siblings
for dk in DKeywords:
kw_parent.insert(ix+1, dk)
ix += 1

关于python - 使用 lxml 将 XML 片段插入到 XML 文档中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46590986/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com