Solved

Xpath - search in the same node

Posted on 2014-01-15
10
326 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
Space-Age Communications Transitions to DevOps

ViaSat, a global provider of satellite and wireless communications, securely connects businesses, governments, and organizations to the Internet. Learn how ViaSat’s Network Solutions Engineer, drove the transition from a traditional network support to a DevOps-centric model.

 
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

Is Your DevOps Pipeline Leaking?

Is your CI/CD pipeline a hodge-podge of randomly connected tools? You’ve likely got a tool to fix one problem & then a different tool to fix another, resulting in a cluster of tools with overlapping functionality. Learn how to optimize your pipeline with Gartner's recommendations

Question has a verified solution.

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

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.
The Windows functions GetTickCount and timeGetTime retrieve the number of milliseconds since the system was started. However, the value is stored in a DWORD, which means that it wraps around to zero every 49.7 days. This article shows how to solve t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

738 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