Skip to content
Advertisement

Convert ReadyAPI xpath for use in Python 3

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.

Advertisement

Answer

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']
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement