2016-10-13 86 views
1

我知道有幾個問題已經回答了這個問題,但由於某種原因,我不能讓它在我讀取文件時工作。xml ns0:前綴不能使以前的解決方案工作

這是代碼我有

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文件結構

<?xml version="1.0" encoding="utf-8"?> 
<Takeoff xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffCatalog-10.0.xsd"> 
    <Catalog> 
     <Item Name="Column Location D" WBS="1" Transparency="0.3" Color="-3887691" LineThickness="0.1" CatalogId="f9f30bfc-6c43-4b7b-a335-5c5c093b4165"> 
      <VariableCollection> 
       <Variable Name="Length" Formula="=ModelLength" Units="Meter" /> 
       <Variable Name="Width" Formula="=ModelWidth" Units="Meter" /> 
       <Variable Name="Thickness" Formula="=ModelThickness" Units="Meter" /> 
       <Variable Name="Height" Formula="=ModelHeight" Units="Meter" /> 
       <Variable Name="Perimeter" Formula="=ModelPerimeter" Units="Meter" /> 
       <Variable Name="Area" Formula="=ModelArea" Units="SquareMeter" /> 
       <Variable Name="Volume" Formula="=ModelVolume" Units="CubicMeter" /> 
       <Variable Name="Weight" Formula="=ModelWeight" Units="Kilogram" /> 
       <Variable Name="Count" Formula="=1" /> 
      </VariableCollection> 
     </Item> 
    </Catalog> 
    <ConfigFile> 
     <Workbook xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://download.autodesk.com/us/navisworks/schemas/nw-TakeoffConfiguration-10.0.xsd" xmlns=""> 
      <GlobalConfiguration> 
       <ConfigureFileType>ItemResources</ConfigureFileType> 
       <UnitsSystem>Metric</UnitsSystem> 
       <Currency> 
        <Name></Name> 
        <Code></Code> 
        <Symbol></Symbol> 
       </Currency> 
       <Column Name="Object"> 
        <Type>String</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="any"></Units> 
       </Column> 
       <Column Name="Description1"> 
        <Type>String</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="any"></Units> 
       </Column> 
       <Column Name="Description2"> 
        <Type>String</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="any"></Units> 
       </Column> 
       <Column Name="ModelLength"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="length">meter</Units> 
       </Column> 
       <Column Name="ModelWidth"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="length">meter</Units> 
       </Column> 
       <Column Name="ModelThickness"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="length">meter</Units> 
       </Column> 
       <Column Name="ModelHeight"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="length">meter</Units> 
       </Column> 
       <Column Name="ModelPerimeter"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="length">meter</Units> 
       </Column> 
       <Column Name="ModelArea"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="area">squaremeter</Units> 
       </Column> 
       <Column Name="ModelVolume"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="volume">cubicmeter</Units> 
       </Column> 
       <Column Name="ModelWeight"> 
        <Type>Number</Type> 
        <Purpose>Input</Purpose> 
        <Formula varies="0"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="weight">kilogram</Units> 
       </Column> 
       <Column Name="Length"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelLength</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="length">meter</Units> 
       </Column> 
       <Column Name="Width"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelWidth</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="length">meter</Units> 
       </Column> 
       <Column Name="Thickness"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelThickness</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="length">meter</Units> 
       </Column> 
       <Column Name="Height"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelHeight</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="length">meter</Units> 
       </Column> 
       <Column Name="Perimeter"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelPerimeter</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="length">meter</Units> 
       </Column> 
       <Column Name="Area"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelArea</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="area">squaremeter</Units> 
       </Column> 
       <Column Name="Volume"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelVolume</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="volume">cubicmeter</Units> 
       </Column> 
       <Column Name="Weight"> 
        <Type>Number</Type> 
        <Purpose>Calculation</Purpose> 
        <Formula varies="1">=ModelWeight</Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="weight">kilogram</Units> 
       </Column> 
       <Column Name="Count"> 
        <Type>Number</Type> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="0" group="any">count</Units> 
       </Column> 
       <Column Name="PrimaryQuantity"> 
        <Type>Number</Type> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Value varies="1"></Value> 
        <Units varies="1" group="any"></Units> 
       </Column> 
      </GlobalConfiguration> 
      <Table Name="ObjectResource"> 
       <ColumnRef Name="Object" /> 
       <ColumnRef Name="Description1" /> 
       <ColumnRef Name="Description2" /> 
       <ColumnRef Name="ModelLength" /> 
       <ColumnRef Name="ModelWidth" /> 
       <ColumnRef Name="ModelThickness" /> 
       <ColumnRef Name="ModelHeight" /> 
       <ColumnRef Name="ModelPerimeter" /> 
       <ColumnRef Name="ModelArea" /> 
       <ColumnRef Name="ModelVolume" /> 
       <ColumnRef Name="ModelWeight" /> 
       <ColumnRef Name="Length"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="area"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="volume"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="weight"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Count"> 
        <Purpose>Calculation</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="PrimaryQuantity"> 
        <Purpose>Calculation</Purpose> 
       </ColumnRef> 
      </Table> 
      <Table Name="StepResource"> 
       <ColumnRef Name="Length"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="area"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="volume"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Purpose>RollUp</Purpose> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="weight"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Count" /> 
       <ColumnRef Name="PrimaryQuantity" /> 
      </Table> 
      <Table Name="ObjectStep"> 
       <ColumnRef Name="Object" /> 
       <ColumnRef Name="ModelLength" /> 
       <ColumnRef Name="ModelWidth" /> 
       <ColumnRef Name="ModelThickness" /> 
       <ColumnRef Name="ModelHeight" /> 
       <ColumnRef Name="ModelPerimeter" /> 
       <ColumnRef Name="ModelArea" /> 
       <ColumnRef Name="ModelVolume" /> 
       <ColumnRef Name="ModelWeight" /> 
       <ColumnRef Name="Length"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="area"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="volume"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="weight"></Units> 
       </ColumnRef> 
      </Table> 
      <Table Name="Step"> 
       <ColumnRef Name="Length"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
      </Table> 
      <Table Name="Object"> 
       <ColumnRef Name="Object" /> 
       <ColumnRef Name="Description1" /> 
       <ColumnRef Name="Description2" /> 
       <ColumnRef Name="ModelLength" /> 
       <ColumnRef Name="ModelWidth" /> 
       <ColumnRef Name="ModelThickness" /> 
       <ColumnRef Name="ModelHeight" /> 
       <ColumnRef Name="ModelPerimeter" /> 
       <ColumnRef Name="ModelArea" /> 
       <ColumnRef Name="ModelVolume" /> 
       <ColumnRef Name="ModelWeight" /> 
       <ColumnRef Name="Length"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="length"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="area"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="volume"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Formula varies="1"></Formula> 
        <Units varies="1" group="weight"></Units> 
       </ColumnRef> 
       <ColumnRef Name="Count"> 
        <Purpose>Calculation</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="PrimaryQuantity"> 
        <Purpose>Calculation</Purpose> 
       </ColumnRef> 
      </Table> 
      <Table Name="Item"> 
       <ColumnRef Name="Length"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Count"> 
        <Formula varies="1">=1</Formula> 
       </ColumnRef> 
       <ColumnRef Name="PrimaryQuantity" /> 
      </Table> 
      <Table Name="ItemGroup" /> 
      <Table Name="Resource"> 
       <ColumnRef Name="Length"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Width"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Thickness"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Height"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Perimeter"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Area"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Volume"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Weight"> 
        <Purpose>RollUp</Purpose> 
       </ColumnRef> 
       <ColumnRef Name="Count"> 
        <Formula varies="1">=1</Formula> 
       </ColumnRef> 
       <ColumnRef Name="PrimaryQuantity" /> 
      </Table> 
      <Table Name="ResourceGroup" /> 
     </Workbook> 
    </ConfigFile> 
</Takeoff> 

結果的一部分,到目前爲止

<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> 
+1

您想實現什麼功能? – Jayvee

+0

即時通訊嘗試使用來自另一個XML的元素添加新的「項目」。但在此之前,我需要獲得ns0:開路。 – RicardoZepeda

回答

0

我現在明白這個問題,你的輸出中有不需要的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") 
+0

謝謝你!不知道我必須註冊最後一個xsd。 !爲你而感謝 – RicardoZepeda

相關問題