W
W
wyfinger2017-11-21 04:45:22
Python
wyfinger, 2017-11-21 04:45:22

Library for XML+XPath in Python?

I have an XML file with the following structure:

<?xml version="1.0" encoding="UTF-8"?>
<DeviceData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="DIGSIXML1-4.xsd">
     <Settings>
          <FunctionGroup Name="Данные энергосистемы 1">
               <SettingPage Name="Парам ТТ/ТН">
                    <Parameter DAdr="0205" Name="Первичный номинальный ток ТТ" Type="Dec">
                         <Value>1000</Value>
                         <Comment Dimension="А" MinValue="10" MaxValue="5000" DefaultValue="1000"/>
                    </Parameter>
                    <Parameter DAdr="0206" Name="Вторичный номинальный ток ТТ" Type="Txt">
                         <Value>12922</Value>
                         <Comment Number="12921" Name="1А"/>
                         <Comment Number="12922" Name="5А"/>
                         <Comment DefaultValue="12921"/>
                    </Parameter>
                    <Parameter DAdr="0210" Name="U4 трансформатор напряжения подкл. как" Type="Txt">
                         <Value>12508</Value>
                         <Comment Number="12508" Name="Не подключен"/>
                         <Comment Number="12790" Name="Uдельта трансформатора напряжения"/>
                         <Comment DefaultValue="12508"/>
                    </Parameter>
               </SettingPage>
          </FunctionGroup>
     </Settings>
</DeviceData>

You need a library for working with XML that supports full-fledged XPath queries, specifically you need this:
//Parameter[@DAdr="0206"]/Comment[@Number=//Parameter[@DAdr="0206"]/Value/text()]/@Name

those. First, we look for the Parameter element with the @DAdr="0206" attribute, then among its Comment subelements we look for one whose @Number attribute matches the text of the Value subelement of the same Parameter element, and at the very end we take the value of the @Name attribute of the found element.
Those. nested queries. For example, this is supported in the XML Tools plugin for Notepad++. Neither ElementTree nor lxml can do this. These XPath queries come from the config, so it will not be possible to search for elements sequentially in the code.
Please advise a library for Python, I tried to find something on the github, but most of it can’t even be installed on the go.

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question