1

I'm having trouble converting xpath expressions that I use in ReadyAPI for use in Python 3 with the lxml library. I've read the lxml documentation but I'm not getting the same results.

Here's my XML:

<Envelope>
    <Body>
        <ReplyResponses>
            <RepyResults>
                <Reply>
                    <ContentsofReply>
                        <Content>
                            <IDofContent>ID of Content 1</IDofContent>
                        </Content>
                    </ContentsofReply>
                    <ID>ID of Reply 1</ID>
                    <Name>Name of Reply</Name>
                </Reply>
            </RepyResults>
        </ReplyResponses>
    </Body>
</Envelope>

I use the following xpath expressions in ReadyAPI:

//*:Reply[*:Name="Name of Reply"]/*:ID

the expected returned result is:

ID of Reply 1

and:

//*:Reply[*:Name="Name of Reply"]/*:ContentsofReply/*:Content/*:IDofContent

the expected returned result is:

ID of Content 1

How do I get the same results in Python 3 with the lxml library? Thanks in advance.

Genaro.exe
  • 48
  • 8
FWAF
  • 25
  • 7

1 Answers1

1

The xpath expressions in your questions contain *:prefixes intended to be used with namespaces which aren't in your sample xml.

To extract the same data from your sample xml with lxml, change your expressions to

//Reply[Name="Name of Reply"]/ContentsofReply/Content/IDofContent/text()

to get

['ID of Content 1']

and to

//Reply[Name="Name of Reply"]/ID/text()

to get

['ID of Reply 1']
Jack Fleeting
  • 24,385
  • 6
  • 23
  • 45
  • thank you, and in the case that I've a XML that does use namespaces, do I need to use the * wildcard in python? – FWAF Jan 29 '21 at 03:27
  • 1
    @FWAF That would depend on the library you use (lxml or elementtree); but depending on the complexity of the xml, you may have to get into declaring namespaces (not fun...) – Jack Fleeting Jan 29 '21 at 03:40