469,271 Members | 1,161 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

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 20559
* 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 zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.