Flamingo.XPath.get()

The get() method is used to get nodes by XPath.

Syntax

Flamingo.XPath.get(xPath, parent)

Parameters

Parameter Type Description
xPath String XPath expression.
parent Node object Context node against which specified XPath will be executed.

Return Value

Type Description
Array Array of HTMLElement objects found by XPath.

Example 1

The example below demonstrates how to display all values of LI elements.

Source HTML code available on your original desktop site page:

<html>
 <body>
   <ul>
     <li>List item 1</li>
     <li>List item 2</li>
   </ul>
 </body>
</html>

You need to add the following code to your template to display all values of LI elements available on your page:

<!--{foreach Flamingo.XPath.get('//li') as item }-->
 Value is: <!--{= Flamingo.XPath.value('.', item) }--><br/>
<!--{/foreach}-->

The result for the sample page will look like:

Value is: List item 1
Value is: List item 2

Example 2

The example below demonstrates how to display all URLs used in a list.

Source HTML code available on original desktop site page:

<html>
 <body>
   <ul>
     <li><a href="http://www.site1.com">List item 1</a></li>
     <li><a href="http://www.site2.com">List item 2</a></li>
   </ul>
 </body>
</html>

You need to add the following code to your template to display all URLs used in the list:

<!--{foreach Flamingo.XPath.get('//li/a') as item }-->
 URL is: <!--{= Flamingo.XPath.value('@href', item) }--><br/>
<!--{/foreach}-->

The result will look like:

URL is: http://www.site1.com

URL is: http://www.site2.com

Example 3

The example below demonstrates how to add CSS class to the list.

Source HTML code available on original desktop site page:

<html>
 <body>
   <ul>
     <li><a href="http://www.site1.com">List item 1</a></li>
     <li><a href="http://www.site2.com">List item 2</a></li>
   </ul>
 </body>
</html>

You need to add the following code to your template to apply a CSS style called custom-class to the list:

<!--{
var list = X.get('//ul');
list[0] && list[0].classList.add('custom-class');
}-->
<!--{= list[0].outerHTML}-->

The HTML code of the resulting page will look like:

<html>
 <body>
   <ul class="custom-class">
     <li><a href="http://www.site1.com">List item 1</a></li>
     <li><a href="http://www.site2.com">List item 2</a></li>
   </ul>
 </body>
</html>

Example 4

The example below demonstrates how to add the target=”_blank” attribute to each link in the list.

Source HTML code available on original desktop site page:

<html>
 <body>
   <ul>
     <li><a href="http://www.site1.com">List item 1</a></li>
     <li><a href="http://www.site2.com">List item 2</a></li>
   </ul>
 </body>
</html>

You need to add the following code to your template to add the target=”_blank” attribute to each link in the list:

<!--{
var links = X.get('//a');
for (var i = 0; i < links.length; i++) {
  links[i].setAttribute('target', '_blank');
}
}-->

<!--{foreach links as link}-->
  <!--{= link.outerHTML}-->
<!--{/foreach}-->

The HTML code of the resulting page will look like:

<html>
 <body>
   <ul>
     <li><a href="http://www.site1.com" target="_blank">List item 1</a></li>
     <li><a href="http://www.site2.com" target="_blank">List item 2</a></li>
   </ul>
 </body>
</html>

See also