471,595 Members | 1,097 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,595 software developers and data experts.

XPath contains() function

My goal is to select some XPath nodes based on whether their values (text)
contain a string passed as argument. To achieve this I use the contains()
function of XPath in an XpathExpression.

For example, given the following fragment...

<code>
<Exprs>
<Expr>red fox</Expr>
<Expr>jump over</Expr>
<Expr>box</Expr>
</Exprs>
</code>

.... I want to select the second <Expr> and test whether it contains the
string argument "jump":
(I am passing values to the contains() function dynamically, but here I
will use static values for clarity.)

<code>
xPthExpr = xPathNavig.Compile("/Exprs/Expr[contains(" + "jump over," +
"jump" + ")]" );
xPathNodeIter = xPathNavig.Select(xPthExpr);
</code>

Now, of course contains() returns true for all the nodes and all the nodes
get selected. Instead, I want a behaviour like this: select ONLY the node(s)
where the value CONTAINS (not is equal to) "jump", something like the LIKE
SQL clause.Can I do this w/ XPath at all? I need it instead XmlDocument for
sake of speed.

TIA
Nov 12 '05 #1
2 20627
* sprungli wrote in microsoft.public.dotnet.xml:
... I want to select the second <Expr> and test whether it contains the
string argument "jump": <code>
xPthExpr = xPathNavig.Compile("/Exprs/Expr[contains(" + "jump over," +
"jump" + ")]" );
xPathNodeIter = xPathNavig.Select(xPthExpr);
</code>


Pass e.g. '.' or 'text()' as the first argument to contains as that
would refer to the string value of the first text child of the element.
Nov 12 '05 #2
Thanks Bjoern, that helped.

---
"Bjoern Hoehrmann" <bj****@hoehrmann.de> wrote in message
news:40***************@news.bjoern.hoehrmann.de...
* sprungli wrote in microsoft.public.dotnet.xml:
... I want to select the second <Expr> and test whether it contains the
string argument "jump":

<code>
xPthExpr = xPathNavig.Compile("/Exprs/Expr[contains(" + "jump over," +
"jump" + ")]" );
xPathNodeIter = xPathNavig.Select(xPthExpr);
</code>


Pass e.g. '.' or 'text()' as the first argument to contains as that
would refer to the string value of the first text child of the element.

Nov 12 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by kj | last post: by
1 post views Thread by Damien Goutte-Gattat | last post: by
2 posts views Thread by Jesper | last post: by
10 posts views Thread by Michael C# | last post: by
5 posts views Thread by jorgedelgadolopez | last post: by
2 posts views Thread by =?Utf-8?B?Z2lkZHk=?= | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.