0

How to write xpath to get matching node based on the node's attributes and childs (if any) attributes

single xml node:

<Nokia_3220.gif jcr:primaryType="nt:file" jcr:uuid="90ff297d-7508-4cc8-8302-71b6e75fb8ba" 
                jcr:created="2014-01-03T04:51:24.377-08:00" jcr:createdBy="admin">
    <jcr:content jcr:primaryType="nt:unstructured" country="India" firstName="Supplier" 
                 jcr:data="FBAAOw==" jcr:lastModified="2014-01-03T18:21:11.216+05:30" 
                 jcr:mimeType="image/gif" lastName="Image1" phone="988-777-5551" 
                 state="WB" status="Active"/>
</Nokia_3220.gif>

what would be the xpath query for jcr:uuid and state and firstName and jcr:created all are like search ?

I tried :

string xpath = "//Nokia_3220.gif[contains(translate(@jcr:uuid, 
                                       'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                       'abcdefghijklmnopqrstuvwxyz'),
                                    '90ff297d-7508-4cc8-8302-71b6e75fb8ba')
                and contains(translate(@state, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',   
                                          'abcdefghijklmnopqrstuvwxyz'),'W')
                and contains(translate(@firstName, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ',                       
                                    'abcdefghijklmnopqrstuvwxyz'),'pplie')]"
xmlnodelist list = objDoc.selectnodes(xpath,ns);

my problem is I can't predict whether a particular attribute belongs to a node itself or it belongs to its child.

How to write xpath to get the node irrespective of the attribute attached to the node or its child, provided the name of the element which contains the attribute for sure?

1 Answer 1

0

Please give this a try:

string xpath = @"//Nokia_3220.gif[
                  contains(translate((. | *)/@jcr:uuid, 
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                     'abcdefghijklmnopqrstuvwxyz'),
                           '90ff297d-7508-4cc8-8302-71b6e75fb8ba') and 
                  contains(translate((. | *)/@state, 
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ',   
                                     'abcdefghijklmnopqrstuvwxyz'),
                           'w') and 
                  contains(translate((. | *)/@firstName,  
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ',            
                                     'abcdefghijklmnopqrstuvwxyz'),
                           'pplie')]";

Note that this makes the assumption that the attributes being inspected do not occur multiple times among a given element and its children. Is that sufficient for your needs?

Here is a version that will allow you to check for a name that matches a certain value:

string xpath = @"//*
                 [contains(local-name(), '3220')]
                 [contains(translate((. | *)/@jcr:uuid, 
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 
                                     'abcdefghijklmnopqrstuvwxyz'),
                           '90ff297d-7508-4cc8-8302-71b6e75fb8ba') and 
                  contains(translate((. | *)/@state, 
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ',   
                                     'abcdefghijklmnopqrstuvwxyz'),
                           'w') and 
                  contains(translate((. | *)/@firstName,  
                                     'ABCDEFGHIJKLMNOPQRSTUVWXYZ',            
                                     'abcdefghijklmnopqrstuvwxyz'),
                           'pplie')]";
Sign up to request clarification or add additional context in comments.

6 Comments

hey Rishe, Thanks for ur answer.Is it possible to make contains search for element name too?
hey Rishe, Thanks for ur answer.Is it possible to make contains search for element name too?I mean Root/*[contains(translate(local-name(), 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', 'abcdefghijklmnopqrstuvwxyz'), '3220')[search for attribute both in element and its child]. I mean, part or full name of element is given, I need a search something like this, where search with provided attribute values in a element that matches or contains given part of element name,such that, only element(s) that matched the given search criteria, should be be result set.
//Nokia_3220.gif[contains(translate((. | *)/@jcr:uuid,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'863a9') and contains(translate((. | *)/@jcr:mimeType,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'gif') and contains(translate((. | *)/@lastName,'ABCDEFGHIJKLMNOPQRSTUVWXYZ','abcdefghijklmnopqrstuvwxyz'),'image')]
is xpath for the xml node under, yield 0 results. <Nokia_3120.gif jcr:primaryType="nt:file" jcr:uuid="9c921bf7-d4c6-4db2-9b6b-cf894db863a9" jcr:created="2014-01-03T04:50:11.080-08:00" jcr:createdBy="admin"> <jcr:content jcr:primaryType="nt:unstructured" status="Active" phone="988-777-5550" firstName="Supplier" state="Andhra Pradesh" lastName="Image" jcr:mimeType="image/gif" jcr:lastModified="2014-01-03T18:19:49.824+05:30" jcr:data="AUEADs=" country="India" />
this yields 0 results, though one element as shown above do exists
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.