Rakesh
Rakesh

Reputation: 310

unable to get xpath where parent and child node have different default namespaces

I am here stuck with a situation wherein I have to resolve and transform the given XML using the xslt. I need to write the xpaths to do this.

Sample xml is given below. where in both ServiceProviderGroupPackage and SecondNode have Different namespaces. I need to get the namespace of IReference Node to get its value.

<?xml version="1.0" encoding="utf-8" ?>
<ServiceProviderGroupPackage Version="1.2.0" xmlns="http://webconnectivity.co.uk/ServiceProviderGroupPackage"
                             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                             xmlns:wsa="http://www.w3.org/2005/08/addressing">
  <FirstNode Version="1.2.0" xmlns="http://www.MySite.org/Standards/MySiteMsgSvc/1"
                    xmlns:rlc="http://www.MySite.org/standards/SecondNode/1"
                    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
  </FirstNode>
  <SecondNode Version="2010-2" xmlns:ac="http://www.MySite.org/Standards/MySiteMsgSvc/1"
                      xmlns="http://www.MySite.org/standards/SecondNode/1"
                      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                      xmlns:lloyds="http://www.lloyds.com/The-Market/Operating-at-Lloyds/Resources/Risk_codes">
    <Movement Sender="Sender" Receiver="Receiver">
      <Contract>
        <Type>direct</Type>
        <Nature>lm_proportional_or_nonproportional</Nature>
        <Reference>B0509311011UMR</Reference>
        <IReference>P00000062MM/13A</IReference>
      </Contract>
    </Movement>
  </SecondNode>
</ServiceProviderGroupPackage>

Upvotes: 0

Views: 1085

Answers (1)

Jens Erat
Jens Erat

Reputation: 38662

Each element inherits the namespace of the parent element if not overwritten, so the IReference element has the same namespace like the SecondNode element, http://www.MySite.org/standards/SecondNode/1.


To retrieve the namespace of an arbitrary element, use the namespace-uri(...) function.

With XPath 2.0, you can use the wildcard-operator * instead of a namespace prefix:

namespace-uri(//*:IReference)

or also

//*:IReference/namespace-uri()

With XPath 1.0, you will have to check for the name in a predicate:

namespace-uri(//*[local-name() = "IReference"])

Upvotes: 2

Related Questions