如果属性存在,则打印子描述

时间:2017-03-08 15:28:46

标签: python xml python-3.x xpath lxml

我有一些xml文件的一部分(例如):

<?xml version="1.0" encoding="UTF-8" ?>
<!-- created at 2017-03-03 11:35:02 -->
<UANodeSet xmlns="http://foundation.org/UA/2011/03/NodeSet.xsd" xmlns:uax="http://foundation.org/UA/20/02/Types.xsd">
    <UAVariable NodeId="ns=1;s=AGENT.OBJECTS.OCP.Data.State" BrowseName="1:State" DataType="UInt16" AccessLevel="7" UserAccessLevel="7" Historizing="true">
        <DisplayName Locale="en">State</DisplayName>
        <Description Locale="en">OCP7.(CONN)</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS.OCP.Data</Reference>
            <Reference ReferenceType="HasTypeDefinition">BaseVariableType</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=KomunikationInput</Reference>
            <Reference ReferenceType="HasNotifier">ns=1;s=Komunikation.Data.State.failure</Reference>
        </References>
        <Value>
            <uax:UInt16>0</uax:UInt16>
        </Value>
    </UAVariable>
    <UAObject NodeId="ns=1;s=AGENT.OBJECTS.LOGIN" BrowseName="1:LOGIN">
        <DisplayName Locale="en">LOGIN</DisplayName>
        <Description Locale="en">LOGIN</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS</Reference>
            <Reference ReferenceType="HasTypeDefinition">FolderType</Reference>
        </References>
    </UAObject>
    <UAObject NodeId="ns=1;s=AGENT.OBJECTS.F01" BrowseName="1:F01">
        <DisplayName Locale="en">F01</DisplayName>
        <Description Locale="en">F01</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS</Reference>
            <Reference ReferenceType="HasTypeDefinition">FolderType</Reference>
        </References>
    </UAObject>
    <UAObject NodeId="ns=1;s=AGENT.OBJECTS.F01.1" BrowseName="1:1">
        <DisplayName Locale="en">1</DisplayName>
        <Description Locale="en">1</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS.F01</Reference>
            <Reference ReferenceType="HasTypeDefinition">ns=1;s=ObjectTypes.PROJECT.Forder</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=ObjectTypes.PROJECT.Forder.Overview</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=ObjectTypes.PROJECT.Forder.Info_Display</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=ObjectTypes.PROJECT.Forder.Version</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=ObjectTypes.PROJECT.Forder.Info_Display_Mobile</Reference>
        </References>
    </UAObject>
    <UAObject NodeId="ns=1;s=AGENT.OBJECTS.F01.1.Data" BrowseName="1:Data">
        <DisplayName Locale="en">Data</DisplayName>
        <Description Locale="en">Data</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS.F01.1</Reference>
            <Reference ReferenceType="HasTypeDefinition">FolderType</Reference>
        </References>
    </UAObject>
    <UAVariable NodeId="ns=1;s=AGENT.OBJECTS.F01.1.Data.State" BrowseName="1:State" DataType="Boolean" ValueRank="1" AccessLevel="7" UserAccessLevel="7" Historizing="true">
        <DisplayName Locale="en">State</DisplayName>
        <Description Locale="en">State</Description>
        <References>
            <Reference ReferenceType="HasComponent" IsForward="false">ns=1;s=AGENT.OBJECTS.F01.1.Data</Reference>
            <Reference ReferenceType="HasTypeDefinition">BaseVariableType</Reference>
            <Reference ReferenceType="HasComponent">ns=1;s=ObjectTypes.PROJECT.Forder.Data.State.RelMirrorInput</Reference>
        </References>
        <Value>
            <uax:ListOfBoolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
                <uax:Boolean>false</uax:Boolean>
            </uax:ListOfBoolean>
        </Value>
    </UAVariable>
</UANodeSet>

如果孩子有属性HasNotifier,我想写脚本,而不是打印OCP7.(CONN)。 我写了xpath,但它没有用。问题是为什么不工作,或者我做错了什么?

//*[namespace-uri() = 'http://foundation.org/UA/2011/03/NodeSet.xsd'][local-name() = 'Reference'][@ReferenceType = 'HasNotifier']/../../Description[@Locale="en"]

这不是重复,没有人可以帮助我(给出提示或解决方案)。 这就是原因,可能是lxml有一些bug - 这个问题,或者真的lxml有一些bug。

0 个答案:

没有答案