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;
}
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>
python xml xliff
marked as duplicate by Michael Kay
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.
add a comment |
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>
python xml xliff
marked as duplicate by Michael Kay
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.
add a comment |
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>
python xml xliff
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
python xml xliff
asked Nov 24 '18 at 3:24
kyekye
98521833
98521833
marked as duplicate by Michael Kay
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
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.
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
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)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
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)
add a comment |
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)
add a comment |
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)
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)
answered Nov 24 '18 at 9:17
SilmathoronSilmathoron
1,0861921
1,0861921
add a comment |
add a comment |