xpath - 选择父级,其中子级具有命名空间字典

时间:2017-02-20 16:58:29

标签: python xml xpath xml-namespaces elementtree

我需要节点AX_Namensnummer,其中子节点istBestandteilVon具有属性值urn:adv:oid:DEBBAL0600001XOX

我想使用命名空间字典。

我正在使用Python 2.7ElementTree在Dynamo工作。所以我不能使用lxml

的xml:

<?xml version="1.0" encoding="UTF-8"?>
<AX_Bestandsdatenauszug
    xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0"
    xmlns:adv="http://www.adv-online.de/namespaces/adv/gid/6.0"
    xmlns:gco="http://www.isotc211.org/2005/gco"
    xmlns:gmd="http://www.isotc211.org/2005/gmd"
    xmlns:gml="http://www.opengis.net/gml/3.2"
    xmlns:ows="http://www.opengis.net/ows"
    xmlns:wfs="http://www.adv-online.de/namespaces/adv/gid/wfs"
    xmlns:wfsext="http://www.adv-online.de/namespaces/adv/gid/wfsext"
    xmlns:xsd="http://www.w3.org/2001/XMLSchema"
    xmlns:xlink="http://www.w3.org/1999/xlink"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:ogc="http://www.adv-online.de/namespaces/adv/gid/ogc"
    xsi:schemaLocation="http://www.adv-online.de/namespaces/adv/gid/6.0 NAS-Operationen.xsd">

    <enthaelt>
    <gml:featureMember>
                <AX_Namensnummer gml:id="DEBBAL0600000XUm">
                    <gml:identifier codeSpace="http://www.adv-online.de/">urn:adv:oid:DEBBAL0600000XUm</gml:identifier>
                    <istBestandteilVon xlink:href="urn:adv:oid:DEBBAL0600000XOX"/>
                    <benennt xlink:href="urn:adv:oid:DEBBAL0600000Y09"/>
                </AX_Namensnummer>
                <AX_Namensnummer gml:id="DEBBAL0600001XUm">
                    <gml:identifier codeSpace="http://www.adv-online.de/">urn:adv:oid:DEBBAL0600001XUm</gml:identifier>
                    <istBestandteilVon xlink:href="urn:adv:oid:DEBBAL0600001XOX"/>
                    <benennt xlink:href="urn:adv:oid:DEBBAL0600000Y08"/>
                </AX_Namensnummer>
            </gml:featureMember>
    </enthaelt>
</AX_Bestandsdatenauszug>

代码:

import clr
import sys
clr.AddReference('ProtoGeometry')
from Autodesk.DesignScript.Geometry import *
sys.path.append("C:\Program Files (x86)\IronPython 2.7\Lib")
#The inputs to this node will be stored as a list in the IN variables.

path="file.xml"
uniStr = unicode(open(path, 'r').read())
fixed = uniStr.encode('ascii', 'replace')
fixed.decode('utf-8', 'replace')
tree = ET.ElementTree(ET.fromstring(fixed))
root = tree.getroot()

xpath=".//adv:AX_Namensnummer[adv:istBestandteilVon/@xlink:href='urn:adv:oid:DEBBAL0600001XOX']"

ns = {"":"http://www.adv-online.de/namespaces/adv/gid/6.0", "adv":"http://www.adv-online.de/namespaces/adv/gid/6.0","gco":"http://www.isotc211.org/2005/gco", 
"gmd":"http://www.isotc211.org/2005/gmd","gml":"http://www.opengis.net/gml/3.2","ows":"http://www.opengis.net/ows",
"wfs":"http://www.adv-online.de/namespaces/adv/gid/wfs","wfsext":"http://www.adv-online.de/namespaces/adv/gid/wfsext","xsd":"http://www.w3.org/2001/XMLSchema",
"xlink":"http://www.w3.org/1999/xlink","xsi":"http://www.w3.org/2001/XMLSchema-instance","ogc":"http://www.adv-online.de/namespaces/adv/gid/ogc"}

elem = root.find(xpath,ns)

print elem

的xpath:

.//广告:AX_Namensnummer [广告:istBestandteilVon / @的xlink:HREF = '瓮:广告:OID:DEBBAL0600001XOX']

错误:

SyntaxError: invalid predicate

任何想法都错了吗? Python 2.7和ElementTree可以处理这种xpath吗?

1 个答案:

答案 0 :(得分:1)

你的XPath,包括谓词,看起来不错。它必须是limitation of ElementTree

也许尝试使用基本谓词定位adv:istBestandteilVon,然后获取其父级(..parent::*的{​​{3}})...

xpath=".//adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."

修改

仅返回adv:AX_Namensnummer ...

xpath=".//adv:AX_Namensnummer/adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."