473,386 Members | 1,864 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 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 11828
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 data in PRODUCTS-LIST that match 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: <data> <x id="1">1.2</x> <x id="2">.2</x> <x...
5
by: Henry | last post by:
I have this simple code, string escaped = Regex.Escape( @"`~!@#$%^&*()_=+{}\|;:',<.>/?" + "\"" ); string input = @"a&+" + "\"" + @"@(-d)\e"; Regex re = new Regex( string.Format(@"(+)", escaped),...
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 between <script> and </script> tag. my current...
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 shows up on both my development machine (Windows...
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. <A> <B/> <C> <D>3</D> <D>2</D>
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? Thanks, Max
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 XSLT? <unsorted> <office...
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 extend it to be more flexible, so that instead of...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.