xpath with regex

  • Thread starter Sebastian Petzelberger
  • Start date
S

Sebastian Petzelberger

Hi group,
please give me an example of a xpath with regex or
better a link with examples.
Thanks in advance,
Sebastian
 
M

Martin Honnen

Sebastian said:
please give me an example of a xpath with regex or
better a link with examples.

I am not sure what you are looking for here, XPath (1.0 at least)
doesn't know regular expressions, it only has some string functions. In
what context are you using XPath, and why do you need "xpath with regex"?
 
S

Sebastian Petzelberger

I am not sure what you are looking for here, XPath (1.0 at least)
doesn't know regular expressions, it only has some string functions.

XPath 2.0 does know regular expressions, doesn't it?
In
what context are you using XPath, and why do you need "xpath with regex"?

I want to find nodes. Instead of using =, >, < I want to match with a
regex. But I do not know how the xpath with regex looks like. I looked
in the spec, but I can't understand it. Any examples out there?
 
M

Martin Honnen

Sebastian said:
XPath 2.0 does know regular expressions, doesn't it?

Yes, but it is still under development, consider mention the XPath
version you are asking about.
I want to find nodes. Instead of using =, >, < I want to match with a
regex. But I do not know how the xpath with regex looks like. I looked
in the spec, but I can't understand it. Any examples out there?

The only implementation of the latest XPath 2.0 spec I know is Saxon,
with Saxon 7.8 the matches function of XPath 2.0 is supported, it takes
a string value to check against a second string value which is a regular
expression pattern and returns a boolean.

Here is a simple example XSLT 2.0 stylesheet which uses the matches
function:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fn="http://www.w3.org/2003/11/xpath-functions">

<xsl:eek:utput method="xml" indent="yes" />

<xsl:template match="/">
<result>
<xsl:for-each select="('1234', 'abc')">
<match><xsl:value-of select="fn:matches(., '\d{4}')" /></match>
</xsl:for-each>
</result>
</xsl:template>

</xsl:stylesheet>

To keep things simple to just demonstrate the use of the matches
function the XSLT doesn't read any nodes from an input XML file so it
doesn't matter against which XML you apply it, when applying it to
itself with Saxon 7.8 from the command line as follows

java -jar C:\Programme\saxon78\saxon7.jar test20040217Xsl.xml
test20040217Xsl.xml

the output is

<?xml version="1.0" encoding="UTF-8"?>
<result xmlns:fn="http://www.w3.org/2003/11/xpath-functions">
<match>true</match>
<match>false</match>
</result>
 
S

Sebastian Petzelberger

Thank you Martin for your awnser, and for your time so far. Sorry,
that I am replying so late.
The only implementation of the latest XPath 2.0 spec I know is Saxon,
with Saxon 7.8 the matches function of XPath 2.0 is supported, it takes
a string value to check against a second string value which is a regular
expression pattern and returns a boolean.

Thank you, I will look deeper in this.
Here is a simple example XSLT 2.0 stylesheet which uses the matches
function:

Do I need xslt?
<match><xsl:value-of select="fn:matches(., '\d{4}')" /></match>

fn:matches(., '\d{4}') How will look this function in an xpath?

//CreateOrderResponse/OrderData/BizTalk/@timestamp
is an example xpath, how does the regex function be part of the xpath?

Thanks again, Sebastian
 
P

Patrick TJ McPhee

[Martin wrote]

% > <match><xsl:value-of select="fn:matches(., '\d{4}')" /></match>
%
% fn:matches(., '\d{4}') How will look this function in an xpath?

That _is_ an XPath. It's actually an example of an XPath extension function
-- is this how it's defined in XPath 2.0? You can define something similar
with most or all XPath implementations if you have a perl regular expression
engine in the appropriate language.

% //CreateOrderResponse/OrderData/BizTalk/@timestamp

% is an example xpath, how does the regex function be part of the xpath?

This is actually a location path. Location paths are part of the xpath
language, but they aren't all of it. The question is, what do you
want to do? If you'd like to select timestamps which match some regular
expression, you could have

//CreateOrderResponse/OrderData/BizTalk/@timestamp[fn:matches(., $yourREhere)]
 
M

Martin Honnen

Sebastian Petzelberger wrote:

Do I need xslt?

That depends on the tool you use (and your knowledge with that tool), I
know how to use Saxon 7.8 as an XSLT 2.0 processor therefore I have
given an XSLT 2.0 stylesheet making use of XPath 2.0. I think Saxon also
allows you to evaluate pure XPath expressions but without looking deeper
into its documentation I can't give an example on how to do this.
fn:matches(., '\d{4}') How will look this function in an xpath?

Well, the value of the select attribute is an XPath expression, and
fn:matches is a function defined in XPath 2.0 (with the prefix fn bound
to the appropriate namespace).
//CreateOrderResponse/OrderData/BizTalk/@timestamp
is an example xpath, how does the regex function be part of the xpath?

//CreateOrderResponse/OrderData/BizTalk/@timestamp[fn:matches(.,
'\d{4}')]
filters all timestamp attribute nodes whose value matches the regular
expression \d{4} (four digits).
 
S

Sebastian Petzelberger

Dear Martin,
//CreateOrderResponse/OrderData/BizTalk/@timestamp[fn:matches(.,
'\d{4}')]
filters all timestamp attribute nodes whose value matches the regular
expression \d{4} (four digits).

Thank you for your solution, that was exactly I was looking for.

Thank you Patrick as well.

Greetings, Sebastian
 

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

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,236
Members
46,822
Latest member
israfaceZa

Latest Threads

Top