XPATH query to search for attributes containing a keyword

I am trying to find something in several attributes in several different nodes

This is how I find it in one attribute

//*[contains(@name,'KEYA')]

XML example:

<cars>

<car model="2000" name="Awesome KEYA Car" name2="somethine else">Brand1</car>
<car model="2005" name="Awesome Car" name2="KEYA something else">Brand 2</car>
<car name="Awesome Car" name2="somethine else">Brand1</car>
<car dontmatch="KEYA" name2="somethine else">Brand3333</car>

</cars>

What I really want is something like this for more than 10 attributes (it should only match the attributes listed in the white list),

//*[contains((@name or @name2 or @name3),'KEYA')]

Using XPATH 1.0. Any ideas on how to do this? I tried several methods, except for repetition, but did not work.

+3
source share
3 answers

Using

/*/*[@*
        [contains('|name|name1|name2|name3|name4|',
                  concat('|',name(),'|')
                  )
       and
         contains(., 'KEYA')
        ]
       ]

Here is a selection created by XPath Visualizer :

enter image description here

Explanation

, . (|) , , , ​​.

+3

/cars/car/@*[contains(.,'KEYA')]/parent::*

enter image description here

ok, ,

/cars/car/@*[name()!='dontmatch'][contains(.,'KEYA')]/parent::*

enter image description here

, ( ). Xpath attr, startswith() contains() - .

XML , LINQ-to-XML, .NET. . API.

+5

:

//*[contains(@name,'KEYA') or contains(@name2,'KEYA')]

Cheeso - :

/cars/car/@*[contains(.,'KEYA') and local-name() != 'dontmatch']/parent::*

XPath ( ), ( Cheeso).

Cheeso, :

//@*[contains(.,'KEYA') and not(contains('dontmatch,dontmatch2', local-name()))]/parent::*

:

//@*[contains(.,'KEYA') and contains('name,name2', local-name())]/parent::**
+1

All Articles