xml ns0:前缀不能使前面的解决方案

2024-04-27 17:36:54 发布

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

我知道关于这个主题有几个问题已经得到了回答,但是由于某些原因,当我阅读文件时,我无法使它起作用。在

这是我的密码

import xml.etree.ElementTree as etree
import copy
etree.register_namespace("","http://www.w3.org/2001/XMLSchema")
etree.register_namespace("","http://www.w3.org/2001/XMLSchema-instance")
#Estract the search set thats necesary
tree=etree.ElementTree()
tree.parse('Selection Sets.xml')
root = tree.getroot()
#QTO with one set name and properties as needed, same properties will be applied to the new elements
itree=etree.ElementTree()
itree.parse('New QTO one set.xml')
iroot=itree.getroot()
print (etree.tostring(iroot))
#----Extract the names to be used in the new sets
catcher=[]
temp=tree.findall('selectionsets/selectionset')
for child in temp:
    catcher.append(str(child.get("name")))
# print (catcher)
#----create new elements inside the QTO xml
'''itemp=iroot.findall("Takeoff/Catalog")
for isets in itemp:
    for icatch in catcher:
        col = copy.deepcopy(isets)
        col.set('Name','%s'%(icatch))
        iroot.find('Takeoff/Catalog/Item').append(col)'''
print (catcher[0])
otree = etree.ElementTree(iroot)
otree.write('Set QTO.xml')

这是原始的xml文件结构

^{pr2}$

到目前为止的部分结果

<ns0:Takeoff xmlns="http://www.w3.org/2001/XMLSchema-instance" xmlns:ns0="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd">
        <ns0:Catalog>
            <ns0:Item CatalogId="f9f30bfc-6c43-4b7b-a335-5c5c093b4165" Color="-3887691" LineThickness="0.1" Name="Column Location D" Transparency="0.3" WBS="1">
                <ns0:VariableCollection>
                    <ns0:Variable Formula="=ModelLength" Name="Length" Units="Meter" />
                    <ns0:Variable Formula="=ModelWidth" Name="Width" Units="Meter" />
                    <ns0:Variable Formula="=ModelThickness" Name="Thickness" Units="Meter" />
                    <ns0:Variable Formula="=ModelHeight" Name="Height" Units="Meter" />
                    <ns0:Variable Formula="=ModelPerimeter" Name="Perimeter" Units="Meter" />
                    <ns0:Variable Formula="=ModelArea" Name="Area" Units="SquareMeter" />
                    <ns0:Variable Formula="=ModelVolume" Name="Volume" Units="CubicMeter" />
                    <ns0:Variable Formula="=ModelWeight" Name="Weight" Units="Kilogram" />
                    <ns0:Variable Formula="=1" Name="Count" />
                </ns0:VariableCollection>
            </ns0:Item>
        </ns0:Catalog>
        <ns0:ConfigFile>
            <Workbook noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffConfiguration-10.0.xsd">
</Workbook>
    </ns0:ConfigFile>
</ns0:Takeoff>

Tags: thenametreehttpxmlvariableetreemeter
1条回答
网友
1楼 · 发布于 2024-04-27 17:36:54

我现在明白问题了,输出中有不需要的ns0前缀。为了避免使用这些名称空间,您可能需要使用相应的前缀注册所有名称空间:

etree.register_namespace("xs","http://www.w3.org/2001/XMLSchema")
etree.register_namespace("xsi","http://www.w3.org/2001/XMLSchema-instance")
etree.register_namespace("","http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd")

相关问题 更多 >