Dmytro Danilenkov
Dmytro Danilenkov

Reputation: 324

How I can find any element in XML by matching via pattern attribute and attribute value

I want to find elements in XML with attribute which contains in naming login or username and attribute value contains more then 15 chars. Probably for first case I need to apply pattern

.*(login|username).*

and for second

^.{15,}$

I'm really trying to cover such cases in more common way. I need to find such kind of rows in XML.

<props>
    <prop key="foo.bar.login">long_value_with_more_than_15_chars</prop>
    <prop name="username.foo.bar" value="long_value__with_more_than_15_chars"/>
    <prop foo.login.bar="long_value_with_more_than_15_chars"/>
</props>

Length of foo.bar.login, username.foo.bar and foo.login.bar doesn't matter. So for this purposes I need to use XPath but I cannot understand how to apply patterns.

Upvotes: 1

Views: 438

Answers (3)

uL1
uL1

Reputation: 2167

Don't edit the question from one to another given info. Please clarify your expectations.

Xpath 2.0:

//*[@*[matches(name(),"login|username") or matches(., "login|username")]][(@value and string-length(@value) gt 15) or string-length() gt 15]

Xpath 1.0:

//*[@*[contains(name(),"login") or contains(name(), "username") or contains(.,"login") or contains(., "username")]][(@value and string-length(@value) > 15) or string-length() > 15]

Explanation:

Search in attributename as well in value of attribute for strings login and username or value of attribute value is greater than 15 chars or value of node is greater than 15 chars.

If you like, give it a up. Otherwise please explain your problems with the results.

Upvotes: 1

Stefan Hegny
Stefan Hegny

Reputation: 2187

for xpath 1 it might read

//*[@*[(contains(name(),'login') or contains(name(),'username'))
       and string-length(.) &gt; 15]

Upvotes: 1

ulab
ulab

Reputation: 1089

how about

//*[@*[(contains(.,'login') or contains(.,'username')) and string-length() > 15]]

Upvotes: 1

Related Questions