我需要节点AX_Namensnummer
,其中子节点istBestandteilVon
具有属性值urn:adv:oid:DEBBAL0600001XOX
。
我想使用命名空间字典。
我使用Python 2.7
和ElementTree
在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:
.//adv:AX_Namensnummer[adv:istBestandteilVon/@xlink:href='urn:adv:oid:DEBBAL0600001XOX']
错误:
SyntaxError: invalid predicate
有什么想法怎么了? Python 2.7和ElementTree可以处理这种xpath吗?
最佳答案
您的XPath(包括谓词)看起来不错。它必须是limitation of ElementTree。
也许尝试使用基本谓词定位adv:istBestandteilVon
,然后获取其父级(..
是parent::*
的abbreviated syntax)...
xpath=".//adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."
编辑
只返回
adv:AX_Namensnummer
...xpath=".//adv:AX_Namensnummer/adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/.."