1

I am currently using lxml for XSLT transformation in python. After transformation I have to write the content to different files. For better understanding my code is here as follows:

    ##function to create a new file and write the result into the file 
    def  new_file(a,b):
        full_path = 'path of teh folder to create the file'+a
        file_new = open(full_path, 'w')
        file_new.write(b)


    if xmlRoot.xpath('//slide[@nav_lvl_1="x" and @nav_lvl_2="y"]'):

        ##xpath to get the all the selected nodes of the attributes x and y
        slide_list = xmlRoot.xpath('//slide[@nav_lvl_1="x" and @nav_lvl_2="y"]')

        ##loop against the obtained list of node element
        for each_slide in slide_list:
            xslRoot = etree.parse('path of xsl_file')
            transform = etree.XSLT(xslRoot)
            newdom = transform(xmlRoot)
            file_name =  ('slide'+each_slide.get('page_number')+'.xml')
            result = etree.tostring(newdom, pretty_print=True)
            print etree.tostring(newdom, pretty_print=True)
            new_file(file_name , result)

The following code works absolutely fine but, when I call the function to create the file and write the result into it, it writes the whole result into the file instead of theloop part of the result.

For example: If i have five node elements in slide_list then, the file xslRoot gets transformed five times for different values. And for each transform I have to write the result in five different files. When I run this code I have all five transformed results in all five files that is created, where i need only one result per file.

What is the mistake i have done. NOt able to figure out. any suggestions?

input Xml would be :

- <slide add_info="Merchandising" name="slide8.xml" nav_lvl_1="x" nav_lvl_2="y" page_number="8">
    and ....

- <slide add_info="Merchandising" name="slide8.xml" nav_lvl_1="x" nav_lvl_2="y" page_number="9">
    and ...

- <slide add_info="Merchandising" name="slide8.xml" nav_lvl_1="x" nav_lvl_2="y" page_number="10">
    and ...

- <slide add_info="Merchandising" name="slide8.xml" nav_lvl_1="x" nav_lvl_2="y" page_number="11">

    and so on...
4

1 回答 1

2

我认为问题在于您在循环的每次迭代中将整个文件加载到结果中。对当前迭代的唯一引用是分配文件名时:

file_name =  ('slide'+each_slide.get('page_number')+'.xml')

除了这个 'each_slide' 在任何地方都没有被引用来获得文档的一个子集而不是整个东西。

于 2013-07-24T16:43:36.130 回答