Xpath查询根据元素和子元素的属性选择元素及其子元素

时间:2014-01-08 04:29:31

标签: c# asp.net xml xpath

如何编写xpath以根据节点的属性和子节点(如果有)获取匹配节点

单个xml节点:

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

什么是jcr的xpath查询:uuid和state以及firstName和jcr:created all都像搜索?

我试过了:

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);

我的问题是我无法预测特定属性是属于某个节点本身还是属于其子节点。

如果必须使用包含该属性的元素的名称,如何编写xpath来获取节点而不管附加到节点或其子节点的属性是什么?

1 个答案:

答案 0 :(得分:0)

请试一试:

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')]";

请注意,这假设被检查的属性在给定元素及其子元素中不会多次出现。这足以满足您的需求吗?

这是一个允许您检查与某个值匹配的名称的版本:

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')]";