0

I have a problem when I want to select an item with xpath from an xml-file when there are different schemas for one namespace.

Given is this valid xml-file:

<getDataAction xmlns="http://example.org/ns1">
   <getData xmlns:f4k1="http://example.org/ns2" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
      <f4k1:place xmlns:f5k1="http://example.org/ns3">
         <f4k1:city i:type="f5k1:Country" xmlns:f5k1="http://example.org/ns4">
           <f5k1:notice>Notice</f5k1:notice>
         </f4k1:city>
         <f4k1:testvillage i:nil="true">
           <f4k1:data xmlns:f5k1="http://example.org/ns5">
             <f5k1:address xmlns:d7k1="http://example.org/ns6">
               <f5k1:important>no</f5k1:important>
             </f5k1:address>
           </f4k1:data>
         </f4k1:testvillage>
      </f4k1:place>
  </getData>
</getDataAction>

I want to select

<f5k1:notice>Notice</f5k1:notice>

So I use this xpath-query //f5k1:notice, but it does not work. If I would change the ns5 namespace (line 8) to ns4 (as in line 4) it will work.

Can you tell me why this does work and what I have to do to get it working without changing the XML?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
Evil_skunk
  • 3,040
  • 4
  • 30
  • 42
  • 1
    How is the prefix `f5k1` mapped to a namespace URI in the code that runs your XPath expression? – jasso Dec 12 '12 at 22:33

1 Answers1

1

The XPath expression //f5k1:notice evaluates to a set of elements whose local name is notice and whose namespace name is whatever namespace the environment has bound to f5k1. Since you don't specify what environment you're using to evaluate this XPath expression, it's impossible to say exactly what is going wrong, but the easiest explanation is that in your environment, f5k1 is not bound to http://example.org/ns4.

The best fix: learn how to bind prefixes to namespaces in the system you're using.

The easiest workaround: //*[local-name="notice" and namespace-name="http://example.org/ns4"].

C. M. Sperberg-McQueen
  • 24,596
  • 5
  • 38
  • 65