Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google sheets IMPORTXML Query get attribute with local-name

This is a follow up question to this previous question: Google sheets IMPORTXML Return no Data N/A

Given the following XML (has namespace) I want to query the CODE number inside CPV_CODE.

<CPV_CODE CODE="90524400"/>

With XPATH I would simply use: /CPV_CODE/@CODE but as I have to use local-name (due to namespace) I have no idea how to get it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried

=importxml(url,"//*[local-name() ='CPV_CODE']")

but it does not return the @CODE number.

>Solution :

In your situation, how about the following xpath?

Sample xpath:

//*[local-name()='CPV_MAIN']/*[local-name()='CPV_CODE']/@CODE

Or

//*[local-name()='CPV_MAIN']//@CODE

Modified formula:

=IMPORTXML(A1,"//*[local-name()='CPV_MAIN']/*[local-name()='CPV_CODE']/@CODE")

Or

=IMPORTXML(A1,"//*[local-name()='CPV_MAIN']//@CODE")
  • In this case, the URL is put in the cell "A1".

Result:

enter image description here

Note:

  • In the URL, it seems that there are 2 CPV_CODE tags. If you want to retrieve <CPV_CODE CODE="90524200"/>, how about the following formula?

      =IMPORTXML(A1,"//*[local-name()='CPV_ADDITIONAL']/*[local-name()='CPV_CODE']/@CODE")
    

    Or

      =IMPORTXML(A1,"//*[local-name()='CPV_ADDITIONAL']//@CODE")
    

Reference:

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading