Need help in Xpath 1.0 and 2.0 Expression using concat - xpath

I'm pulling up images dynamically based on EmployerName field using the below function.
concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")
It works fine.
But, If the EmployerName is wrong or its empty I should display a default image.
How can we do that using both Xpath 1.0 & 2.0
Any help will be thankful...

There's a rather bizarre solution for XPath 1.0 like this:
substring(S, 1, string-length(S) * number(C))
where S is a string and C is a boolean condition. If C is false, number(C) is 0, so nothing is output. If C is true, number(C) is 1, so the whole string is output. So the effect is "if condition C is true then output S else output nothing"; and you can combine this with another expression using the inverse condition to output different strings based on the value of the condition.
Or you could move to XPath 2.0, where life is much more boring.

In XPath 2.0, you could also do following:
concat("C:\Projects\GlobalResourceSet\Images\",
($EmployerName[. != ''], 'default')[1],
"_banner_2013.png"
)
This puts both the employer name (if set) and the "default image name" in a sequence and then selects the first of both.
Newlines added for readability, can be removed arbitrarily.

In xpath 2.0 this could work
if ($EmployerName = "") then "C:\Projects\GlobalResourceSet\Images\default.png" else concat("C:\Projects\GlobalResourceSet\Images\",$EmployerName,"_banner_2013.png")

Related

Conditional XPath on multiple elements

I am trying to use xpath to set an exchange property from the value of one element, depending on what the value is in another element...
So in my case i want an xpath to identify the PartyID where the RoleCode = 60. And another xpath expression to find the PartyID for where the RoleCode = Z60.
I would have used [1] etc but the send cannot guarantee the order the segments will be in.
Help is much appreciated!
<RegisteredProductCollection>
<RegisteredProduct>
<Status>1</Status>
<AddressLine2>test</AddressLine2>
<AddressLine1>37</AddressLine1>
<RegisteredProductPartyInformation>
<RegisteredProductPartyInformation>
<PartyID>9000028253</PartyID>
<RoleCode>60</RoleCode>
</RegisteredProductPartyInformation>
<RegisteredProductPartyInformation>
<PartyID>1288219</PartyID>
<RoleCode>Z60</RoleCode>
</RegisteredProductPartyInformation>
</RegisteredProductPartyInformation>
<PostalCode>3200</PostalCode>
<Country>NZ</Country>
<ProductID>G02411</ProductID>
<SerialID>1234124124</SerialID>
<City>test</City>
<ReferenceDate>20200514000000</ReferenceDate>
<District>wai</District>
</RegisteredProduct>
</RegisteredProductCollection>
You can use these XPath-1.0 expressions:
//RegisteredProductPartyInformation[RoleCode='60']/PartyID
and
//RegisteredProductPartyInformation[RoleCode='Z60']/PartyID
Change the ' to " if necessary.

Can I condense an XPath expression that checks for conditional values of an attribute?

I have the following XML payload:
<fizz>
<buzz class="foo">
<whatever/>
</buzz>
</fizz>
The value of the /fizz/buzz[#class]/#class attribute can be foo, bar or whistlefeather. I'm trying to write an efficient XPath expression that covers all three scenarios. The best I have is:
/fizz/buzz[#class]/#class = 'foo' |
/fizz/buzz[#class]/#class = 'bar' |
/fizz/buzz[#class]/#class = 'whistlefeather'
Is there some "shorthand" way to make this more condense/efficient (less verbose)?
Using this (all xpath version) :
/fizz/buzz[#class='foo' or #class='bar' or #class='whistlefeather']
Using xpath >=2 :
/fizz/buzz[#class=("foo", "bar", "whistlefeather")]
Correcting the answer from #GillesQuenot:
Using any XPath version:
/fizz/buzz[#class='foo' or #class='bar' or #class='whistlefeather']
Using XPath 2.0 or later:
/fizz/buzz[#class=("foo", "bar", "whistlefeather")]
(Note, this returns the selected buzz elements. It's unclear what you actually want the expression to return.)

using an expression to show or hide rectangle based on a parameter value (SSRS)

Trying to show/hide a couple of rectangles in SSRS based on an expression which uses the value of a parameter in the report. See the screenshots for more details. When the '-Cover pages' label is picked I want it to display the rectangle but I consistently get the following errors. It can't seem to convert and read the parameter expression no matter what I do.
The expression I'm trying to use is:
=iif(Parameters!specparam.Value="-Cover Pages",true,false)
It looks like the label of your parameter is what you're looking for based on the image provided and your expression. Try to switch instead to:
=IIF(Parameters!specparam.Label="-Cover Pages",TRUE,FALSE)
(Note: I switched specparam.Value to specparam.Label.
Your comment is very close. Apply this expression to the Hidden property of the rectangle:
=IIF( Parameters!specparam.Label.Equals("-Cover Pages"), FALSE, TRUE )
You'll notice I have switched around the FALSE and TRUE as you don't want the rectangle to hide when the parameter matches.
Edit:
As you're dealing with a multivalue parameter, you can use a combination of Array.IndexOf and Split to check if your value is one of the selected parameters.
Apply this expression to the Hidden property of your rectangle:
=IIF( Array.IndexOf( Split( Parameters!specparam.Value, "," ), "-Cover Pages" ) > -1, FALSE, TRUE )

How to use like in XPath?

I have a page that searches with filters. I have this code for example,
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[#LastName != '"+txtSearch.value+"']");
xmlTempResultSearch.removeAll();
This selects the data that is not equal to the LastName inputted on the txtSearch textbox and then removes them from the result set so that its filtered to equal the last name on the txtSearch textbox.
My problem with this code is that it should be equal (=) to the txtSearch.value, what I want is that I want the result set LIKE the txtSearch.value. What happens on my page is that when I type 'santos' on the txtSearch textbox, its result set is all those last names with 'santos'. But when I type 'sant', nothing appears. I want the same result set with 'santos' because it all contains 'sant'
You can use all of the XPath (1.0) string functions. If you have XPath 2.0 available, then you can even use RegEx.
contains()
starts-with()
substring()
substring-before()
substring-after()
concat()
translate()
string-length()
There is no **ends-with() in XPath 1.0, but it can easily be expressed with this XPath 1.0 expression**:
substring($s, string-length($s) - string-length($t) +1) = $t
is true() exactly when the string $s ends with the string $t.
You can use start-with function and not function. Reference:
http://www.w3schools.com/xpath/xpath_functions.asp
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[not(starts-with(#LastName,'"+ txtSearch.value +"'))]");
you can use contains() function of XPath:
xmlTempResultSearch = xmlResidentListDisplay.selectNodes("//PeopleList/Row[not(contains(#LastName,'"+txtSearch.value+"'))]");

Selenium Webdriver + Ruby regex: Can I use regex with find_element?

I am trying to click an element that changes per each order like so
edit_div_123
edit_div_124
edit_div_xxx
xxx = any three numbers
I have tried using regex like so:
#driver.find_element(:css, "#edit_order_#{\d*} > div.submit > button[name=\"commit\"]").click
#driver.find_element(:xpath, "//*[(#id = "edit_order_#{\d*}")]//button").click
Is this possible? Any other ways of doing this?
You cannot use Regexp, like the other answers have indicated.
Instead, you can use a nifty CSS Selector trick:
#driver.find_element(:css, "[id^=\"edit_order_\"] > div.submit > button[name=\"commit\"]").click
Using:
^= indicates to find the element with the value beginning with your criteria.
*= says the criteria should be found anywhere within the element's value
$= indicates to find the element with with your criteria at the end of the value.
~= allows you to find the element based on a single criteria when the actual value has multiple space-seperated list of values.
Take a look at http://net.tutsplus.com/tutorials/html-css-techniques/the-30-css-selectors-you-must-memorize/ for some more info on other neat CSS tricks you should add to your utility belt!
You have no provided any html fragment that you are working on. Hence my answer is just based on the limited inputs provided your question.
I don't think WebDriver APIs support regex for locating elements. However, you can achieve what you want using just plain XPath as follows:
//*[starts-with(#id, 'edit_div_')]//button
Explanation: Above xpath will try to search all <button> nodes present under all elements whose id attribute starts with string edit_div_
In short, you can use starts-with() xpath function in order to match element with id format as edit_div_ followed by any number of characters
No, you can not.
But you should do something like this:
function hasClass(element, className) {
var re = new RegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
return re.test(element.className);
}
This worked for me
#driver.find_element(:xpath, "//a[contains(#href, 'person')]").click

Resources