Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Xpath - search in the same node

Posted on 2014-01-15
10
Medium Priority
?
330 Views
Last Modified: 2014-01-23
Hello everybody;

in the attached xml file i want to search the OBJECT_ID:

The following xpath command gives me the Filename back: 193478618937468178.000

//Object[@id='2657']/Fields/Field[@name='Filename']/text()

Open in new window

                                 

How can i select the OBJECT_ID in case i know the Filename: 193478618937468178.000 ?

/Fields/Field[@name='Filename'][@value='193478618937468178.000']/@id --> works not
                                 

Thank you very much
regards bounty
xpath4.xml
0
Comment
Question by:bounty457
  • 4
  • 4
  • 2
10 Comments
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39781960
You have to apply some logic like this,


For any Object @id,
      if Object/Fields/Field[@name='Filename'][@value='193478618937468178.000']
            then return that Object id

Checkout,
/*/Object@id/Fields/Field[@name='Filename'][@value='193478618937468178.000']
0
 
LVL 1

Author Comment

by:bounty457
ID: 39782035
this shows syntax error:  

/*/Object@id/Fields/Field[@name='Filename'][@value='193478618937468178.000']

Open in new window

0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39782427
//Object[Fields/Field[@name='Filename']/@value='193478618937468178.000']/@id
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39784568
This gives you perfectly...

In XPATH 1.0 you can write,
//Object[Fields/Field='193478618937468178.000'[@name='Filename']]/@id
This gives you node from where you can fetch value of id.


In XPATH 2.0 you can write,
//Object[Fields/Field='193478618937468178.000'[@name='Filename']]/@id/string()
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 2000 total points
ID: 39784718
it gives not perfectly, it has a syntax error

my mistake, I thought the filename was in an attribute not in the text field
It helps if you post an example XML so we can test
I now made atest XML based on the OP's first XPath

tested
//Object[Fields/Field[@name='Filename']='193478618937468178.000']/@id

the XSLT2 variant will hardly ever add value
0
 
LVL 1

Author Comment

by:bounty457
ID: 39785092
Great, Thank you very much. It works fine.

Regards Bounty
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39785219
welcome
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39802208
Didn't understand what's wrong with this,

In XPATH 1.0 you can write,
//Object[Fields/Field='193478618937468178.000'[@name='Filename']]/@id
This gives you node from where you can fetch value of id.

This has been tested at,
http://www.xpathtester.com/test
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 39802362
it is a pretty basic error actually
you are putting a predicate on a string, you need to do so on a node
the underlying data model of the xpathtester.com is wrong

you simply can't do this
/Field='193478618937468178.000'[@name='Filename']
the attribute is on the Field, so you need to do this instead
/Field[@name='Filename']='193478618937468178.000'

the closest to your solution that is valid would be something like this
//Object[Fields/Field[.='193478618937468178.000'][@name='Filename']]/@id
but I prefer mine

please don't use xpathtester if you want to be sure, it has obvious errors

For learning basic XPath1, this one is still a good reference
http://shop.oreilly.com/product/9780596002916.do
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
ID: 39802604
Thanks..
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses

916 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question