Reading XML elements when multiple elements have same name

3,099 views
Skip to first unread message

Malay Jain

unread,
Jan 9, 2014, 8:47:07 AM1/9/14
to robotframe...@googlegroups.com
I am trying to get all theName attributes of Setting elements from APP1 parent element.

XML below

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE documentation SYSTEM "doc.dtd">
<documentation>


<app appname="APP1" appver="125">
<setting name="SETTING 1" multiInstance="true">
<tag><![CDATA[]]></tag>
<description/>
<comment/>
<type/>
</setting>
<setting name="SETTING 2" multiInstance="false">
<tag><![CDATA[CMSTP]]></tag>
<description/>
<comment/>
<type/>
</setting>
<app appname="APP2" appver="232">
<setting name="SETTING 3" multiInstance="false">
<tag><![CDATA[]]></tag>
<description><![CDATA DESC}</description
<defvalue><![CDATA[DV01]]></defvalue>
</setting>
<setting name="SETTING 4" multiInstance="false">
<tag><![CDATA[]]></tag>
<description><![CDATA DESC}</description
<defvalue><![CDATA[DV01]]></defvalue>
</setting>


Problem here is:-

parent elements(App1) are  multiple.
Setting element inside Parent element are also multiple.

How do I resolve this problem,as ROBOT XML library reads only unique elements.


Any help is greatly appreciated.

Thanks,
Malay

Kevin O.

unread,
Jan 9, 2014, 11:42:28 AM1/9/14
to robotframe...@googlegroups.com
    ${settings}=    Get Elements    doc.xml    app[@appname='APP1']/setting[@name]
    ${names}=    Create List
    :FOR    ${setting}    IN    @{settings}
    \    Append To List    ${names}    ${setting.attrib['name']}
    Log List    ${names}

With your fragment, the output is:
List length is 2 and it contains following items:
0: SETTING 1
1: SETTING 2

Mykhailo Poliarush

unread,
Jan 10, 2014, 2:34:54 AM1/10/14
to robotframe...@googlegroups.com
I've done small changes to XML library and rewrote it based on lxml library.
Extra method added:
  • get_elements_texts
  • elements_text_should_be
  • elements_text_should_match
  • elements_text_should_match_regexp
  • elements_should_match
  • elements_should_be_equal
  • elements_count_should_be_equal
  • set_elements_attribute
  • set_elements_tag
  • set_elements_text
You can look how I did my code do similar to get same attributes for all elements.

Pekka Klärck

unread,
Jan 10, 2014, 6:51:48 AM1/10/14
to Mykhailo Poliarush, robotframework-users
2014/1/10 Mykhailo Poliarush <pol...@gmail.com>:
> I've done small changes to XML library and rewrote it based on lxml library.

Have you considered submitting the enhancements back to the original
library? It would be great if the original library could be configured
to use lxml too. The main benefit of ElementTree compared to it is
that lxml needs to be installed separately and that installation is
also somewhat complicated in some environments.

> Extra method added:
>
> get_elements_texts
> elements_text_should_be
> elements_text_should_match
> elements_text_should_match_regexp
> elements_should_match
> elements_should_be_equal
> elements_count_should_be_equal
> set_elements_attribute
> set_elements_tag
> set_elements_text

These seem to be all related to handling multiple elements at once. If
that's a common use case, such keywords could obviously be added to
the original library too.

> https://github.com/polusok/robotframework-lxmllibrary

I looked at the project and noticed you had copied some source files
from Robot Framework and removed copyrights at least from some of
them. The former is fine but the latter is one of the few things that
are forbitten by the Apache 2 license
<http://www.apache.org/licenses/LICENSE-2.0.html>. See especially
points b) and c) under section 4. Redistribution:

b) You must cause any modified files to carry prominent notices
stating that You changed the files; and
c) You must retain, in the Source form of any Derivative Works
that You distribute, all copyright, patent, trademark, and attribution
notices from the Source form of the Work, excluding those notices that
do not pertain to any part of the Derivative Works; and

Cheers,
.peke
--
Agile Tester/Developer/Consultant :: http://eliga.fi
Lead Developer of Robot Framework :: http://robotframework.org

Mykhailo Poliarush

unread,
Jan 10, 2014, 8:03:45 AM1/10/14
to robotframe...@googlegroups.com, Mykhailo Poliarush
Have you considered submitting the enhancements back to the original 
library? It would be great if the original library could be configured 
to use lxml too.
No, I just did small hack to make necessary keywords for myself. But to give changes back to xml library, refine it, make it more generic and do code review.

The main benefit of ElementTree compared to it is 
that lxml needs to be installed separately and that installation is 
also somewhat complicated in some environments. 
But main advantage of lxml it supports xpath 1.0 fully which I need for my testing purposes.


These seem to be all related to handling multiple elements at once. If 
that's a common use case, such keywords could obviously be added to 
the original library too. 
Yes, all keywords just handling multiple elements at once. 
There is such use case for me. I'm not sure about others.

copyrights 
Yes, i just copied existed xml library and cleaned up code to leave only minimal code necessary to make workable version.
Anyway, you're right. I will add copyrights right away.

Mykhailo Poliarush

Malay Jain

unread,
Jan 10, 2014, 9:34:31 PM1/10/14
to robotframe...@googlegroups.com
thanks for the help Kevin.
I was close to find an answer, but was struggling to get the right xpath. can you please suggest a good tutorial point for xpath.
one more thing,when you used setting.attrib in the for loop, are you using python syntax.

I always get the answer on this forum. but it's much more important to understand things minutely. Pardon me, if my questions sound silly:-)

Pekka Klärck

unread,
Jan 11, 2014, 3:19:24 AM1/11/14
to mal...@gmail.com, robotframework-users
2014/1/11 Malay Jain <mal...@gmail.com>:
> I was close to find an answer, but was struggling to get the right xpath. can you please suggest a good tutorial point for xpath.
> one more thing,when you used setting.attrib in the for loop, are you using python syntax.

I'm sure you can find good general xpath resources with a little web
search. Notice that, as commented by Mykhailo, XML library doesn't
support the full xpath syntax, only the subset supported by Python's
ElementTree module. The ElementTree version that is part of Python 2.7
also has better xpath support than earlier.

To learn more about xpath supported by XML library on different Python
versions, just read the Introduction section of the library
documentation. That's also the place where to find what attributes the
returned elements contain. To understand how accessing the attributes
works in general, take a look at Extended variable syntax section in
Robot Framework User Guide.

Kevin O.

unread,
Jan 11, 2014, 4:47:16 AM1/11/14
to robotframe...@googlegroups.com
I am glad you found my answer of some use.
I have found the Microsoft XPath examples page to be of great use, but I do not have a better link than that for you.
There are probably better resources, but I do not have one to provide you with.
In the world of web app testing, FireBug + Firebug comes in handy because it allows you to test XPaths and see the result.
The setting.attrib reference is not python syntax per se, but like Pekka mentioned, the extended variable syntax. Such syntax is relevant when referencing a Python object, Java object or a CLR(.Net) object, so it is not specific to Python.
Reply all
Reply to author
Forward
0 new messages