0
votes

Suppose I have an XML file -

<OperationDate>
            <Type> XYZ </Type>
            <Qualifier> ABCD </Qualifier>
            <Value>
                <Date> 2021-01-22 </Date>
            </Value>
</OperationDate>
<OperationDate>
            <Type> PQR </Type>
            <Qualifier> LMNO </Qualifier>
            <Value>
                <Date> 2021-02-12 </Date>
            </Value>
</OperationDate>
<OperationDate>
            <Type> ABC </Type>
            <Qualifier> QXYZ </Qualifier>
            <Value>
                <Date> 2021-03-02 </Date>
            </Value>
</OperationDate>

Now I want to extract the Value/Date from this snippet conditional to the Type and Qualifier, as both of them together form the identifier element for this Value.

What would be the XPath command for this particular functionality.

PS - Consider this XML file to be an autogenerated file so I can't make changes to it. I just have to extract the Value from it.

I am very new to XPath and only need it for this one functionality, I found ways to put conditions for attribute but I need condition for a child element only.

2

2 Answers

0
votes

Try this:

for operation in response.xpath("//OperationDate//Value"):
     value = operation.xpath("./Date/text()")

This code is for python so I'm not sure how it would be translated in other languages interms of logic.

0
votes

I take your question to mean that you have both the Type and the Qualifier, and you need the Date associated with them.

Overall, what you need to know about is the "predicate" of an xpath, that is, the part in square braces. With this, you can select only nodes meeting specific criteria.

One way to do it would be

//OperationDate[Type="your type" and Qualifier="your qualifier"]/Value/Date/text()

Testing in Python:

In [10]: tree.xpath('//OperationDate[Type=" XYZ " and Qualifier=" ABCD "]/Value/Date/text()')
Out[10]: [' 2021-01-22 ']

Note, however, that this version is sensitive to leading and trailing whitespace in your values.

In [12]: tree.xpath('//OperationDate[Type="XYZ" and Qualifier="ABCD"]/Value/Date/text()')
Out[12]: []

A version that fixes that would be

//OperationDate[normalize-space(Type)="your type" and normalize-space(Qualifier)="your qualifier"]/Value/Date/text()

In [11]: tree.xpath('//OperationDate[normalize-space(Type)="XYZ" and normalize-space(Qualifier)="ABCD"]/Value/Date/text()')
Out[11]: [' 2021-01-22 ']