How to check whether the substring is present in a string in Nunjucks Template - nunjucks

If we have a string like "This is High Price" how can I check whether "high" is present in that string or not in Nunjucks Template

Thanks to a GitHub issue, discovered in works like so: "high" in "This is High Price"
Note: This is case-sensitive search.

Related

find xpath of label with non breaking space

I am trying to get xpath for following html code, but nothing seems to be working. I appreciate your suggestion. I need to get the xpath based on text.
<label class="label" securityidpath="ACCOUNTS_FS.PART_ACCOUNT_HEADER_FS.PART_ACCOUNT_STATUS" title="Part Account Status">Part Account Status:
</label>
FYI, I tried following variant xpath
//label[normalize-space(text())='Part Account Status:\u00a0']
//label[normalize-space(text())='Part Account Status:\u000a']
//label[normalize-space(text())='Part Account Status:\u202f']
and all the options as per following url https://en.wikipedia.org/wiki/Whitespace_character
Thank You,
Yougander
You can use
/label[normalize-space(text())='Part Account Status: ']
Or use the hexadecimal variant   instead of the decimal  .
Also note that XPath uses slashes (and not backslashes) to define paths, so referencing the root node label would be done by /label.
The typo lable instead of label is trivial.
BTW you can avoid the trouble with the entity by using the title attribute in your XPath:
label[normalize-space(#title)='Part Account Status']
your element name is wrong and change UTF character name as HexaDecimal entity values:
//label[normalize-space(text())='Part Account Status: ']
XPATH require forward slash

Amazon Cloudsearch not searching with partial string

I'm testing Amazon Cloudsearch for my web application and i'm running into some strange issues.
I have the following domain indexes: name, email, id.
For example, I have data such as: John Doe, John#example.com, 1
When I search for jo I get nothing. If I search for joh I still get nothing, But if I search for john then I get the above document as a hit. Why is it not getting when I put partial strings? I even put suggestors on name and email with fuzzy matching enabled. Is there something else i'm missing? I read the below on this:
http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching-text.html
http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching.html
http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching-compound-queries.html
I'm doing the searches using boto as well as with the form on AWS page.
What you're trying to do -- finding "john" by searching "jo" -- is a called a prefix search.
You can accomplish this either by searching
(prefix field=name 'jo')
or
q=jo*
Note that if you use the q=jo* method of appending * to all your queries, you may want to do something like q=jo* |jo because john* will not match john.
This can seem a little confusing but imagine if google gave back results for prefix matches: if you searched for tort and got back a mess of results about tortoises and torture instead of tort (a legal term), you would be very confused (and frustrated).
A suggester is also a viable approach but that's going to give you back suggestions (like john, jordan and jostle rather than results) that you would then need to search for; it does not return matching documents to you.
See "Searching for Prefixes in Amazon CloudSearch" at http://docs.aws.amazon.com/cloudsearch/latest/developerguide/searching-text.html
Are your index field types "Text"? If they are just "Literals", they have to be an exact match.
I think you must have your name and email fields set as the literal type instead of the text type, otherwise a simple text search of 'jo' or 'Joh' should've found the example document.
While using a prefix search may have solved your problem (and that makes sense if the fields are set as the literal type), the accepted answer isn't really correct. The notion that it's "like a google search" isn't based on anything in the documentation. It actually contradicts the example they use, and in general muddies up what's possible with the service. From the docs:
When you search text and text-array fields for individual terms, Amazon CloudSearch finds all documents that contain the search terms anywhere within the specified field, in any order. For example, in the sample movie data, the title field is configured as a text field. If you search the title field for star, you will find all of the movies that contain star anywhere in the title field, such as star, star wars, and a star is born. This differs from searching literal fields, where the field value must be identical to the search string to be considered a match.

XPATH Search and replace full words

Strange I can't find this information online, does anyone know how to search and replace a full word for something else?
For example:
<div class="cell_user" alt="hotel Review - Young couple">
I want to remove: "hotel Review - " so I am left with:
"Young couple"
It seems to be something to do with fn:replace but I cannot find a single example anywhere of someone using it!
Thanks for any advice
If you have a constant prefix or infix string as in your example you can always use a string function such as substring-after():
"substring-after(//div[#class = 'cell_user']/#alt, 'hotel Review - ')"
You may want to tweak the beginning of the XPath expression a little bit to be more selective depending on your context. Also see xsl substring-after usage.
If you know that the relevant part is always after a hyphen and there is always only a single hyphen in your string you may even write
"substring-after(//div[#class = 'cell_user']/#alt, '- ')"

Trouble using Xpath "starts with" to parse xhtml

I'm trying to parse a webpage to get posts from a forum.
The start of each message starts with the following format
<div id="post_message_somenumber">
and I only want to get the first one
I tried xpath='//div[starts-with(#id, '"post_message_')]' in yql without success
I'm still learning this, anyone have suggestions
I think I have a solution that does not require dealing with namespaces.
Here is one that selects all matching div's:
//div[#id[starts-with(.,"post_message")]]
But you said you wanted just the "first one" (I assume you mean the first "hit" in the whole page?). Here is a slight modification that selects just the first matching result:
(//div[#id[starts-with(.,"post_message")]])[1]
These use the dot to represent the id's value within the starts-with() function. You may have to escape special characters in your language.
It works great for me in PowerShell:
# Load a sample xml document
$xml = [xml]'<root><div id="post_message_somenumber"/><div id="not_post_message"/><div id="post_message_somenumber2"/></root>'
# Run the xpath selection of all matching div's
$xml.selectnodes('//div[#id[starts-with(.,"post_message")]]')
Result:
id
--
post_message_somenumber
post_message_somenumber2
Or, for just the first match:
# Run the xpath selection of the first matching div
$xml.selectnodes('(//div[#id[starts-with(.,"post_message")]])[1]')
Result:
id
--
post_message_somenumber
I tried xpath='//div[starts-with(#id,
'"post_message_')]' in yql without
success I'm still learning this,
anyone have suggestions
If the problem isn't due to the many nested apostrophes and the unclosed double-quote, then the most likely cause (we can only guess without being shown the XML document) is that a default namespace is used.
Specifying names of elements that are in a default namespace is the most FAQ in XPath. If you search for "XPath default namespace" in SO or on the internet, you'll find many sources with the correct solution.
Generally, a special method must be called that binds a prefix (say "x:") to the default namespace. Then, in the XPath expression every element name "someName" must be replaced by "x:someName.
Here is a good answer how to do this in C#.
Read the documentation of your language/xpath-engine how something similar should be done in your specific environment.
#FindBy(xpath = "//div[starts-with(#id,'expiredUserDetails') and contains(text(), 'Details')]")
private WebElementFacade ListOfExpiredUsersDetails;
This one gives a list of all elements on the page that share an ID of expiredUserDetails and also contains the text or the element Details

Invalid Token when using XPath

I am making a modification to a web application using XPath, and when executed I get an error message - Invalid token!
This is basic what I am doing
public xmlNode GetSelection (SelectParams params, xmldocument docment)
{
xpathstring = string.format("Name =\'{0}' Displaytag = \'{1}' Manadatory=\'{2}', params.Name, params.Displaytag, params.Manadatory);
return document.selectsinglenode(xpathstring);
}
As you can see, I am making a string and setting values on the nodes I am trying to find against my xml document, and thus returning xml data that matches my parameters.
What is happening is that I am getting an xpathexeception error in Visual Studio and it says invalid token.
I do know that in the xml document that the parameters I am looking in the tags have double quotes, for example, Name="ABC". So, I thought the problem could be solved using an "\".
Can anyone help?
Update from comments
In the Xml Document, the tag has
attributes where they are set as
Name="ABC" Displaytag="ATag"
Manadatory="true".
I guess you need:
//*[#Name="ABC"][#Displaytag="ATag"][#Manadatory="true"]
Or
//*[#Name="ABC" and #Displaytag="ATag" and #Manadatory="true"]
Meaning: any element in the whole document having a Name attribute with "ABC" value, a Displaytag attribute with "ATag" value and a Manadatory attribute with "true" value.
The string passed as argument to SelectSingleNode() (BTW, the exact capitalization is important) is something like:
Name ='someName' Displaytag = 'someString' Manadatory='true'
This is extremely different than a syntactically legal XPath expression.
And the error message just reflects the fact that toxic food has been given to the XPath engine.
Solution: Do read at least a light XPath tutorial and then specify a correct XPath expression.
The string you are constructing is not a valid XPath expression. In fact, it is nothing like XPath at all.
Indeed, even if it were a valid XPath expression, constructing it this way by string concatenation is a very dangerous practice, because of the possibility of injection attacks. But I suspect that advice will fall on stony ground.

Resources