通过XPath选择具有特定属性的子节点的父节点?

时间:2017-02-17 17:28:26

标签: xml xpath xml-namespaces

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

以下是我的XML中的一个小剪辑:

<enthaelt>
        <wfs:FeatureCollection gml:id="A75">
        <gml:featureMember>
            <AX_Namensnummer gml:id="DEBBAL0600000XUm">
                <gml:identifier codeSpace="http://www.adv-online.de/">urn:adv:oid:DEBBAL0600000XUm</gml:identifier>
                <lebenszeitintervall>
                    <AA_Lebenszeitintervall>
                        <beginnt>2012-01-11T07:03:49Z</beginnt>
                    </AA_Lebenszeitintervall>
                </lebenszeitintervall>
                <modellart>
                    <AA_Modellart>
                        <advStandardModell>DLKM</advStandardModell>
                    </AA_Modellart>
                </modellart>
                <anlass>000000</anlass>
                <laufendeNummerNachDIN1421>0001.00.00.00.00</laufendeNummerNachDIN1421>
                <eigentuemerart>3000</eigentuemerart>
                <istBestandteilVon xlink:href="urn:adv:oid:DEBBAL0600000Y09"/>
                <benennt xlink:href="urn:adv:oid:DEBBAL0600000Y09"/>
            </AX_Namensnummer>
        </gml:featureMember>
    </wfs:FeatureCollection>
</enthaelt>

以下是命名空间:

<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">

我的XPath:

.//{http://www.adv-online.de/namespaces/adv/gid/6.0}AX_Namensnummer/{http://www.adv-online.de/namespaces/adv/gid/6.0}istBestandteilVon[@{http://www.w3.org/1999/xlink}href='urn:adv:oid:DEBBAL0600000Y09']

我得到了节点istBestandteilVon,但我需要AX_Namensnummer

我做错了什么?

1 个答案:

答案 0 :(得分:0)

  

我做错了什么?

  • 未定义和使用名称空间前缀。
  • 没有意识到哪些元素在命名空间中,哪些不在。
  • 不了解默认名称空间如何影响子元素。
  • 定位指出的@xlink:href值并不存在 元件。
  • 将谓词放在子元素而不是父元素上。

对于这个XML(修复为命名空间 - 格式良好并更新以反映更新问题中的默认命名空间),

<?xml version="1.0" encoding="UTF-8"?>
<AX_Namensnummer xmlns="http://www.adv-online.de/namespaces/adv/gid/6.0" 
                 xmlns:gml="http://www.adv-online.de/namespaces/adv/gid/6.0" 
                 xmlns:xlink="http://www.w3.org/1999/xlink"
                 gml:id="DEBBAL0600000XUm">   
    <gml:identifier codeSpace="http://www.adv-online.de/">urn:adv:oid:DEBBAL0600000XUm</gml:identifier>  
    <lebenszeitintervall> 
      <AA_Lebenszeitintervall> 
        <beginnt>2012-01-11T07:03:49Z</beginnt> 
      </AA_Lebenszeitintervall> 
    </lebenszeitintervall>  
    <modellart> 
      <AA_Modellart> 
        <advStandardModell>DLKM</advStandardModell> 
      </AA_Modellart> 
    </modellart>  
    <anlass>000000</anlass>  
    <laufendeNummerNachDIN1421>0001.00.00.00.00</laufendeNummerNachDIN1421>  
    <eigentuemerart>3000</eigentuemerart>  
    <istBestandteilVon xlink:href="urn:adv:oid:DEBBAL0600000XOX"/>  
    <benennt xlink:href="urn:adv:oid:DEBBAL0600000Y09"/> 
</AX_Namensnummer> 

并且对于此命名空间声明,

xlink="http://www.w3.org/1999/xlink"
adv="http://www.adv-online.de/namespaces/adv/gid/6.0"

这个XPath,

//adv:AX_Namensnummer[adv:istBestandteilVon/@xlink:href='urn:adv:oid:DEBBAL0600000XOX']

将根据请求选择父AX_Namensnummer元素。

另请参阅: How does XPath deal with XML namespaces?

相关问题