473,511 Members | 16,660 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 16630


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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
6812
by: bdinmstig | last post by:
I refined my attempt a little further, and the following code does seem to work, however it has 2 major problems: 1. Very limited support for XPath features Basic paths are supported for...
0
1903
by: gael.pegliasco | last post by:
Hi, How are you dear and nice helper :) ? I'm trying to test xpath with this simple program : import xml.dom.minidom from xml.xpath.Context import Context import xml.xpath
4
2211
by: Timo Nentwig | last post by:
Hi! Seems that * cannot stand for non-existing nodes, i.e. /html/*/title will not match <html> <title>won't match</title>
4
21190
by: Son KwonNam | last post by:
In XSLT, is this possible to get value from xml using XPath which is in XSLT variable? I mean XPath strings can be dynamic while XSL Transforming. If possible, How?? Because I'm not a...
5
4184
by: laks | last post by:
Hi I have the following xsl stmt. <xsl:for-each select="JOB_POSTINGS/JOB_POSTING \"> <xsl:sort select="JOB_TITLE" order="ascending"/> This works fine when I use it. But when using multiple...
18
7704
by: jacksu | last post by:
I have a simple program to run xpath with xerces 1_2_7 XPathFactory factory = XPathFactory.newInstance(); XPath xPath = factory.newXPath(); XPathExpression xp = xPath.compile(strXpr);...
9
2130
by: David Thielen | last post by:
Hi; I am sure I am missing something here but I cannot figure it out. Below I have a program and I cannot figure out why the xpath selects that throw an exception fail. From what I know they...
5
7910
by: Gnic | last post by:
Hi , I have an XmlDocument instance, I want to find a node in the xml, but I don't know it's path until runtime, for example <aaa> <bbb name="x"/> <aaa attr="y"> <ccc>sometext</ccc> </aaa>
0
7242
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7355
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
7081
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7510
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5668
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
3225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3213
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1576
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
447
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.