Morkkis
Morkkis

Reputation: 495

XML Library Get Element Attributes not returning attribute values

I'm relatively new to XML and I'm attempting to create automated test cases for checking whether a received XML message contains correct data in correct structure. The XML Message consists of multiple schema instances and certain tags are common throughout these instances. In some cases I don't know which namespace I should check before running, preventing me from simply using the index to find the element. I would need to get the namespace of the current instance I'm reading to be able to determine if this is the element I need.

The XML Content is provided as a Telnet message, I'm simply reading the output and if something was received attempting to parse it with the XML Library. The output is expected to match XML Schema (validated elsewhere) so I shouldn't have to worry about formatting.

The XML looks something like this

<?xml version="1.0" encoding="iso-8859-1"?>
<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceLocation="MessageA1.xsd">
    <version>1.0</version>
    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MessageInfo">
        <itemID>111111</itemID>
        <stuff>content</stuff>
    </items>
    <items xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="MessageContent">
        <itemID>121212</itemID>
        <stuff>different content</stuff>
    </items>
</Message>

I have tried using the Robot Framework XML library with the default ElementTree as well as lxml but (not surprisingly) both provide similar results. The test script looks like this

*** Settings ***
Library    XML    use_lxml=True
Library    String
Library    Collections
Library    Telnet

*** Test Cases ***
Get Element Namespace
    ${MESSAGE}=    Read
    ${XML}=    Parse XML    ${MESSAGE}
    ${ELEMENTS}=    Get Elements    ${XML}    //items
    FOR    ${ELEMENT}    IN    @{ELEMENTS}
        @{ATTRIBUTES}=    Get Element Attributes    ${ELEMENT}
    END

The attributes list then returns only following

@{ATTRIBUTES} = [ {http://www.w3.org/2001/XMLSchema-instance}type ]

I'm aware that the attributes are only available usually for the element that declares them but I'm not fully able to grasp why I'm not getting the value for type at all.

Reading the XML Library Documentation the value returned should be a Python dictionary, but that is not the case here - Is there something wrong with the XML formatting or am I misunderstanding something?

This issue was encountered when using Python 3.8.7 and Robot Framework 4.0.1.

Upvotes: 0

Views: 618

Answers (2)

Morkkis
Morkkis

Reputation: 495

So it seems that I have stumbled on the most basic of basic mistakes with Robot. It turns out that calling a dictionary as a list will only provide you with the list of keys you requested.

By changing the line

@{ATTRIBUTES}=    Get Element Attributes    ${ELEMENT}

to

${ATTRIBUTES}=    Get Element Attributes    ${ELEMENT}

The issue is fixed.

Upvotes: 0

Claudio Batista
Claudio Batista

Reputation: 341

This works:

    *** Settings ***
Library    XML    use_lxml=True
Library    String
Library    Collections

*** Test Cases ***
Get Element Namespace
    ${XML}=    Parse XML    xml_example.xml
    ${element}    Get Element Text    ${XML}    //items[1]/itemID
    Log    ${element}

Just dont forget that when you are getting all the elements and running the FOR, the first element will be the "version", so there won't an identical value for you to read.

Upvotes: 0

Related Questions