2

I am having issues with xPath for this document. I want to get element C50603 when element C21201 has part number 1111111. It works if I remove namespace.

This is what I have so far but without namespace.

   /*[local-name()='Root']
   /*[local-name()='InputMessagePart_1']
   /*[local-name()='EFACT_D97A_ORDERS']
   /*[local-name()='LINLoop1'][C212/C21201='111111']
   /*[local-name()='RFFLoop3']
   /*[local-name()='RFF_6']
   /*[local-name()='C506_6']
   /*[local-name()='C50603']

ns3 is :='http://schemas.microsoft.com/BizTalk/EDI/EDIFACT/2006']

    <ns3:LINLoop1>
    <ns3:LIN>
        <LIN01>2</LIN01>
        <ns3:C212>
            <C21201>22222222</C21201>
            <C21202>VP</C21202>
            <C21204>91</C21204>
        </ns3:C212>
    </ns3:LIN>
    <ns3:QTY_3>
        <ns3:C186_3>
            <C18601>21</C18601>
            <C18602>1</C18602>
            <C18603>PCE</C18603>
        </ns3:C186_3>
    </ns3:QTY_3>
    <ns3:PRILoop1>
        <ns3:PRI>
            <ns3:C509>
                <C50901>AAA</C50901>
                <C50902>xxxxx</C50902>
                <C50903>CT</C50903>
                <C50905>1</C50905>
                <C50906>PCE</C50906>
            </ns3:C509>
        </ns3:PRI>
    </ns3:PRILoop1>
    <ns3:RFFLoop3>
        <ns3:RFF_6>
            <ns3:C506_6>
                <C50601>LI</C50601>
                <C50603>00002</C50603>
            </ns3:C506_6>
        </ns3:RFF_6>
    </ns3:RFFLoop3>
    <ns3:SCCLoop2>
        <ns3:SCC_2>
            <SCC01>1</SCC01>
        </ns3:SCC_2>
        <ns3:QTYLoop4>
            <ns3:QTY_7>
                <ns3:C186_7>
                    <C18601>21</C18601>
                    <C18602>1</C18602>
                    <C18603>PCE</C18603>
                </ns3:C186_7>
            </ns3:QTY_7>
            <ns3:DTM_25>
                <ns3:C507_25>
                    <C50701>2</C50701>
                    <C50702>xxxx</C50702>
                    <C50703>102</C50703>
                </ns3:C507_25>
            </ns3:DTM_25>
        </ns3:QTYLoop4>
    </ns3:SCCLoop2>
</ns3:LINLoop1>
z--
  • 2,186
  • 17
  • 33
bizguy
  • 23
  • 2

2 Answers2

1

At first glance the problem appears to be in this predicate

[C212/C21201='111111']

The C212 element has an ns3 prefix so you need to account for its namespace, and it's also another level down from the LINLoop1 inside ns3:LIN

[*[local-name() = 'LIN']/*[local-name() = 'C212']/C21201='111111']

(This will work provided the C21201 element isn't in a namespace. I can't tell from the partial XML you've posted whether or not this is the case - if there's an xmlns="..." anywhere higher up the tree then you'll need to use the same local-name() trick on that path step too.)

Ian Roberts
  • 120,891
  • 16
  • 170
  • 183
0

It works if I remove namespace.

There's your real answer, right there. Namespaces create many problems and solve none.

This is what I have so far but without namespace.

 /*[local-name()='LINLoop1'][C212/C21201='111111']

This can't be right, as you are missing the LIN element between LINLoop1 and C212. The conditional on /*[local-name()='LINLoop1'] would have to look something like this

[./*[local-name()='LIN']/*[local-name()='C212']/*[local-name()='C21201' and text()='111111']]

Note that all these local-name() shenanigans are needed only because namespaces have got in the way. (Nuisance value is all they're good for, anyway.) As a practical matter, removing namespaces is often the best first step.

Community
  • 1
  • 1
arayq2
  • 2,502
  • 17
  • 21
  • [*[local-name() = 'LIN']/*[local-name() = 'C212']/C21201='111111'] – bizguy Jul 15 '14 at 16:38
  • And is that why you downgraded the answer? – arayq2 Jul 15 '14 at 16:38
  • That means you don't have a default namespace higher up in the XML element hierarchy. It was impossible to tell from the fragment you quoted, so local-name='C21201'] was a "take no prisoners" approach to the problem. Once again, the more namespaces you remove, the simpler the answer gets. :-) – arayq2 Jul 15 '14 at 16:41
  • The `C212` etc elements are not nested inside the `LIN` element, therefore your XPath will not work. – MarioDS Jul 15 '14 at 17:59
  • Please take another look. – arayq2 Jul 15 '14 at 18:18
  • @arayq2 Oh yes, I was confused by the `LIN01`. My bad. I've merged your last two conditions using `and` (had to do an edit anyway to remove downvote), and converted my vote. – MarioDS Jul 15 '14 at 20:12