-1

I am trying to find the XPath of the below XML. Its consists of properties array list. I need to fetch the value for 'COMPUTER'.

<output  xmlns:tns="http://www.ariuy.org/" custname="marcus" >
    <tns:column name="customer_english_name">marcus ag</tns:column>
    <tns:column name="customer_primary_name">marcus ag</tns:column>
   
    <tns:reqline>
        <tns:orderline user_item_description="xyz">
            <tns:column name="properties">
                <tns:column name="name">COMPUTER</tns:column>
                <tns:column name="value">HCL </tns:column>
            </tns:column>
            <tns:column name="properties">
                <tns:column name="name">LAPTOP</tns:column>
                <tns:column name="value">HP</tns:column>
            </tns:column>
            <tns:column name="properties">
                <tns:column name="name">PHONE</tns:column>
                <tns:column name="value">MI</tns:column>
            </tns:column>
            <tns:column name="properties">
                <tns:column name="name">JOB</tns:column>
                <tns:column name="value">Developer</tns:column>
            </tns:column>
            
            ......
       
        </tns:orderline>
    </tns:reqline>
</output>

I have tried

/tns:output/tns:reqline/tns:OrderLine/tns:properties[@name='COMPUTER']/@value

but it is not working.

Chris
  • 4,762
  • 3
  • 44
  • 79
Anurag
  • 1
  • 2

3 Answers3

1

To fecth HCL(value under COMPUTER), you can go with :

(//*[name()="tns:column"][@name="properties"])[1]/*[2]/text()

Look for the first column element with a specific attribute of the page. Then select its second child.

Or more secure option :

//*[name()="tns:column"][preceding-sibling::*[name()="tns:column"][.="COMPUTER"]]/text()

Look anywhere for a column element where its preceding-sibling contains the value "COMPUTER".

E.Wiest
  • 5,425
  • 2
  • 7
  • 12
  • These answers are characteristic of a class of answers that can be shorter and more general (+1) than the one I posted, at the expense of efficiency, which won't matter at all for typical documents, but could for database dumps or other large documents. – kjhughes Jul 03 '20 at 15:28
0

Assuming that you have the namespace prefix tns properly declared to abbreviate the http://www.ariuy.org/ namespace value...

This XPath,

/output/tns:reqline/tns:orderline
  /tns:column[@name='properties'][tns:column[@name='name']='COMPUTER']
  /tns:column[@name='value']

will select

<tns:column name="value">HCL </tns:column>

as requested.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
0

You say:

/tns:output/tns:reqline/tns:OrderLine/tns:properties[@name='COMPUTER']/@value

Well

(a) the output element is not in the tns namespace

(b) the orderline element starts with o not O

(c) you don't have an element called tns:properties

(d) there's no @name attribute whose value is "COMPUTER"

(e) there's no @value attribute anywhere.

That's a lot of errors for one line of code.

Others have given you working solutions, but I think it's important you understand why your attempt is wrong.

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Well ....There is no element called properties but there is array of elements. I need to fetch value of computer – Anurag Jul 03 '20 at 18:03
  • According to you what should be the path? – Anurag Jul 03 '20 at 18:06
  • I started such a list mentally while fixing OP's XPath but lost track and decided it wasn't worth cycling back for so many egregious errors. Interesting to see now that neither a list of errors to fix nor a working solution suits. – kjhughes Jul 03 '20 at 22:08
  • @Anurag, with a question like this I prefer to encourage you to solve the problem yourself rather than solving it for you. Sorry if I'm being a bit nanny-ish here, but I think it's a better use of my time. – Michael Kay Jul 04 '20 at 08:07