Solved

Xpath - search in the same node

Posted on 2014-01-15
10
322 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Preface This article introduces an authentication and authorization system for a website.  It is understood by the author and the project contributors that there is no such thing as a "one size fits all" system.  That being said, there is a certa…
SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn the benefit of using external CSS files and the relationship between class and ID selectors. Create your external css file by saving it as style.css then set up your style tags: (CODE) Reference the nav tag and set your prop…

773 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