2016-05-01 26 views
3

我有三個XML文件(下面的示例)。我已經使用它們各自的audioId屬性值命名了這些文件。因此,有問題的文件將被稱爲93.xml2137.xml解析來自一個文件的特定XML屬性並將其附加到另一個iff另一個屬性出現在第二個文件中

93.xml:

<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="93" /> 

2173.xml:

<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="2137" /> 

mainDataSet.xml:

<word id="2137" title="over" level="1" grouping="Sight Words" YRule="0" MagicE="0" SoftC="0" doublevowel="0" longvowel="0" displayorder="101" silentletters="0"/> 

文件mainDataSet.xml包含〜3,000條目。爲了這個問題,我只提供了一個條目。

我的問題是關於如何我就從mainDataSet.xmlmainDataSet.xml追加title屬性爲word標籤中2173.xml如果在這兩個文件的id比賽(或者即使在mainDataSet.xmlid相匹配的文件)。例如,我所提供的樣品中,輸出應該是:

<word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="2137" title="over" /> 

mainDataSet.xml解析我的XML,我正在做的:

e = xml.etree.ElementTree.parse('mainDataSet.xml').getroot() 
for atype in e.findall('word'): 
    print(atype.get('title')) 

回答

2

添加屬性,使用.attrib字典。下面是循環通過mainDataSet.xml內的word元件的樣本代碼,檢索id屬性值,解析相應的XML文件(93.xml2173.xml在這種情況下),更新word元件和轉儲樹迴文件:

import xml.etree.ElementTree as ET 


e = ET.parse('mainDataSet.xml').getroot() 
for word in e.findall('word'): 
    word_id = word.attrib.get("id") 
    if word_id: 
     filename = "%s.xml" % word_id 
     e_word = ET.parse(filename) 
     e_word.getroot().attrib['title'] = word.attrib.get('title') 
     e_word.write(filename) 

樣品mainDataSet.xml,我用:

<words> 
    <word id="2137" title="over" level="1" grouping="Sight Words" YRule="0" MagicE="0" SoftC="0" doublevowel="0" longvowel="0" displayorder="101" silentletters="0"/> 
    <word id="93" title="something else" level="1" grouping="Sight Words" YRule="0" MagicE="0" SoftC="0" doublevowel="0" longvowel="0" displayorder="101" silentletters="0"/> 
</words> 

以下是我在運行腳本後得到:

  • 93.xml

    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="93" title="something else" /> 
    
  • 2173.xml

    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" Use="P,L" audioId="2137" title="over" /> 
    
+0

完美。丹科!我會通過這個,讓你知道它是怎麼回事! – thel3l

2

對於OP或未來的讀者,考慮一個XSLT 1.0溶液,其Python可以運行使用lxml模塊。作爲信息,XSLT是專門用於操縱XML文件的特殊用途語言(其腳本是格式良好的XML文件)。該腳本可移植到其他通用語言(Java,PHP,C#),XSLT處理器(Saxon,Xalan),甚至是命令行解釋器(Bash,PowerShell)。具體來說,對於這個問題,XSLT維護document()函數,該函數可以訪問外部xml文件中的節點,以用於比較需要,如ID。

輸入(添加根標記)

mainDataSet.xml

<root> 
    <word id="2137" title="over" level="1" grouping="Sight Words" YRule="0" 
     MagicE="0" SoftC="0" doublevowel="0" longvowel="0" 
     displayorder="101" silentletters="0"/> 
</root> 

2137.xml

<root> 
    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" 
      Stage="0" Use="P,L" audioId="2137" /> 
</root> 

93.xml

<root> 
    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" 
     Stage="0" Use="P,L" audioId="93" /> 
</root> 

XSLT腳本(將外部保存爲.xsl;讀入.py;假設所有的XML文件在同一目錄下)

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" > 
    <xsl:output method="xml" indent="yes" encoding="UTF-8" /> 

    <xsl:template match="root"> 
    <xsl:copy> 
     <xsl:apply-templates select="word"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match="word">  
    <xsl:copy> 
     <xsl:copy-of select="@*"/> 
     <xsl:if test="@audioId = document('mainDataSet.xml')/root/word/@id"> 
     <xsl:attribute name="title"> 
      <xsl:value-of select="document('mainDataSet.xml')/root/word/@title"/> 
     </xsl:attribute> 
     </xsl:if> 
    </xsl:copy> 
    </xsl:template> 

</xsl:stylesheet> 

的Python腳本

import lxml.etree as ET 

# LOAD XML AND XSL 
xslt = ET.parse('XSLTScript.xsl') 
for i in ['2137', '93']: 
    dom = ET.parse('{}.xml'.format(i)) 

    # TRANSFORM XML 
    transform = ET.XSLT(xslt) 
    newdom = transform(dom) 

    # PRETTY PRINT OUTPUT 
    tree_out = ET.tostring(newdom, encoding='UTF-8', pretty_print=True) 
    print(tree_out.decode("utf-8")) 

    # SAVE TO FILE 
    xmlfile = open('{}.xml'.format(i),'wb') 
    xmlfile.write(tree_out) 
    xmlfile.close() 

輸出(使用發佈的數據)

2173.xml

<root> 
    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" 
     Use="P,L" audioId="2137" title="over"/> 
</root> 

93.xml

<root> 
    <word BloomsTaxonomy="1,2,3" DictationGroupid="i-e combination List 7" Stage="0" 
     Use="P,L" audioId="93"/> 
</root> 
+0

Purr-fect。這兩個解決方案都有效我會通過他們,讓你知道。丹科! – thel3l

+0

我最終使用了@alecxe的解決方案。這個也是一個不錯的主意!非常感謝! – thel3l

相關問題