XPath question

G

Guest

Hi,
Sorry if this is not hte right group, but I couln't find a XPath group.

In a XML document I have an attribute that conatains a text. I would like
the node to be selected if the attibute contains a specified substring. Is it
possibe to use the XPath function 'contains()' to filter out a substring in
an attribure. I can only find XSL examples.

If I have :

<root><foo attr='hi;mom'><yada/></foo></root>

I would like something like this:

XmlDocument.SelectSingeNode("root/foo[attr='{contains('mom')}']/yada");

to return the node yada. I.e. if the substring 'mom' is present in the text
of the attribute node attr.

Is it possible an if yes what is the correct syntax. Thanks.

Brgds Jesper, Denmark.
 
H

Hans Kesting

Jesper said:
Hi,
Sorry if this is not hte right group, but I couln't find a XPath
group.

In a XML document I have an attribute that conatains a text. I would
like the node to be selected if the attibute contains a specified
substring. Is it possibe to use the XPath function 'contains()' to
filter out a substring in an attribure. I can only find XSL examples.

If I have :

<root><foo attr='hi;mom'><yada/></foo></root>

I would like something like this:

XmlDocument.SelectSingeNode("root/foo[attr='{contains('mom')}']/yada");

to return the node yada. I.e. if the substring 'mom' is present in
the text of the attribute node attr.

Is it possible an if yes what is the correct syntax. Thanks.

Brgds Jesper, Denmark.

What about:

XmlDocument.SelectSingleNode("/root/foo[contains(@attr, 'mom')]/yada");


from MSDN:

-----------
contains Function
Checks whether the first argument string contains the second argument string.
boolean contains(str1, str2)

Parameters
str1
A string that might contain the second argument.
str2
A string that might be contained in the first argument.

Return Values
Returns true if the first argument string contains the second argument string. Returns false otherwise.

Remarks
If an argument is not of type string, it is first converted to a string and then evaluated. The contains() function is
case-sensitive.
 
G

Guest

It works.
Thanks!. Nice Xmas to you.


Hans Kesting said:
Jesper said:
Hi,
Sorry if this is not hte right group, but I couln't find a XPath
group.

In a XML document I have an attribute that conatains a text. I would
like the node to be selected if the attibute contains a specified
substring. Is it possibe to use the XPath function 'contains()' to
filter out a substring in an attribure. I can only find XSL examples.

If I have :

<root><foo attr='hi;mom'><yada/></foo></root>

I would like something like this:

XmlDocument.SelectSingeNode("root/foo[attr='{contains('mom')}']/yada");

to return the node yada. I.e. if the substring 'mom' is present in
the text of the attribute node attr.

Is it possible an if yes what is the correct syntax. Thanks.

Brgds Jesper, Denmark.

What about:

XmlDocument.SelectSingleNode("/root/foo[contains(@attr, 'mom')]/yada");


from MSDN:

-----------
contains Function
Checks whether the first argument string contains the second argument string.
boolean contains(str1, str2)

Parameters
str1
A string that might contain the second argument.
str2
A string that might be contained in the first argument.

Return Values
Returns true if the first argument string contains the second argument string. Returns false otherwise.

Remarks
If an argument is not of type string, it is first converted to a string and then evaluated. The contains() function is
case-sensitive.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top