VTD-XML: XPath Statement - if(10 > 5) then "yes" else "no" - xpath

With VTD-XML, is it possible to execute the following XPath statement?
if(10 > 5) then "yes" else "no"`
I have a much more complex statement, but it will not run with my current setup. There are these methods possible: evalXPathToBoolean(), evalXPathToNumber(), and evalXpathToString(). But, I am receiving this error when trying to execute:
com.ximpleware.extended.XPathParseExceptionHuge: XPath Syntax error: #8
autoPilot.selectXPath(xpath) // does not like this
while((i=autoPilot.evalXPath())!=-1){
list.add(vtdNav.toString(i));
}
This is valid XPath and works in XMLSpy. Is it possible to do this with VTD-XML? Please advise.

VTD-XML supports full set of XPath 1.0. The syntax you alluded to is likely XPath 2.0 syntax... which is not supported by VTD-XML at this point.
The solution/work-around is to take your conditional statement and code it into your application logic, and only use VTD-XML XPath boolean/number evaluation capability to obtain the values , which you can then weave into your condition logic...

Related

string-join() function throws invalid expression

XPathError
message: "Invalid expression.
(string-join(/form/Data/MProcess/Process/Id/string(), ','))
I thought the problem was Process Id was not returning string so I tried string() and text() but they did not work. I can't find the solution.
Here is an example process id: aee865d1-7253-489c-8d53-2a0d580639d0
string-join() is an XPath 2.0 function, and many early XPath processors were never upgraded to 2.0. Check what XPath processor you are using and what language level it supports, and upgrade if you can.

Watir Check Text exists

I get it not the Watir in conjunction with rspec find my text.
The following code leads to this error.
Code:browser.text.include?("Coverberechnung").should == true
Error1: expected: true got: false (using ==)
Error2: Using should from rspec-expectations' old :should syntax
without explicitly enabling the syntax is deprecated. Use the new
:expect syntax or explicitly enable :should instead. Called from
Maybe I can have a help
URL for the Site: enter link description here
You're looking for an initially-capitalized string (i.e. Coverberechnung), but that string is all-capitalized on the test site (i.e. COVERBERECHNUNG).
Try:
browser.text.include?("COVERBERECHNUNG").should == true
or (using expect syntax)
expect(browser.text.include?("COVERBERECHNUNG")).to be true
I prefer to use
expect(browser.text).to include('coverberechnung')
If I wanted to be indifferent about case I would do something like this:
browser.text.upcase.include?("COVERBERECHNUNG").should == true
or
browser.text.downcase.include?("coverberechnung").should == true
this way you can avoid text comparison that may have varying cases.
also for you last problem #3:
use
expect(browser.text.downcase.include?("coverberechnung")).to be true
they deprecated that version some time ago. so you can give this one a try with no issue.
NOTE: only one caveat is that this will ignore case. As described above.
Or you can just do the following:
fail unless #browser.text.include? 'COVERBERECHNUNG'
Or if you want to target that exact string, you could do the following instead:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present
This code will raise an exception after 30 seconds (thus, failing your test in the process) if it can't find a header element with the text: 'COVERBERECHNUNG'. You can also override the waiting or polling process by doing the following:
#browser.h1(text: 'COVERBERECHNUNG').wait_until_present(10)
That code will check that h1 element within 10 seconds.

Using XPath functions in libxml

I am trying to evaluate certain XPath expressions with libxml 2.8.0 and am getting error messages with functions. It is not clear to me whether libxml implements the whole or a subset of the XPath specification. Is there any resource clearly identifying what is implemented and how to use it?
In my specific case, one expression I am trying to evaluate is:
/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./#actionA,'cook') and fn:exists(./IngredientA[fn:contains(./#classes,'onion')])])]
I am using libxml through the XML::LibXML Perl module, but I have also tried my expression with the xpath1.c example tool available from xmlsoft.org, and got the same error messages:
$ ./xpath-tester data/fk.xml "/TemporalInformation/RecipeInformation[fn:exists(.> /ActionToActionRelation[fn:contains(./#actionA,'cook') and fn:exists(./IngredientA[fn:contains(./#classes,'onion')])])]" "fn=http://www.w3.org/2005/xpath-functions"
xmlXPathCompOpEval: function contains not found
XPath error : Unregistered function
xmlXPathCompOpEval: function exists not found
XPath error : Unregistered function
XPath error : Invalid expression
XPath error : Stack usage errror
Error: unable to evaluate xpath expression "/TemporalInformation/RecipeInformation[fn:exists(./ActionToActionRelation[fn:contains(./#actionA,'cook') and fn:exists(./IngredientA[fn:contains(./#classes,'onion')])])]"
Usage: ./xpath-tester <xml-file> <xpath-expr> [<known-ns-list>]
where <known-ns-list> is a list of known namespaces
in "<prefix1>=<href1> <prefix2>=href2> ..." format
I have tried with and without the fn namespace, both with xpath.c and my Perl script, and got the same result.
The libxml XPath implementation is only XPath 1.0, so 2.0-only functions such as exists aren't available. In 1.0 you only have a few core functions - there's starts-with but not ends-with, no regular expression support, no date handling, etc. and there's no strong typing and none of the higher-level constructs like if and for.
So to answer your question, yes, it does support the whole of XPath, but only XPath 1.0.

How do I verify an xPath expression in Selenium IDE?

How would I test for the following expression in Selenium?
not(//select[#id='ddlCountry']/#class) or
not(contains(//select[#id='ddlCountry']/#class,'invalidValue'))
true if the class attribute doesnt exist, or if it does, the attribute doesn't contain invalidValue.
I've tried using the verifyElementPresent command, but it errors out, I assume because I'm returning a boolean rather than a node.
I'm happy with an alternative to this if theres no way to do the above using xPath.
In case your XPath engine API doesnt allow expressions returning atomic values (not nodes), then you still can
Use:
//select[#id='ddlCountry'][contains(#class,'invalidValue')]
and test if an element was selected or not.
true if the class attribute doesnt
exist, or if it does, the attribute
doesn't contain invalidValue.
not(//select[#id='ddlCountry']/#class[contains(.,'invalidValue')])

Explain xpath and xquery in simple terms

I am new to programming. I know what XML is. Can anyone please explain in simple terms what xpath and xquery do Where are they used?
XPath is a way of locating specific elements in an XML tree.
For instance, given the following structure:
<myfarm>
<animal type="dog">
<name>Fido</name>
<color>Black</color>
</animal>
<animal type="cat">
<name>Mitsy</name>
<color>Orange</color>
</animal>
</myfarm>
XPath allows you to traverse the structure, such as:
/myfarm/animal[#type="dog"]/name/text()
which would give you "Fido"
XQuery is an XML query language that makes use of XPath to query XML structures. However it also allows for functions to be defined and called, as well as complex querying of data structures using FLWOR expressions. FLWOR allows for join functionality between data sets defined in XML.
FLWOR article from wikipedia
Sample XQuery (using some XPath) is:
declare function local:toggle-boolean($b as xs:string)
as xs:string
{
if ($b = "Yes") then "true"
else if ($b = "No") then "false"
else if ($b = "true") then "Yes"
else if ($b = "false") then "No"
else "[ERROR] # local:toggle-boolean"
};
<ResultXML>
<ChangeTrue>{ local:toggle-boolean(doc("file.xml")/article[#id="1"]/text()) }</ChangeTrue>
<ChangeNo>{ local:toggle-boolean(doc("file.xml")/article[#id="2"]/text()) }</ChangeNo>
</ResultXML>
XPath is a simple query language which serves to search in XML DOM. I think that it can be compared to SQL Select statements with databases. XPath can evaluate many programs which work with XML and has a mass usage. I recommend u to learn it.
XQuery is much more powerful and complicated it also offers many options how to transform result, it offers cycles etc. But also it is query language. It is also used as query language into XML databases. I think that this language has only specific usage and probably is not necessary to know it, in the beginning there will be enough if u know that it exists and what it can
There is simple explanation I hope that it is enough and understandable

Resources