how to find xml element by attribute value using ElementTree [duplicate]





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







1
















This question already has an answer here:




  • Parse xml from file using etree works when reading string, but not a file

    2 answers




Attempting to parse an XML with namespaces and attributes. I wish to retrieve an element by a given attribute (id).



Documentation specifies but




[@attrib] Selects all elements that have the given attribute.



[@attrib='value'] Selects all elements for which the given attribute has the given value. The value cannot contain quotes.




neither solutions below returns an element when given an attribute name & value or when given an attribute name only



from lxml import etree
tree = etree.parse("es.xml")
root = tree.getroot()

print root.findall('file/body/trans-unit', root.nsmap)


# Unable to fetch all with attribute == id
print root.findall('file/body/trans-unit[@id]', root.nsmap)

# Unable to fetch element with id = 111
print root.find('file/body/trans-unit[@id="111"]', root.nsmap)


XML



<?xml version="1.0" encoding="UTF-8"?>
<xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
<file original="PDDDD" source-language="en" datatype="plaintext" target-language="es">
<header>
<tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
</header>
<body>
<trans-unit id="111">
<source>Welcome!</source>
<target>Welcome!</target>
<note>Class = "UILabel"; text = "Welcome!"; ObjectID = "7oz-Od-KDt";</note>
</trans-unit>
</body>
</file>
</xliff>









share|improve this question













marked as duplicate by Michael Kay xml
Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 24 '18 at 9:46


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

























    1
















    This question already has an answer here:




    • Parse xml from file using etree works when reading string, but not a file

      2 answers




    Attempting to parse an XML with namespaces and attributes. I wish to retrieve an element by a given attribute (id).



    Documentation specifies but




    [@attrib] Selects all elements that have the given attribute.



    [@attrib='value'] Selects all elements for which the given attribute has the given value. The value cannot contain quotes.




    neither solutions below returns an element when given an attribute name & value or when given an attribute name only



    from lxml import etree
    tree = etree.parse("es.xml")
    root = tree.getroot()

    print root.findall('file/body/trans-unit', root.nsmap)


    # Unable to fetch all with attribute == id
    print root.findall('file/body/trans-unit[@id]', root.nsmap)

    # Unable to fetch element with id = 111
    print root.find('file/body/trans-unit[@id="111"]', root.nsmap)


    XML



    <?xml version="1.0" encoding="UTF-8"?>
    <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
    <file original="PDDDD" source-language="en" datatype="plaintext" target-language="es">
    <header>
    <tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
    </header>
    <body>
    <trans-unit id="111">
    <source>Welcome!</source>
    <target>Welcome!</target>
    <note>Class = "UILabel"; text = "Welcome!"; ObjectID = "7oz-Od-KDt";</note>
    </trans-unit>
    </body>
    </file>
    </xliff>









    share|improve this question













    marked as duplicate by Michael Kay xml
    Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

    StackExchange.ready(function() {
    if (StackExchange.options.isMobile) return;

    $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
    var $hover = $(this).addClass('hover-bound'),
    $msg = $hover.siblings('.dupe-hammer-message');

    $hover.hover(
    function() {
    $hover.showInfoMessage('', {
    messageElement: $msg.clone().show(),
    transient: false,
    position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
    dismissable: false,
    relativeToBody: true
    });
    },
    function() {
    StackExchange.helpers.removeMessages();
    }
    );
    });
    });
    Nov 24 '18 at 9:46


    This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.





















      1












      1








      1









      This question already has an answer here:




      • Parse xml from file using etree works when reading string, but not a file

        2 answers




      Attempting to parse an XML with namespaces and attributes. I wish to retrieve an element by a given attribute (id).



      Documentation specifies but




      [@attrib] Selects all elements that have the given attribute.



      [@attrib='value'] Selects all elements for which the given attribute has the given value. The value cannot contain quotes.




      neither solutions below returns an element when given an attribute name & value or when given an attribute name only



      from lxml import etree
      tree = etree.parse("es.xml")
      root = tree.getroot()

      print root.findall('file/body/trans-unit', root.nsmap)


      # Unable to fetch all with attribute == id
      print root.findall('file/body/trans-unit[@id]', root.nsmap)

      # Unable to fetch element with id = 111
      print root.find('file/body/trans-unit[@id="111"]', root.nsmap)


      XML



      <?xml version="1.0" encoding="UTF-8"?>
      <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
      <file original="PDDDD" source-language="en" datatype="plaintext" target-language="es">
      <header>
      <tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
      </header>
      <body>
      <trans-unit id="111">
      <source>Welcome!</source>
      <target>Welcome!</target>
      <note>Class = "UILabel"; text = "Welcome!"; ObjectID = "7oz-Od-KDt";</note>
      </trans-unit>
      </body>
      </file>
      </xliff>









      share|improve this question















      This question already has an answer here:




      • Parse xml from file using etree works when reading string, but not a file

        2 answers




      Attempting to parse an XML with namespaces and attributes. I wish to retrieve an element by a given attribute (id).



      Documentation specifies but




      [@attrib] Selects all elements that have the given attribute.



      [@attrib='value'] Selects all elements for which the given attribute has the given value. The value cannot contain quotes.




      neither solutions below returns an element when given an attribute name & value or when given an attribute name only



      from lxml import etree
      tree = etree.parse("es.xml")
      root = tree.getroot()

      print root.findall('file/body/trans-unit', root.nsmap)


      # Unable to fetch all with attribute == id
      print root.findall('file/body/trans-unit[@id]', root.nsmap)

      # Unable to fetch element with id = 111
      print root.find('file/body/trans-unit[@id="111"]', root.nsmap)


      XML



      <?xml version="1.0" encoding="UTF-8"?>
      <xliff xmlns="urn:oasis:names:tc:xliff:document:1.2">
      <file original="PDDDD" source-language="en" datatype="plaintext" target-language="es">
      <header>
      <tool tool-id="com.apple.dt.xcode" tool-name="Xcode" tool-version="10.1" build-num="10B61"/>
      </header>
      <body>
      <trans-unit id="111">
      <source>Welcome!</source>
      <target>Welcome!</target>
      <note>Class = "UILabel"; text = "Welcome!"; ObjectID = "7oz-Od-KDt";</note>
      </trans-unit>
      </body>
      </file>
      </xliff>




      This question already has an answer here:




      • Parse xml from file using etree works when reading string, but not a file

        2 answers








      python xml xliff






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 24 '18 at 3:24









      kyekye

      98521833




      98521833




      marked as duplicate by Michael Kay xml
      Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 24 '18 at 9:46


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









      marked as duplicate by Michael Kay xml
      Users with the  xml badge can single-handedly close xml questions as duplicates and reopen them as needed.

      StackExchange.ready(function() {
      if (StackExchange.options.isMobile) return;

      $('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
      var $hover = $(this).addClass('hover-bound'),
      $msg = $hover.siblings('.dupe-hammer-message');

      $hover.hover(
      function() {
      $hover.showInfoMessage('', {
      messageElement: $msg.clone().show(),
      transient: false,
      position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
      dismissable: false,
      relativeToBody: true
      });
      },
      function() {
      StackExchange.helpers.removeMessages();
      }
      );
      });
      });
      Nov 24 '18 at 9:46


      This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


























          1 Answer
          1






          active

          oldest

          votes


















          0














          I don't know how to do it with the {None: "urn:oasis:names:tc:xliff:document:1.2"} namespace of root.nsmap...



          However, if you redefine a namespace map like



          ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}


          then this works



          root.find('.//n:trans-unit[@id="111"]', ns)





          share|improve this answer






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            0














            I don't know how to do it with the {None: "urn:oasis:names:tc:xliff:document:1.2"} namespace of root.nsmap...



            However, if you redefine a namespace map like



            ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}


            then this works



            root.find('.//n:trans-unit[@id="111"]', ns)





            share|improve this answer




























              0














              I don't know how to do it with the {None: "urn:oasis:names:tc:xliff:document:1.2"} namespace of root.nsmap...



              However, if you redefine a namespace map like



              ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}


              then this works



              root.find('.//n:trans-unit[@id="111"]', ns)





              share|improve this answer


























                0












                0








                0







                I don't know how to do it with the {None: "urn:oasis:names:tc:xliff:document:1.2"} namespace of root.nsmap...



                However, if you redefine a namespace map like



                ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}


                then this works



                root.find('.//n:trans-unit[@id="111"]', ns)





                share|improve this answer













                I don't know how to do it with the {None: "urn:oasis:names:tc:xliff:document:1.2"} namespace of root.nsmap...



                However, if you redefine a namespace map like



                ns = {"n": "urn:oasis:names:tc:xliff:document:1.2"}


                then this works



                root.find('.//n:trans-unit[@id="111"]', ns)






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 9:17









                SilmathoronSilmathoron

                1,0861921




                1,0861921

















                    這個網誌中的熱門文章

                    Tangent Lines Diagram Along Smooth Curve

                    Yusuf al-Mu'taman ibn Hud

                    Zucchini