2016-05-01 7 views
3

私は3つのXMLファイル(以下のサンプル)を持っています。私はaudioId属性のそれぞれの値を持つファイルに名前を付けました。したがって、問題のファイルは93.xml2137.xmlと呼ばれることになります。特定のXML属性を1つのファイルから解析し、別の属性が2番目のファイルに存在する場合は別の属性に追加します。

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には〜3000件のエントリが含まれています。私はこの質問の目的のためにそれから1つのエントリしか提供していない。

私の質問は、私は両方のファイルでid一致した場合mainDataSet.xmlからmainDataSet.xmlから2173.xmlwordタグにtitle属性を追加する方法を(または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:OPや将来の読者のために

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

パーフェクト。ダンケ!私はこれを通過し、それがどのように行くのかを教えてあげよう! – thel3l

2

、Pythonはlxmlモジュールを使用して実行できるXSLT 1.0のソリューションを検討してください。情報として、XSLTは、XMLファイルを操作するように設計された特別な目的の言語です(そのスクリプトは整形式XMLファイルです)。このスクリプトは、他の汎用言語(Java、PHP、C#)、XSLTプロセッサ(Saxon、Xalan)、コマンドラインインタプリタ(Bash、PowerShell)まで移植可能です。具体的には、この質問のために、XSLTは、IDのような比較ニーズのために外部xmlファイル内のノードにアクセスできるdocument()関数を保持しています。

入力(ルートタグを追加)

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で読み込みます。

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

)すべてのXMLファイルが同じディレクトリにあると仮定し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

関連する問題