Skip to content

How to use XPATH / conditions in wrapped() #252

@mehmax

Description

@mehmax

Firstly thanks for this amazing package!

I am trying to use an XPATH expression in my wrapped field definition, but it seems like it is not applied correctly.

Below is an example where I want to map the product_attribute/value element to the name attribute in my Class only if the product_attribute/type is name

When using lxml this can be achieved via the XPATH expression product_attribute/[type='name']/value. When I try the same expression in the wrapped function, it does not work.

Now my questions:

  • Are XPATHs even supported?
  • How could I apply a condition in an other way?

from pydantic_xml import BaseXmlModel, wrapped
from lxml import etree


if __name__ == "__main__":

    xml_str = """
    <product>
        <product_attribute>
            <type>name</type>
            <value>Product Name</value>
        </product_attribute>
        <product_attribute>
            <type>price</type>
            <value>1</value>
        </product_attribute>
    </product>

    """

    # using lxml & XPATH
    tree = etree.fromstring(xml_str)
    print("Product Name: ", tree.findtext("product_attribute/[type='name']/value"))


    # using pydantic-yml without XPATH
    class Product(BaseXmlModel, search_mode="ordered", tag="product"):
        name: str = wrapped("product_attribute/value")

    print("Product Name without condition: ", Product.from_xml(xml_str).name)

    # using pydantic-yml with condition
    class Product(BaseXmlModel, search_mode="ordered", tag="product"):
        name: str = wrapped("product_attribute/[type='name']/value")

    print("Product Name with condition: ", Product.from_xml(xml_str).name)


Many thanks in advance!

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions