[Python-de] XML in meherer kleine XMLs zerlegen

Walter Dörwald walter at livinglogic.de
Fre Aug 20 14:49:49 CEST 2004


Kavsek Paul Günter wrote:

> Hallo,
> 
> hat jemand ein Beispiel wie man so was macht:
> 
> =================================================
> input document:
> <book>
> <title>this is the title which i need all the time</title>
> <chapter>lets say that this is all the data of chapter1</chapter>
> <chapter>lets say that this is all the data of chapter2</chapter>
> .....
> </book>
>  
> output document1:
> <book>
> <title>this is the title which i need all the time</title>
> <chapter>lets say that this is all the data of chapter1</chapter>
> </book>
>  
> output document2:
> <book>
> <title>this is the title which i need all the time</title>
> <chapter>lets say that this is all the data of chapter2</chapter>
> </book>
>  
> output document(n)
> ....
> =================================================
> 
> danke für die Hilfe

Mit XIST (http://www.livinglogic.de/Python/xist/)
ungefähr folgendermaßen (völlig ungetestet):
---
from ll.xist import xsc, parsers

class xmlns(xsc.Namespace):
    class book(xsc.Element): pass
    class title(xsc.Element): pass
    class chapter(xsc.Element): pass

p = xsc.Prefixes(xmlns)
e = parsers.parseFile("input document", prefixes=p)

t = e.findfirst(xsc.FindTypeTop(xmlns.title))

for (i, c) in enumerate(e.walk(xsc.FindTypeTop(xmlns.chapter))):
    e2 = xmlns.book(t, c)
    e2.write(open("output document%d" % i, "wb"), , prefixes=p)
---

Bis demnächst,
    Walter Dörwald