?
Solved

Xpath - search in the same node

Posted on 2014-01-15
10
Medium Priority
?
329 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Amazon Web Services EC2 Cheat Sheet

AWS EC2 is a core part of AWS’s cloud platform, allowing users to spin up virtual machines for a variety of tasks; however, EC2’s offerings can be overwhelming. Learn the basics with our new AWS cheat sheet – this time on EC2!

 
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

Congratulations! You’re Certified – Now What?

Starting a new career can be overwhelming. Becoming certified in your field of expertise is a great start, but where do you go from here?  Here are some tips to help you on your career journey.

Question has a verified solution.

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

Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
Without even knowing it, most of us are using web applications on a daily basis.  In fact, Gmail and Yahoo email, Twitter, Facebook, and eBay are used by most of us daily—and they are web applications. We generally confuse these web applications to…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

718 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