How to use starts-with() , contains() and ends-with() in XPath to find the xml node innertext? in XPATH 1.0

One possible way: //Heading[starts-with(., ‘Ethical’) and ends-with(., ‘consent’)] The ends-with() function is XPath 2.0. In XPath 1.0, it can be replaced using substring() and string-length(). Here is the equivalent XPath 1.0 (wrapped for readability): //Heading[ starts-with(., ‘Ethical’) and ‘consent’ = substring(., string-length(.) – string-length(‘consent’) +1) ]

selenium – Failed to execute ‘evaluate’ on ‘Document’: The string is not a valid XPath expression

This error message… SyntaxError: Failed to execute ‘evaluate’ on ‘Document’: The string ‘//span[contains(@class, ‘md_countryName_fdxiah8’ and text(), ‘Colombia’)]’ is not a valid XPath expression. …implies that the XPath which you have used was not a valid XPath expression. Seems you were pretty close. You can use either of the following Locator Strategies: Using xpath 1: country … Read more

Encoding XPath Expressions with both single and double quotes

Wow, you all sure are making this complicated. Why not just do this? public static string XpathExpression(string value) { if (!value.Contains(“‘”)) return ‘\” + value + ‘\”; else if (!value.Contains(“\””)) return ‘”‘ + value + ‘”‘; else return “concat(‘” + value.Replace(“‘”, “‘,\”‘\”,'”) + “‘)”; } .NET Fiddle & test

How to handle double quotes in string before XPath evaluation?

PHP has Xpath 1.0, if you have a string with double and single quotes, a workaround is using the Xpath concat() function. A helper function can decide when to use what. Example/Usage: xpath_string(‘I lowe “double” quotes.’); // xpath: ‘I lowe “double” quotes.’ xpath_string(‘It\’s my life.’); // xpath: “It’s my life.” xpath_string(‘Say: “Hello\’sen”.’); // xpath: concat(‘Say: … Read more

How can I use XPath to find the minimum value of an attribute in a set of elements?

Here’s a slightly shorter solution. Maximum: /foo/bar/@score[not(. < ../../bar/@score)][1] Minimum: /foo/bar/@score[not(. > ../../bar/@score)][1] I’ve edited the predicate so that it’s applicable to any sequence of bar, even if you decide to change the path. Note that parent of attribute is the element to which it belongs. If embedding these queries in XML files like XSLT … Read more

How to pass variable parameter into XPath expression?

Given that you’re using the Axiom XPath library, which in turn uses Jaxen, you’ll need to follow the following three steps to do this in a thoroughly robust manner: Create a SimpleVariableContext, and call context.setVariableValue(“val”, “value1”) to assign a value to that variable. On your BaseXPath object, call .setVariableContext() to pass in the context you … Read more

XSLT string replace

replace isn’t available for XSLT 1.0. Codesling has a template for string-replace you can use as a substitute for the function: <xsl:template name=”string-replace-all”> <xsl:param name=”text” /> <xsl:param name=”replace” /> <xsl:param name=”by” /> <xsl:choose> <xsl:when test=”$text=”” or $replace=””or not($replace)” > <!– Prevent this routine from hanging –> <xsl:value-of select=”$text” /> </xsl:when> <xsl:when test=”contains($text, $replace)”> <xsl:value-of select=”substring-before($text,$replace)” … Read more