Solved

Xpath - search in the same node

Posted on 2014-01-15
10
315 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
Comment Utility
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
Comment Utility
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
Comment Utility
//Object[Fields/Field[@name='Filename']/@value='193478618937468178.000']/@id
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 1

Author Comment

by:bounty457
Comment Utility
Great, Thank you very much. It works fine.

Regards Bounty
0
 
LVL 60

Expert Comment

by:Geert Bormans
Comment Utility
welcome
0
 
LVL 6

Expert Comment

by:Mahesh Bhutkar
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks..
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

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…
Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
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…

743 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now