1

I need the node AX_Namensnummer where child node istBestandteilVon has the attribute value urn:adv:oid:DEBBAL0600000Y09

Here is a small clip from my 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:

.//{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.

What am I doing wrong?

Yuli
  • 243
  • 4
  • 9

1 Answers1

0

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.
  • 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 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,

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

will select the parent AX_Namensnummer element as requested.

See also: How does XPath deal with XML namespaces?

Community
  • 1
  • 1
kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • I added the all namespaces. Every time i try to use the prefix it says:" prefix not found in prefix map":( – Yuli Feb 17 '17 at 18:45
  • So, there *is* a default namespace. Answer adjusted to accommodate `adv="http://www.adv-online.de/namespaces/adv/gid/6.0"` as a default namespace. – kjhughes Feb 17 '17 at 19:54
  • Strongly recommend that you review how namespaces work in XML and XPath. For example, make sure you understand [***How does XPath deal with XML namespaces***](http://stackoverflow.com/questions/40796231/how-does-xpath-deal-with-xml-namespaces) – kjhughes Feb 17 '17 at 19:56
  • Now i know more about namespaces,prefix...I know i should use the prefix, but why i can't use the hole namespaces? This xpath should work:`.//{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']` ,but he doesn't work. I got `SyntaxError: invalid predicate` – Yuli Feb 20 '17 at 15:49
  • You'll need XPath 3.0 for [***Braced URI Literals***](https://www.w3.org/TR/xpath-30/#doc-xpath30-BracedURILiteral). – kjhughes Feb 20 '17 at 16:01
  • Ok, so i can't use ElementTree, because the module provides limited support for XPath expressions? But easy xpath with the hole namespace like :`.//{http://www.adv-online.de/namespaces/adv/gid/6.0}AX_Namensnummer` work. – Yuli Feb 20 '17 at 16:25
  • No, I didn't say that. I said that you'll need XPath 3.0 for **braced URI literals**. Just declare and use normal namespace prefixes as shown in [***How does XPath deal with XML namespaces***](http://stackoverflow.com/questions/40796231/how-does-xpath-deal-with-xml-namespaces) and as I've shown in this answer. – kjhughes Feb 20 '17 at 17:01
  • Please [**accept**](http://meta.stackoverflow.com/q/5234/234215) this answer if it's helped. Thanks. – kjhughes Feb 20 '17 at 17:02