简体   繁体   English

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

[英]Select parent nodes which have a subnode with specific attribute via XPath?

I need the node AX_Namensnummer where child node istBestandteilVon has the attribute value urn:adv:oid:DEBBAL0600000Y09 我需要节点AX_Namensnummer ,其中子节点istBestandteilVon具有属性值urn:adv:oid:DEBBAL0600000Y09

Here is a small clip from my XML: 这是我的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>

here are the namespaces: 这是名称空间:

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

my XPath: 我的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']

I get the node istBestandteilVon , but I need AX_Namensnummer . 我得到节点istBestandteilVon ,但我需要AX_Namensnummer

What am I doing wrong? 我究竟做错了什么?

What am I doing wrong? 我究竟做错了什么?

  • Not defining and using namespace prefixes. 不定义和使用名称空间前缀。
  • Not realizing which elements are in namespaces and which are not. 没有意识到哪些元素在命名空间中,哪些不在。
  • Not understanding how default namespaces affect child elements. 不了解默认名称空间如何影响子元素。
  • Targeting a @xlink:href value that doesn't exist on the noted element. 定位在所提到的元素上不存在的@xlink:href值。
  • Placing the predicate on the child rather than the parent element. 将谓词放在子元素而不是父元素上。

For this XML (repaired to be namespace-well-formed and updated to reflect the default namespace in your updated question), 对于这个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> 

and for this namespace declaration, 并为此命名空间声明,

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

this XPath, 这个XPath,

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

will select the parent AX_Namensnummer element as requested. 将根据请求选择父AX_Namensnummer元素。

See also: How does XPath deal with XML namespaces? 另请参见: XPath如何处理XML命名空间?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM