Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

The wiki discusses upon the enhancement of xpath-capabilities for searching specific elements via CPS-core API and basically to:

  • provide possibility to run xpath-searches over multiple anchors and dataspacesprovide possibilites to search for specific ranges, operators <>, regex ...

...

Jira : CPS-1131

 1.Using >,< operators

...

Example

Description

/bookstore/book[@price>800]Select price nodes with price>800
/bookstore/book[@price>800
] &limit=4
and @price<1000]Select price nodes between 800-1000
 /bookstore/book[@price>800
and @price<1000]&limit=10
]/titleselects all the title nodes with a price higher than 800
 /bookstore/book[@price>800]/title&limit=2

NOTE: We can add limit to above examples as a query parameter

 2.Using Contains function

           

...

Example 

Description

/bookstore/book/[contains(@language,'En')] 

It is used when the value of any attribute changes dynamically, The contain feature has an ability to find the element with partial text as shown in below XPath example:

/bookstore/book/[contains(@language,'en')] 


XPath expression partial value

‘en’

‘En’ is used in place English




3.Using OR & AND functions

...

the below or  

 /bookstore/book/[@title='The Golden compass' and @lang='english'] 

  • Xpath expression finds values contains barnes and title the lemon table
/bookstore /book[@author contains("Philip Pullman") and @title = "The Golden compass "]

/bookstore /book[@author  contains("Iain M. Banks") and (@title  contains("Far Horizons") or @title  contains("Feersum Endjinn "))] 

Example

Description

/bookstore/book/[@title='The Golden compass' or @lang='english']
  • In OR expression, two conditions are used, whether 1st condition OR 2nd condition should be true. It is also applicable if any one condition is true or maybe both. Means any one condition should be true to find the element.
  • In
  • this XPath expression, it identifies the elements whose single or both conditions are true.
 /bookstore/book/[@title='The Golden compass' and @lang='english']
  • In AND expression, two conditions are used, both conditions should be true to find the element. It fails to find element if any one condition is false.

 4.Using starts-with/Ends-with function

...

function/bookstore/book[starts-with(@code,'sid')]
ExampleDescription


/bookstore/book[starts-with

(@code,'sid')]


  • For example -: Suppose the ID of particular element changes dynamically like:

Code = “sid1”
Code = “sid2”
Code = “sid3”
Code = “sid4”
Code = “sid5”

Here, XPath finds those element whose ‘Code’ starting with ‘sid’.

similarly,

...

                                                         Code                              Code                              Code                              Code

/bookstore/book[ends-with(@code,'sid')]

Example

Description

/bookstore/book[ends-with(@code,'sid')]
  • For example -: Suppose the ID of particular element changes dynamically like:

 Code = “1-sid”

 Code = “2-sid”

 Code = “3-sid”

 Code = “4-sid”

 Code = “5-sid”

 

Here, XPath finds those element whose ‘Code’ ending with ‘sid’.

 5.Using Reqular Expression

Suppose    ‘title’: ‘Feersum Endjinn’

Xpath Regular Expressions

Example

Description

 /bookstore/book[@title = ^([a-zA-Z{10}\s])]

Suppose    ‘title’: ‘Feersum Endjinn’

Here, Xpath finds title attributes accordingly as given in expression

Search along multiple anchors within a dataspace

Need to create new end point.

For Example,

http://localhost:8883/cps/api/v1/dataspaces/:dataspace-name/nodes?xpath=/bookstore/categories[@code='02'][@title='The Golden Compass']

...