EzDevInfo.com

xpath interview questions

Top xpath frequently asked interview questions

Getting the value of an attribute in XML

How would one get the value of attribute1 (blah) in the following xml using xslt:

<name attribute1="blah" attribute2="blahblah">
</name>

Source: (StackOverflow)

XPath: find link URL by link text

I have a well formed XHTML page. I want to find the destination URL of a link when I have the text that is linked.

Example

<a rel='nofollow' href="http://stackoverflow.com">programming questions site</a>
<a rel='nofollow' href="http://cnn.com">news</a>

I want an XPath expression such that if given programming questions site it will give http://stackoverflow.com and if I give it news it will give http://cnn.com.


Source: (StackOverflow)

Advertisements

How to use "not" in xpath?

I want to write something of the sort:

//a[not contains(@id, 'xx')]

(meaning all the links that there 'id' attribute doesn't contain the string 'xx')

I can't find the right syntax.


Source: (StackOverflow)

XPath: select text node

Having the following XML:

<node>Text1<subnode/>text2</node>

How do I select either the first or the second text node via XPath?

Something like this:

/node/text()[2]

of course doesn't work because it's the merged result of every text inside the node.


Source: (StackOverflow)

How can I convert a string to upper- or lower-case with XSLT?

How do you do case conversion in XSL?

<xsl:variable name="upper">UPPER CASE</xsl:variable>
<xsl:variable name="lower" select="???"/>

Source: (StackOverflow)

Is there a JSON equivalent of XQuery/XPath?

When searching for items in complex JSON arrays and hashes, like:

[
    { "id": 1, "name": "One", "objects": [
        { "id": 1, "name": "Response 1", "objects": [
            // etc.
        }]
    }
]

Is there some kind of query language I can used to find an item in [0].objects where id = 3?


Source: (StackOverflow)

Select values from XML field in SQL Server 2008

Just looking at my XML field, my rows look like this:

<person><firstName>Jon</firstName><lastName>Johnson</lastName></person>
<person><firstName>Kathy</firstName><lastName>Carter</lastName></person>
<person><firstName>Bob</firstName><lastName>Burns</lastName></person>

Note that these are three rows in my table.

I'd like to return a SQL result as a table as in

Jon  | Johnson
Kathy| Carter
Bob  | Burns

What query will accomplish this?


Source: (StackOverflow)

Is there a way to get element by Xpath using JavaScript in Selenium WebDriver?

I am looking for something like:

getElementByXpath(//html[1]/body[1]/div[1]).innerHTML

I need to get the innerHTML of elements using JS (to use that in Selenium WebDriver/Java, since WebDriver can't find it itself), but how?

I could use Id attribute, but not all elements have Id attribute.

[FIXED]

I am using jsoup to get it done in Java. That works for my needs. Thanks for the answers. :)


Source: (StackOverflow)

How to match attributes that contain a certain string?

I have the problem selecting nodes by attribute containing more than one word.

Example:

<div class="atag btag" />

This is my xpath expression: //*[@class='atag']

The expression only works with <div class="atag" /> but not for the above shown.

Any suggestions?


Source: (StackOverflow)

How to use xpath in Python?

Is there a full implementation? How is the library used, where is its website?


Source: (StackOverflow)

XPath and XSLT 2.0 for .NET? [closed]

.NET 3.5 doesn't completely support XPATH 2.0 or XSLT 2.0, which is just too bad. Does anyone know if these two will be included and fully supported in any future .NET versions?


Source: (StackOverflow)

Getting attribute using XPath

Given an XML structure like so:

<?xml version="1.0" encoding="ISO-8859-1"?>

<bookstore>

<book>
  <title lang="eng">Harry Potter</title>
  <price>29.99</price>
</book>

<book>
  <title lang="eng">Learning XML</title>
  <price>39.95</price>
</book>

</bookstore>

How could I get the value of lang (where lang is eng in book title), for the first element?


Source: (StackOverflow)

What is the correct XPath for choosing attributes that contain "foo"?

Given this XML, what XPath returns all elements whose prop attribute contains Foo (the first three nodes):

<bla>
 <a prop="Foo1"/>
 <a prop="Foo2"/>
 <a prop="3Foo"/>
 <a prop="Bar"/>
</bla>

Source: (StackOverflow)

How to read XML using XPath in Java

I want to read XML data using XPath in Java, so for the information I have gathered I am not able to parse XML according to my requirement.

here is what I want to do:

Get XML file from online via its URL, then use XPath to parse it, I want to create two methods in it. One is in which I enter a specific node attribute id, and I get all the child nodes as result, and second is suppose I just want to get a specific child node value only

<?xml version="1.0"?>
<howto>
  <topic name="Java">
      <url>http://www.rgagnonjavahowto.htm</url>
  <car>taxi</car>
  </topic>
  <topic name="PowerBuilder">
       <url>http://www.rgagnon/pbhowto.htm</url>
       <url>http://www.rgagnon/pbhowtonew.htm</url>
  </topic>
  <topic name="Javascript">
        <url>http://www.rgagnon/jshowto.htm</url>
  </topic>
 <topic name="VBScript">
       <url>http://www.rgagnon/vbshowto.htm</url>
 </topic>
 </howto>

In above example I want to read all the elements if I search via @name and also one function in which I just want the url from @name 'Javascript' only return one node element.

I hope I cleared my question :)

Thanks.

Kai


Source: (StackOverflow)

xpath find if node exists

Using a xpath query how do you find if a node (tag) exists at all?

For example if I needed to make sure a website page has the correct basic structure like /html/body and /html/head/title


Source: (StackOverflow)