Check if a class element exists depending the class attribute using Xpath - xpath

I need to get the ratings(stars) of a particular company using Xpath .Below is the block of div element from which I need to decide if its a 5 star , 4 star , 3 star etc depending the class attribute
div class="star-rating star-rating-4 star-rating--medium" div
"star-rating star-rating-4 star-rating--medium" , if this is class with 4 star rating I need to return 4 , 5 if its a 5 star rating and so on .One way I can do this is try to get Xpath of all variants(3,4,5 star rating) and whichever doesn't turn up as null return that .Is there any better way to achieve this ?

Here is the xpath
Based on the class of div:
//div[#class='star-rating star-rating-4 star-rating--medium']/substring(#class,string-length('star-rating star-rating-')+1,1)
OutPut: 4
Based on index of the rating div:
//div[starts-with(#class,'star-rating star-rating-')][2]/substring(#class,string-length('star-rating star-rating-')+1,1)
This will return the rating of 2nd div with class name that starts with star-rating star-rating-

Related

XPath to return value only elements containing the text

would like to return value of 'Earnings per share' (i.e. -7.3009, -7.1454, -19.6295, -1.6316)
from "http://www.aastocks.com/en/stocks/analysis/company-fundamental/earnings-summary?symbol=01801"
using below as a example for '-7.3009'
=importxml("http://www.aastocks.com/en/stocks/analysis/company-fundamental/earnings-summary?symbol=01801", "//tr/td[contains(text(),'Earnings')]/td[2]")
However, it returns #N/A.
Can someone help?
this xpath will return your specific data
id("cnhk-list")//tr[td[contains(., "Earnings Per Share")]]/td[starts-with(#class, "cfvalue")]//text()
xpath explanation in english is " you actually needs to select the td where row contains Earnings Per Share which is in table that has some specific ID

Xpath operators for WP All Import

I want to import all products filtering these they applied all 3 criteria:
1) they are in stock (DIM)
2) stock is more than 3 pcs (stock_indicator)
3) and they belong to one (any) of these groups 1 or 4
I want all 3 criteria, but in 3rd any of these options
i.e.:
/product[dim1[1] = "1" and stock_indicator[1] > 3 and group[1] = "1" or group/category/id[1] = "4"]
The above does not returns any product, like no product have all these requirements.
What am I doing wrong?
Dim = availability
XML sample:
/product[dim1[1] = "1" and stock_indicator[1] > 3 and group[1] = "1" or group/category/id[1] = "4"]
First of all your XPath assumes that all product elements are at root level, which would not make for a well-formed XML document; the all should be wrapped in some element.
If that is no problem in your environment (since we do not know the whole setup from your question) probably the most prominent problem in your XPath is that you try to compare the value of stock_indicator against a xs:integer but in fact your data sample encodes them as xs:string.
Consequently
stock_indicator[1] > 3
will always return falseā€¦
Try
stock_indicator[1]/number() > 3
or
number(stock_indicator[1]) > 3
instead.
Nevertheless depending on the data structure (e.g. multiple stock_indicatorelements in one product [whatever that might mean]) this could return false positives.
You can use the following XPath to filter the products :
//product[availability="1"][stock_indicator>3][group/id=1 or group/id=4]
// at the beginning as stated by #Benjamin W. Bohl to catch all products
availability is used instead of "Dim"
cleaner syntax used for predicates
no position indexes used ([1]) assuming you only have 1 availability, 1 stock_indicator, 1 id per group in each product
XML used to test.
XPath :
XML is filtered (2 of the 4 products fulfill the conditions) :

filter datatables with same prefix but different suffix in numbers

I am using dataTables for a class list of students. One of my tables looks like this
Student Name || Class Name
Victoria Say || Grade 1
Phillip Hey || Grade 1
Stephen Chew || Grade 3
Marian Boot || Grade 2
Mary Brave || Grade 3
Betty Nancy || Form 1
Bright Hanson || Form 3
If I type 'Grade 3' in the search box, instead of returning, [Stephen Chew and Mary Brave], the table
returns all the five members whose Class Name contains the word [Grade]. How would I able to return
only the [Grade 3] students using the search box. Thank you in advance.
First you need to disable the default search handler and attach your own. Then inside your search function you need to use search() and set regex to true and smart search to false so that it only returns exact matches.
$('.dataTables_filter input', dt.table().container())
.off('.DT')
.on('keyup.DT cut.DT paste.DT input.DT search.DT', function(e) {
table.search(`^${this.value}$`, true, false).draw();
}
Where table is a JQuery reference to your DataTable. E.g.
const table = $('#tableId').DataTable();

How can I cap my result to 1 with importXML

Using importXML I am searching for the class 'current' which contains 'current-price'.
Using this code it returns the same result 3 times as the 'current' class exists 3 times on the website.
=importxml("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/lg-55sm8200pla-55-smart-4k-ultra-hd-hdr-led-tv-with-google-assistant-10191769-pdt.html", "//div//strong[contains(#class, 'current')]")
Is there a simple way to cap my search to 1 result so that it doesn't overlap data on the row below?
sure, you can use INDEX like:
=INDEX(IMPORTXML("https://www.currys.co.uk/gbuk/tv-and-home-entertainment/televisions/televisions/lg-55sm8200pla-55-smart-4k-ultra-hd-hdr-led-tv-with-google-assistant-10191769-pdt.html",
"//div//strong[contains(#class, 'current')]"), 1, 1)

Can i display different static block for different products?

I want to know if we are able to select different for different products.
So, i can not place for product x only the field 1 and 2 ("30 Tage.." und kostenfreie...") of static block and for product y the field 3 and 4 of static block? Please Help Me
You can add a attribute with text field & set name as product_block_identfier, Insert the static block identifier value in this field.
Now call static block on view.phtml file
getLayout()->createBlock('cms/block')->setBlockId($_product->getProductBlockIdentfier())->toHtml() ?>
$_product->getProductBlockIdentfier() this will get the block id from the product attribute product_block_identfier
Hope this will work for you!

Resources