469,343 Members | 5,388 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

xpath with regex

Hi group,
please give me an example of a xpath with regex or
better a link with examples.
Thanks in advance,
Sebastian
Jul 20 '05 #1
7 16322


Sebastian Petzelberger wrote:
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"?

--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #2
> 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?
Jul 20 '05 #3


Sebastian Petzelberger wrote:
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?


Yes, but it is still under development, consider mention the XPath
version you are asking about.
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?


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:output 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>
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #4
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
Jul 20 '05 #5
In article <98**************************@posting.google.com >,
Sebastian Petzelberger <Se********************@haenchen.softwarezentrum.d e> wrote:

[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)]

--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #6


Sebastian Petzelberger wrote:

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

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.
<match><xsl:value-of select="fn:matches(., '\d{4}')" /></match>

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).
--

Martin Honnen
http://JavaScript.FAQTs.com/

Jul 20 '05 #7
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
Jul 20 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by bdinmstig | last post: by
4 posts views Thread by Timo Nentwig | last post: by
4 posts views Thread by Son KwonNam | last post: by
5 posts views Thread by laks | last post: by
9 posts views Thread by David Thielen | last post: by
5 posts views Thread by Gnic | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.