Thursday, September 20, 2018

SuiteScript > XML > How to use XPATH with the NetSuite XML APIs

Question/Problem:
 
How to use XPATH with the NetSuite XML APIs

Answer/Solution:
 

Before being able to use the XML APIs, we should know what XPATH is and how to use it.

 

XPath

          is a language for finding information in an XML document.

          is used to navigate through elements and attributes in an XML document

          uses path expressions to select nodes or node sets in an XML document.

 

The path expressions used in XPATH look very much like the expressions you see when you work with a traditional computer file system. The node is selected by following a path or steps. The most useful path expressions are listed below:

 

Expression

Description

nodename

Selects all child nodes of the named node

/

Selects from the root node

//

Selects nodes in the document from the current node that match the selection no matter where they are

.

Selects the current node

..

Selects the parent of the current node

@

Selects attributes

 

 

We will use the following xml for our examples below:

 

/*** Create XML ***/

var stXMLRequest = '<?xml version="1.0" encoding="ISO-8859-1"?>';

stXMLRequest = stXMLRequest + '<bookstore>';

stXMLRequest = stXMLRequest + '<book category="cooking">';

stXMLRequest = stXMLRequest + '<title >Everyday Italian</title>';

stXMLRequest = stXMLRequest + '<author>Giada De Laurentiis</author>';

stXMLRequest = stXMLRequest + '<year>2005</year>';

stXMLRequest = stXMLRequest + '<price>30.00</price>';

stXMLRequest = stXMLRequest + '</book>';

stXMLRequest = stXMLRequest + '<book category="cooking">';

stXMLRequest = stXMLRequest + '<title >Chef At Home</title>';

stXMLRequest = stXMLRequest + '<author>Michael Smith</author>';

stXMLRequest = stXMLRequest + '<year>2010</year>';

stXMLRequest = stXMLRequest + '<price>29.95</price>';

stXMLRequest = stXMLRequest + '</book>';

stXMLRequest = stXMLRequest + '<book category="gardening">';

stXMLRequest = stXMLRequest + '<title >Orchids For Life</title>';

stXMLRequest = stXMLRequest + '<author>Gardener Sam</author>';

stXMLRequest = stXMLRequest + '<year>2008</year>';

stXMLRequest = stXMLRequest + '<price>9.95</price>';

stXMLRequest = stXMLRequest + '</book>';

stXMLRequest = stXMLRequest + '</bookstore>';

 

//convert the string data into XML using nlapiStringToXML

var xmlRequest = nlapiStringToXML(stXMLRequest);

 

 

XPath Nodes

 

          root element node  - <bookstore>

          element node         - <author>Giada De Laurentiis</author>

          attribute node        - lang="en"

 

 

The following table shows path expressions that can be used to available XML APIs.

 

XML API

Path

Return Values

nlapiSelectValue()

nlapiSelectValues()

 //price

 Value of price(s)

 /bookstore/book/price

 Value of price(s)

 /bookstore/book[2]/author

 Author(s) of the second book

 /bookstore/book[price>25.00]/title

 Title(s) of book(s) with price greater than 25.00

 //title[@]

 Title(s) of books with lang="fr"

 //book[@category='gardening']/year

 Year(s) of books with category="gardening"

nlapiSelectNode()

 /bookstore/book/title

 <title >Everyday Italian</title>

 /bookstore/book

 <book category="cooking">

     <title >Everyday Italian</title>

     <author>Giada De Laurentiis</author>

     <year>2005</year>

     <price>30.00</price>

 </book>

 /bookstore

 <bookstore>

    <book category="cooking">

         <title >Everyday Italian</title>

         <author>Giada De Laurentiis</author>

         <year>2005</year>

         <price>30.00</price>

    </book>

    <book category="cooking">

         <title >Chef At Home</title>

         <author>Michael Smith</author>

         <year>2010</year>

         <price>29.95</price>

     </book>

     <book category="gardening">

         <title >Orchids For Life</title>

         <author>Gardener Sam</author>

         <year>2008</year>

         <price>9.95</price>

     </book>

</bookstore>

nlapiSelectNodes()

 //title

 [

   <title >Everyday Italian</title>,

   <title >Chef At Home</title>,

   <title >Orchids For Life</title>

 ]

 /bookstore/book

 [ 

    <book category="cooking">

         <title >Everyday Italian</title>

         <author>Giada De Laurentiis</author>

         <year>2005</year>

         <price>30.00</price>

    </book>,

    <book category="cooking">

         <title >Chef At Home</title>

         <author>Michael Smith</author>

         <year>2010</year>

         <price>29.95</price>

     </book>,

     <book category="gardening">

         <title >Orchids For Life</title>

         <author>Gardener Sam</author>

         <year>2008</year>

         <price>9.95</price>

     </book>

  ]

 

 

 

Reference: XPATH Tutorial, W3 Schools, http://www.w3schools.com/xpath/

No comments:

Post a Comment