【问题标题】:xpath - select parent where child has a specific attrib value with namespace dictionaryxpath - 选择父级,其中子级具有命名空间字典的特定属性值
【发布时间】:2017-02-20 16:58:29
【问题描述】:

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

我想使用命名空间字典。

我在 Dynamo 与 Python 2.7ElementTree 一起工作。所以我不能使用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 吗?

【问题讨论】:

标签: python xml xpath xml-namespaces elementtree


【解决方案1】:

您的 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。那么我可以询问具体的父母姓名吗?喜欢xpath=".//adv:istBestandteilVon[@xlink:href='urn:adv:oid:DEBBAL0600001XOX']/..adv:AX_Namensnummer"?这个例子不起作用...
  • @Yuli - parent::self:: 轴似乎在 ElementTree 中不起作用,因此您必须将 adv:AX_Namensnummer 添加回 XPath 的开头。我用一个例子更新了我的答案。如果这不起作用,请考虑更新您的示例 XML,以便我们可以重现。
猜你喜欢
  • 2011-07-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多