xpath - 使用名称空间字典选择子项具有特定attrib值的父项
我需要节点AX_Namensnummer
,其中子节点istBestandteilVon
具有属性值urn:adv:oid:DEBBAL0600001XOX
。xpath - 使用名称空间字典选择子项具有特定attrib值的父项
我想使用命名空间字典。
我在迪纳摩工作,与Python 2.7
和ElementTree
。所以我不能使用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']/.."
你的Xpath实在是一个选项,但是我得到了一个真正的大XML。这部分只是一个例子。当我使用你的xpath。我有两个或两个以上的父母,因为在我的xml中有超过'adv:istBestandteilVon'的值。那么我可以要求e特定的父母名称吗?像'xpath =“.// adv:istBestandteilVon [@xlink:href ='urn:adv:oid:DEBBAL0600001XOX']/.. adv:AX_Namensnummer”'?这个例子不起作用... – Yuli
@Yuli - 'parent ::'和'self ::'轴在ElementTree中似乎不起作用,所以您必须将'adv:AX_Namensnummer'加回到开头的XPath。我用一个例子更新了我的答案。如果这不起作用,请考虑更新您的示例XML,以便重现。 –
你的xpath的作品!好,thx为您的帮助! – Yuli
可能重复的[P ython XPath SyntaxError:无效谓词](http://stackoverflow.com/questions/33830821/python-xpath-syntaxerror-invalid-predicate) – Andersson
不能使用lib lxml @ Andersson – Yuli