472,330 Members | 1,488 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,330 software developers and data experts.

Using XSLT to Filter out elements with CDATA matching a regular expression

Hi,

I'm new to XSLT and I'm having a hard time figuring out whether XSLT
will do what I need it to do.

I have a XML file with a whole bunch of <message> elements. These
<message> elements have <![CDATA[...]]> in them. I would like to use
XSLT to remove the <message> elements whose CDATA (the "...") matches
a particular regular expression.

Could anyone help me determine if XSLT can do this? If it can, could
someone at least point me in the right direction so I can write the
transform?

Thanks!

--Edwin G. Castro
Jul 20 '05 #1
3 11683
Edwin G. Castro wrote:
Hi,

I'm new to XSLT and I'm having a hard time figuring out whether XSLT
will do what I need it to do.

I have a XML file with a whole bunch of <message> elements. These
<message> elements have <![CDATA[...]]> in them. I would like to use
XSLT to remove the <message> elements whose CDATA (the "...") matches
a particular regular expression.


Edwin,

The trick is to match on whatever text the CDATA would evaluate to. For
example, here is a testcase I wrote.

input :

<?xml version="1.0"?>
<root>
<a>some data</a>
<a><![CDATA[Some CDATA]]></a>
</root>

XSL :

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="//text()[contains(.,'CDATA')]" />
</xsl:template>

</xsl:stylesheet>

....and when XSL is run on input, I get :

<?xml version="1.0" encoding="UTF-8"?>
Some CDATA
Regards,
Kenneth
Jul 20 '05 #2
In article <d9*************************@posting.google.com> ,
Edwin G. Castro <ec*****@hp.com> wrote:

% I have a XML file with a whole bunch of <message> elements. These
% <message> elements have <![CDATA[...]]> in them. I would like to use
% XSLT to remove the <message> elements whose CDATA (the "...") matches
% a particular regular expression.

XSLT can't detect the CDATA sections as such. They're just (parts of) text
nodes so far as XPath and XSLT are concerned. Assuming that's not
a problem, you run into the problem that XSLT 1.0 doesn't provide any
regular expression support.

When you run into a basic limitation like that, scoot over to
http://exslt.org and see what's being proposed there. In this
case, you'll find a little number they like to call
http://exslt.org/regular-expressions, which can be used like this

<xsl:stylesheet xmlns:xsl = 'http://www.w3.org/1999/XSL/Transform'
version = '1.0'
xmlns:re = 'http://exslt.org/regular-expressions'>
<xsl:template match='node()|@*'>
<xsl:copy>
<xsl:apply-templates select='node()|@*'/>
</xsl:copy>
</xsl:template>

<!-- ignore messages dated in the first two months of the year -->
<xsl:template match = 'message[re:test(string(.),
"(Janvier|Fevrier) 200[34]")]'/>
</xsl:stylesheet>

This interface is marked experimental, so it might not work with your
XSLT processor, even if it supports the EXSLT extensions. Also, some
processors require you to have extension-element-prefixes='re' on
the stylesheet element, but I believe they shouldn't.

If that doesn't work for you, you could see if there are other
extension functions or language interfaces available to you. On
the other hand, if all you want to do is drop out elements whose
content matches an RE, this may be a job for another language.
--

Patrick TJ McPhee
East York Canada
pt**@interlog.com
Jul 20 '05 #3
> If that doesn't work for you, you could see if there are other
extension functions or language interfaces available to you. On
the other hand, if all you want to do is drop out elements whose
content matches an RE, this may be a job for another language.


Sounds like my current solution in using C# to process the file is a
much better option. The processor I'm using (via NAnt) is the one
provided by .NET so I'm sure it doesn't support the extensions.

On the other hand, going through this thought process has provided
some better ideas on how to organize my code. Thanks for the
responses.

--Edwin
Jul 20 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: Luca | last post by:
Hello, have a problem with this XML, I need XSLT to filter the products list based on rules writes in RULE tag. I need copy only CAT and PROD...
6
by: Gary Huntress | last post by:
Hi, I'm having trouble with an xslt transform. I'm trying to transform a vector into an array of width N. For example here is my vector: ...
5
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e";...
11
by: rajarao | last post by:
hi I want to remove the content embedded in <script> and </script> tags submitted via text box. My java script should remove the content embedded...
6
by: Mark Miller | last post by:
I have a scheduled job that uses different XSL templates to transform XML and save it to disk. I am having problems with the code below. The problem...
5
by: Newbie | last post by:
The answer to this is probably obvious, but I'm somewhat new to XSLT and can't find it. I need to sort a set of nodes within a document, e.g. ...
7
by: Max | last post by:
Hello everyone! Can anyone help me to convert the CDATA expression "CDATA ::= (Char* - (Char* ']]>' Char*)" to Javascript Regular Expression? ...
1
by: Edsel | last post by:
Hi all, Can anyone help with this? I would like to extract the attributes from a XML node and then transform them back to XML as elements using...
6
by: Zetten | last post by:
I have an AD search module which works as I want it to; searching for a matching forename and/or surname in the appropriate OU. I would like to...
0
by: tammygombez | last post by:
Hey fellow JavaFX developers, I'm currently working on a project that involves using a ComboBox in JavaFX, and I've run into a bit of an issue....
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.