Reputation: 263
I need the node AX_Namensnummer
where child node istBestandteilVon
has the attribute value urn:adv:oid:DEBBAL0600001XOX
.
And i want to use a namespace dictionary.
Im working in Dynamo with Python 2.7
and ElementTree
. So i can't use 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>
code:
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:
.//adv:AX_Namensnummer[adv:istBestandteilVon/@xlink:href='urn:adv:oid:DEBBAL0600001XOX']
Error:
SyntaxError: invalid predicate
Any ideas whats wrong? Can Python 2.7 and ElementTree handle this kind of xpath?
Upvotes: 1
Views: 564
Reputation: 52878
Your XPath, including the predicate, looks good. It must be a limitation of ElementTree.
Maybe try targeting the adv:istBestandteilVon
with a basic predicate and then getting its parent (..
is the abbreviated syntax of parent::*
)...
xpath=".//adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."
EDIT
To only return adv:AX_Namensnummer
...
xpath=".//adv:AX_Namensnummer/adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."
Upvotes: 1