简体   繁体   English

元素树:如何查找具有相同属性值的所有下一个子元素

[英]Element Tree : How to find all the next child elements having same attribute value

I have an XML file with following structure: 我有一个具有以下结构的XML文件:

<?xml version="1.0"?>
<data>
<product>
    <Product_Code>code1</Product_Code>
    <Size>x</Size>
    <Quantity>1<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>c</Size>
    <Quantity>5<Quantity>
</product>
<product>
    <Product_Code>code2</Product_Code>
    <Size>z</Size>
    <Quantity>2<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>a</Size>
    <Quantity>1<Quantity>
</product>
<product>
    <Product_Code>code1</Product_Code>
    <Size>y</Size>
    <Quantirt>1<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>b</Size>
    <Quantity>5<Quantity>
</product>
</data>

There are products in the XML. XML中有产品。 Each product has a code,size and quantoty. 每个产品都有代码,尺寸和数量。 Code can be common. 代码可以是通用的。

I want to select all different size corresponding to each code and quantity corresponding to each size. 我想选择与每个代码相对应的所有不同大小以及与每个大小相对应的数量。 I want to do it on the go. 我想随时随地做。 Eg If iterate through the child elements as and find an element with code = "code1", I'd like to find all other elements in the root withcode = "code1" and their corresponding size and quantity value. 例如,如果遍历子元素as并找到一个代码为“ code1”的元素,我想在根中找到所有其他代码为“ code1”的元素及其相应的大小和数量值。

The actual proble is to generate following XML: 实际的问题是生成以下XML:

<products>
    <product>
        <Product_Code>code1<Product_Code>
        <variants>
            <variant>
                <size>x</size>
                <quantity>1</quantity>
            </variant>
            <variant>
                <size>y</size>
                <quantity>1</quantity>
            </variant>
        </variants>
    </product>
    <product>
        <Product_Code>code2<Product_Code>
        <variants>
            <variant>
                <size>z</size>
                <quantity>2</quantity>
            </variant>
        </variants>
    </product>
    <product>
        <Product_Code>code3<Product_Code>
        <variants>
            <variant>
                <size>a</size>
                <quantity>1</quantity>
            </variant>
            <variant>
                <size>b</size>
                <quantity>5</quantity>
            </variant>
            <variant>
                <size>c</size>
                <quantity>5</quantity>
            </variant>
        </variants>
    </product>
</products>

This is the best I have right now for it being rather late in the evening or morning, depending on how you look at it. 这是我目前最好的,因为它取决于傍晚还是傍晚或早晨。 There will likely need to be some optimization if you are doing this on a large amount of XML, but this should get the job done for now and will work on Python 2.7+. 如果您要对大量XML进行此操作,则可能需要进行一些优化,但这应该可以立即完成,并且可以在Python 2.7+上使用。

This solution uses a dictionary to aggregate the products together and create the variants sub-element. 该解决方案使用字典将产品汇总在一起,并创建变体子元素。 Then it iterates through the dictionary to create the new tree with the product code, and variants as children. 然后,遍历字典以使用产品代码和变体作为子代来创建新树。

import xml.etree.ElementTree as etree

tree = etree.parse('test.xml')
root = tree.getroot()
products = dict()
newroot = etree.Element('products')

for product in root.iterfind("product"):
    product_code = product.find('Product_Code')
    product.remove(product_code)
    product.tag = "variant"
    variants = products.setdefault(product_code.text, 
                                   etree.Element("variants"))
    variants.append(product)

for product in products:
    product_element = etree.Element('product')
    product_code = etree.Element('Product_Code')
    product_code.text = product
    product_element.append(product_code)
    product_element.append(products[product])
    newroot.append(product_element)

result_xml = etree.tostring(newroot)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 元素树:如何查找具有特定值的所有子元素 - Element Tree: How to find all child elements with a specific value 如何使用ElementTree获取XML树中下一个孩子的属性值 - How to get attribute value of next child in XML tree using ElementTree 使用 python 中的元素树删除特定数量的具有相同标签和属性值的 XML 元素 - Remove specific numbers of XML elements having same tags and attribute values using Element tree in python 如何使用 BeautifulSoup 找到具有未知值属性的元素? - How to find an element having an attribute of unknown value using BeautifulSoup? 使用元素树删除 xml 节点的所有内容和子元素 - Delete all contents and child elements of an xml node using element tree 如何在python中找到元素树中的元素数量? - How to find the number of elements in element tree in python? 如何使用 WebDriverWait() 访问在 selenium python 中具有相同 class 名称的下一个元素的值 - How to use the WebDriverWait() to access the value of the next element having the same class names in selenium python 如何创建与父元素同名的多个子子元素 - how to create several child subElements having the same name as parent Element 如何在找到父 div 的同时找到子元素? - How to find child element while having found the parent div? 如何使用 python 查找所有相同类型的元素 - How to find all elements of the same type with python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM