Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xpath - using contains with a wildcard

I have the following, and trying to see if there's a better approach. I know it cn be done using starts-with/contains. I'm testing with firefox 10, which I believe implements xpath 2.+.

Test node is

<a id="foo">
.
.
.
<a id="foo1">
.
<a id="foo2">

Is there a way to use wildcards to be able to get the foo1/foo2 nodes..

Something like

//a[@id =* 'foo'] 

or 

//a[contains(@id*,'foo')] 

Which would say, give me the "a" where the id starts with "foo" but has additional chars... This would then skip the 1st node with the "foo"

I thought I had seen an rticle on this, but can't find it!

As I recall, the article stated that xpath had a set of operators that could be used to designate the start/end of a given pattern in a string.

thanks

like image 535
tom smith Avatar asked Sep 12 '12 18:09

tom smith


People also ask

What does the wildcard operator * Do in XPath?

xml file to learn XPath wildcards and its style sheet which uses Xpath expressions to select a value from the elements. The most widely used type is the asterisk(*). To match all nodes below expression is used and the (//) operator denotes a descendant type wildcard.

How to use wildcard in XML?

The @* wildcard matches all attribute nodes. For example, this XSLT template rule copies the values of all attributes of a person element in the document into the content of an attributes element in the output: <xsl:template match="person"> <attributes><xsl:apply-templates select="@*"/></attributes> </xsl:template> ...


2 Answers

Use:

//a[@id[starts-with(., 'foo') and string-length() > 3]]
like image 150
Dimitre Novatchev Avatar answered Oct 11 '22 15:10

Dimitre Novatchev


Not a wildcard, but probably what you need nonetheless:

//a[(@id!='foo') and starts-with(@id,'foo')]

See W3C XPath spec.

like image 45
Lucero Avatar answered Oct 11 '22 14:10

Lucero