4 new revisions:

Revision: af5fa5ac6790
Author:   Pekka Klärck
Date:     Thu Jul  5 14:47:05 2012
Log:      XML lib: Reorganized doc
http://code.google.com/p/robotframework/source/detail?r=af5fa5ac6790

Revision: 8a9b2b2f981e
Author:   Pekka Klärck
Date:     Thu Jul  5 14:57:54 2012
Log:      XML lib: test fixing and tuning
http://code.google.com/p/robotframework/source/detail?r=8a9b2b2f981e

Revision: e0dc1845e0b5
Author:   Pekka Klärck
Date:     Fri Jul  6 04:42:41 2012
Log: XML lib: Test for '..' in xpath. Thought initially that it works only ...
http://code.google.com/p/robotframework/source/detail?r=e0dc1845e0b5

Revision: c7a20b7b8fab
Author:   Pekka Klärck
Date:     Fri Jul  6 04:42:54 2012
Log:      XML lib: cleanup
http://code.google.com/p/robotframework/source/detail?r=c7a20b7b8fab

==============================================================================
Revision: af5fa5ac6790
Author:   Pekka Klärck
Date:     Thu Jul  5 14:47:05 2012
Log:      XML lib: Reorganized doc
http://code.google.com/p/robotframework/source/detail?r=af5fa5ac6790

Modified:
 /src/robot/libraries/XML.py

=======================================
--- /src/robot/libraries/XML.py Thu Jul  5 14:00:18 2012
+++ /src/robot/libraries/XML.py Thu Jul  5 14:47:05 2012
@@ -38,22 +38,18 @@
     be used as an input with all other keywords, but these keywords also
     support parsing XML files and strings directly.

-    *Elements attributes*
-
- All keywords returning elements, such as `Parse XML`, and `Get Element`,
-    return ElementTree's
- [Element classes| http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element].
-    These elements can be used as inputs for other keywords, but they also
-    contain several useful attributes that can be accessed directly using
-    the extended variable syntax.
-
-    The attributes that are both useful and convenient to use in the test
-    data are explained below with examples. Also other attributes, and also
-    methods, can be accessed, but that is typically better to do in custom
-    libraries than directly in the test data.
-
-    In the examples `${XML}` and `${HTML}` refer to the following example
-    structures:
+    *Finding elements with xpath*
+
+ ElementTree, and thus also this library, supports finding elements using + xpath expressions. ElementTree does not, however, support the full xpath + syntax, and what is supported depends on its version. ElementTree 1.3 that + is distributed with Python/Jython 2.7 supports richer syntax than versions
+    distributed with earlier Python interpreters.
+
+    Supported xpath syntax is explained below and
+    [ElementTree documentation|http://effbot.org/zone/element-xpath.htm]
+    provides more details. In the examples `${XML}` refers to the following
+    example structure:

     | <example>
     |   <first>text</first>
@@ -65,72 +61,12 @@
     |     <second id="child"/>
     |     <child><grandchild/></child>
     |   </third>
-    | </example>
-
-    | <html>
-    |   <head><title>Example</title></head>
-    |   <body>
+    |   <html>
     |     <p>
-    |       Example text with <b>bold</b> and <i>italics</i>.
+    |       Text with <b>bold</b> and <i>italics</i>.
     |     </p>
-    |   </body>
-    | </html>
-
-    _tag_
-
-    The tag of the element.
-
-    | ${root} =       | Parse XML   | ${XML}  |
-    | Should Be Equal | ${root.tag} | example |
-
-    _text_
-
- The text that the element contains or Python `None` if the element has no
-    text. Notice that the text _does not_ contain texts of possible child
-    elements nor text after/between children. Notice also that in XML
- whitespace is significant, so the text contains also possible indentation
-    and newlines. To get also text of the possible children, optionally
-    whitespace normalized, use `Get Element Text` keyword.
-
-    | ${1st} =        | Get Element | ${XML}    | first  |
-    | Should Be Equal | ${1st.text} | text      |        |
-    | ${2nd} =        | Get Element | ${XML}    | second |
-    | Should Be Equal | ${2nd.text} | ${NONE}   |        |
-    | ${p} =          | Get Element | ${HTML}   | body/p |
- | Should Be Equal | ${p.text} | \n${SPACE*6}Example text with${SPACE} |
-
-    _tail_
-
- The text after the element before the next opening or closing tag. Python - `None` if the element has no tail. Similarly as with `text`, also `tail`
-    contains possible indentation and newlines.
-
-    | ${title} =      | Get Element    | ${HTML}   | head/title  |
-    | Should Be Equal | ${title.tail}  | ${NONE}   |             |
-    | ${b} =          | Get Element    | ${HTML}   | body/p/b    |
-    | Should Be Equal | ${b.tail}      | ${SPACE}and${SPACE}     |
-
-    _attrib_
-
-    A Python dictionary containing attributes of the element.
-
-    | ${1st} =        | Get Element         | ${XML} | first  |
-    | Should Be Empty | ${1st.attrib}       |        |        |
-    | ${2nd} =        | Get Element         | ${XML} | second |
-    | Should Be Equal | ${2nd.attrib['id']} | 2      |        |
-
-    *Finding elements with xpath*
-
- ElementTree, and thus also this library, supports finding elements using - xpath expressions. ElementTree does not, however, support the full xpath - syntax, and what is supported depends on its version. ElementTree 1.3 that - is distributed with Python/Jython 2.7 supports richer syntax than versions
-    distributed with earlier Python interpreters.
-
-    Supported xpath syntax is explained below and
-    [ElementTree documentation|http://effbot.org/zone/element-xpath.htm]
-    provides more details. The examples use same `${XML}` and `${HTML}`
-    structures as earlier examples.
+    |   </html>
+    | </example>

     _Tag names_

@@ -191,6 +127,63 @@

     Predicates can also be stacked like `path[predicate1][predicate2]`.
     A limitation is that possible position predicate must always be first.
+
+    *Elements attributes*
+
+ All keywords returning elements, such as `Parse XML`, and `Get Element`,
+    return ElementTree's
+ [Element classes| http://docs.python.org/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element].
+    These elements can be used as inputs for other keywords, but they also
+    contain several useful attributes that can be accessed directly using
+    the extended variable syntax.
+
+    The attributes that are both useful and convenient to use in the test
+    data are explained below with examples. Also other attributes, and also
+    methods, can be accessed, but that is typically better to do in custom
+    libraries than directly in the test data.
+
+    The examples use same `${XML}` structure as earlier examples.
+
+    _tag_
+
+    The tag of the element.
+
+    | ${root} =       | Parse XML   | ${XML}  |
+    | Should Be Equal | ${root.tag} | example |
+
+    _text_
+
+ The text that the element contains or Python `None` if the element has no
+    text. Notice that the text _does not_ contain texts of possible child
+    elements nor text after/between children. Notice also that in XML
+ whitespace is significant, so the text contains also possible indentation
+    and newlines. To get also text of the possible children, optionally
+    whitespace normalized, use `Get Element Text` keyword.
+
+    | ${1st} =        | Get Element | ${XML}  | first  |
+    | Should Be Equal | ${1st.text} | text    |        |
+    | ${2nd} =        | Get Element | ${XML}  | second |
+    | Should Be Equal | ${2nd.text} | ${NONE} |        |
+    | ${p} =          | Get Element | ${XML}  | html/p |
+    | Should Be Equal | ${p.text}   | \\n${SPACE*6}Text with${SPACE} |
+
+    _tail_
+
+ The text after the element before the next opening or closing tag. Python + `None` if the element has no tail. Similarly as with `text`, also `tail`
+    contains possible indentation and newlines.
+
+    | ${b} =          | Get Element    | ${XML}  | html/p/b  |
+    | Should Be Equal | ${b.tail}      | ${SPACE}and${SPACE} |
+
+    _attrib_
+
+    A Python dictionary containing attributes of the element.
+
+    | ${1st} =        | Get Element         | ${XML} | first  |
+    | Should Be Empty | ${1st.attrib}       |        |        |
+    | ${2nd} =        | Get Element         | ${XML} | second |
+    | Should Be Equal | ${2nd.attrib['id']} | 2      |        |
     """

     _whitespace = re.compile('\s+')
@@ -201,7 +194,7 @@
             return ET.parse(source).getroot()

     def get_element(self, source, xpath='.'):
- if xpath == '.': # ET included in Python < 2.7 does not support '.'.
+        if xpath == '.':  # ET < 1.3 does not support '.' alone.
             return self._get_parent(source)
         elements = self.get_elements(source, xpath)
         if not elements:

==============================================================================
Revision: 8a9b2b2f981e
Author:   Pekka Klärck
Date:     Thu Jul  5 14:57:54 2012
Log:      XML lib: test fixing and tuning
http://code.google.com/p/robotframework/source/detail?r=8a9b2b2f981e

Modified:
 /atest/testdata/standard_libraries/xml/xpath.txt

=======================================
--- /atest/testdata/standard_libraries/xml/xpath.txt Thu Jul 5 05:51:05 2012 +++ /atest/testdata/standard_libraries/xml/xpath.txt Thu Jul 5 14:57:54 2012
@@ -58,7 +58,8 @@

 '[tag]'
     [Documentation]    FAIL No element matching 'child[nonex]' found.
-    *[grandchild]    child
+    another[child]    another
+    */grandchild[ggc]    grandchild
     child[nonex]    not-found

 '[position]'
@@ -69,6 +70,7 @@

 Stacked predicates
     child[@id][@a3='y']    child    ${NONE}
+    child/grandchild[1][ggc]    grandchild    ${NONE}

 Non-ASCII tag names
     täg    täg    xml=<rööt><täg/></rööt>

==============================================================================
Revision: e0dc1845e0b5
Author:   Pekka Klärck
Date:     Fri Jul  6 04:42:41 2012
Log: XML lib: Test for '..' in xpath. Thought initially that it works only with ElementTree, not Elements that we return.
http://code.google.com/p/robotframework/source/detail?r=e0dc1845e0b5

Modified:
 /atest/robot/standard_libraries/xml/xpath.txt
 /atest/testdata/standard_libraries/xml/xpath.txt

=======================================
--- /atest/robot/standard_libraries/xml/xpath.txt       Thu Jul  5 05:03:56 2012
+++ /atest/robot/standard_libraries/xml/xpath.txt       Fri Jul  6 04:42:41 2012
@@ -26,6 +26,10 @@

 '//' returning multiple elements
     Check Test Case    ${TESTNAME}
+
+'..'
+    [Tags]    2.7+
+    Check Test Case    ${TESTNAME}

 '[@attrib]'
     [Tags]    2.7+
=======================================
--- /atest/testdata/standard_libraries/xml/xpath.txt Thu Jul 5 14:57:54 2012 +++ /atest/testdata/standard_libraries/xml/xpath.txt Fri Jul 6 04:42:41 2012
@@ -45,6 +45,12 @@
     \    Should Be Equal    ${elem.tag}    child
     Should Be Equal    ${elem.text}    text

+'..'
+    another/..    test
+    another/child/..    another
+    .//ggc/..    grandchild
+    another/child/../../child[@id="3"]  child
+
 '[@attrib]'
     [Documentation]    FAIL No element matching 'child[@nonex]' found.
     child[@a2]    child    ${NONE}

==============================================================================
Revision: c7a20b7b8fab
Author:   Pekka Klärck
Date:     Fri Jul  6 04:42:54 2012
Log:      XML lib: cleanup
http://code.google.com/p/robotframework/source/detail?r=c7a20b7b8fab

Modified:
 /src/robot/libraries/XML.py

=======================================
--- /src/robot/libraries/XML.py Thu Jul  5 14:47:05 2012
+++ /src/robot/libraries/XML.py Fri Jul  6 04:42:54 2012
@@ -194,8 +194,6 @@
             return ET.parse(source).getroot()

     def get_element(self, source, xpath='.'):
-        if xpath == '.':  # ET < 1.3 does not support '.' alone.
-            return self._get_parent(source)
         elements = self.get_elements(source, xpath)
         if not elements:
             raise RuntimeError("No element matching '%s' found." % xpath)
@@ -204,14 +202,17 @@
                                % (len(elements), xpath))
         return elements[0]

-    def _get_parent(self, source):
+    def get_elements(self, source, xpath):
+        source = self._parse_xml(source)
+        if xpath == '.':  # ET < 1.3 does not support '.' alone.
+            return [source]
+        return source.findall(self._get_xpath(xpath))
+
+    def _parse_xml(self, source):
         if isinstance(source, basestring):
             return self.parse_xml(source)
         return source

-    def get_elements(self, source, xpath):
-        return self._get_parent(source).findall(self._get_xpath(xpath))
-
     if sys.version_info >= (2, 7):
         def _get_xpath(self, xpath):
             return xpath

Reply via email to