Help | Site Map
Connecting Tech Pros Worldwide
 
 
LinkBack Thread Tools
  #1  
Old July 27th, 2006, 02:15 PM
Dana B
Guest
 
Posts: n/a
Default Extracting values from CDATA

I am trying to get the values in FIELD2 and FIELD3 in the XML file
below using XSLT. I can get the value of CLOB_DATA. It comes back as
an string. How can I extract the values FIELD2 and FIELD3?

Thanks in advance.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<XML>
<FIELD1>field1</FIELD1>
<CLOB_DATA>
<![CDATA[<?xml version="1.0"?>
<FIELD2>field2</FIELD2>
<FIELD3>field3</FIELD3>
]]>
</CLOB_DATA>
</XML>

  #2  
Old July 27th, 2006, 02:45 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA



Dana B wrote:
Quote:
I am trying to get the values in FIELD2 and FIELD3 in the XML file
below using XSLT. I can get the value of CLOB_DATA. It comes back as
an string. How can I extract the values FIELD2 and FIELD3?
Quote:
<CLOB_DATA>
<![CDATA[<?xml version="1.0"?>
<FIELD2>field2</FIELD2>
<FIELD3>field3</FIELD3>
]]>
</CLOB_DATA>
For that stuff in the CDATA section your XSLT stylesheet sees only a
string and no fields or elements. So you need to have a way to parse
that string as XML which needs an extension function, XSLT 1.0 can't do
that directly.

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #3  
Old July 27th, 2006, 03:15 PM
Dana B
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

Can you show me an example of how I can parse the XML string with an
extension function?

Thanks
Dana

  #4  
Old July 27th, 2006, 03:25 PM
Martin Honnen
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA



Dana B wrote:
Quote:
Can you show me an example of how I can parse the XML string with an
extension function?
That depends highly on which XSLT processor you use and whether it
supports that kind of extension function.
I could show you can example for MSXML with the extension function being
written in JScript but that does not help you at all if you are using a
diffetrent XSLT processor.

--

Martin Honnen
http://JavaScript.FAQTs.com/
  #5  
Old July 27th, 2006, 04:15 PM
Andy Dingley
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

Dana B wrote:
Quote:
I am trying to get the values in FIELD2 and FIELD3 in the XML file
below using XSLT.
That's not XML - That's XML, wrapped up as a string, wrapped up in more
XML. Three layers of data! It's not only hard to parse, it actually
_means_ something quite different. Of course it might be intended to
mean the same thing after all, so don't take that difference too
seriously.

Now XSLT likes to see XML and it will (grudgingly) let you work with
strings inside XML. However it has no way to work with XML inside this
(3rd layer down).

If you want to work with this then you need to tree walk the outer XML
to get the strings, then pass this content to an XML parser! It's not
hard to do in a script language with a DOM (Java / JavaScript / Perl /
VB), although I'm not keen on the efficiency aspects.

As to doing it _within_ XSLT then I've never tried it, but it ought to
work fairly much the same, so long as you have an extension language
like JavaScript with access to a suitable DOM.

The real solution though is to not wrap it up in the first place. Is
this "inner" XML really well-formed XML ? Is it a well-formed fragment,
or is it also a well-formed XML document? (your's isn't - two root
elements) If it isn't, then any attempt to parse it is doomed. If it
is (even if it's a fragment) then why was it put in a CDATA section to
begin with ?

If this "inner" content _is_ XML, then there's no reason at all to wrap
it in a CDATA section. This is what namespacing is for, after all.

  #6  
Old July 28th, 2006, 12:55 AM
Joe Kesselman
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

Why are you using the CDATA section in the first place? If the data's
structure is important, make that part of the structure of your
document, not a string value.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  #7  
Old July 28th, 2006, 05:05 AM
Dana B
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

I didn't create the XML but I have to parse it. I'm not sure why they
used CDATA. There are probably characters that they didn't want to
encode. I am trying to parse the fields and convert it to CSV using
XSLT if possible and practical. If not, I will use Java.

  #8  
Old July 28th, 2006, 12:45 PM
Joe Kesselman
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

If you're really stuck with it... I agree with others who said the
cleanest solution is to extract the contents of the CDATA section and
run them through a second parsing pass.

Your customer handed you lemons. Make lemonade.

--
() ASCII Ribbon Campaign | Joe Kesselman
/\ Stamp out HTML e-mail! | System architexture and kinetic poetry
  #9  
Old September 4th, 2006, 08:55 AM
Naim KANJ
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA





*** Sent via Developersdex http://www.developersdex.com ***
  #10  
Old September 4th, 2006, 09:05 AM
Naim KANJ
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA



I have the same problem, i have an xml document that i can't change its
CDATA section which contain data in xml format that i need to browse in
html format using xslt1.0 .

the result in the parser is text written in xml format.

I need your help,

Thanks in advance,

Naim KANJ

*** Sent via Developersdex http://www.developersdex.com ***
  #11  
Old September 4th, 2006, 11:05 PM
Peter Flynn
Guest
 
Posts: n/a
Default Re: Extracting values from CDATA

Naim KANJ wrote:
Quote:
>
I have the same problem, i have an xml document that i can't change its
CDATA section which contain data in xml format that i need to browse in
html format using xslt1.0 .
>
the result in the parser is text written in xml format.
Tell the originators to remove the CDATA markup before they send you
the document.

Or filter it out in a script to another file before you process it.

///Peter
--
XML FAQ: http://xml.silmaril.ie/
 

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles