Используйте xpath для поиска узла по идентификатору атрибута и тексту - PullRequest
0 голосов
/ 28 мая 2018

Я понимаю, как найти узел по идентификатору атрибута или по тексту, но не знаю, какой синтаксис найти по обоим.Я использую библиотеку xml, у меня нет доступа к lxml, потому что я не могу установить на свой рабочий компьютер.Вот выдержка из XML, с которым я работаю.

<product product-id="000100015BA00-00823">
    <ean/>
    <upc/>
    <page-attributes/>
    <custom-attributes>
        <custom-attribute attribute-id="color">000100015BA00-00823</custom-attribute>
    </custom-attributes>
    <pinterest-enabled-flag>false</pinterest-enabled-flag>
    <facebook-enabled-flag>false</facebook-enabled-flag>
</product>
<product product-id="000100103H103-07546">
    <ean/>
    <upc/>
    <unit/>
    <min-order-quantity>1</min-order-quantity>
    <custom-attributes>
        <custom-attribute attribute-id="color">000100103H103-07546</custom-attribute>
    </custom-attributes>
    <pinterest-enabled-flag>false</pinterest-enabled-flag>
</product>

Вот что я пробовал:

for x in  root.findall('dw:product/dw:custom-attributes/dw:custom-attribute[@attribute-id="color" and text()="000100103H103-07546"]', ns):
    print x.text

Но я получаю это сообщение об ошибке: SyntaxError:неверный предикат

Я думаю, это потому, что text () не работает с пакетом xml.Есть ли способ сделать это без lxml?

Спасибо!

...