|
Re: [tdom] CDATA in tdom
On 15 May, Moriz Wahl wrote:
> I have an XML-Document looking like this:
>
> <Equation ID="Equ1">
> <EquationSource Format="TEX">
> <![CDATA[$$
> P_{i} = \hat k{\left( {D_{1} ,D_{2} , \ldots D_{n} } \right)}\,,
> $$]]></EquationSource></Equation>
>
> --------------------------------------------------------------------------
> I tried this simple script:
>
> [...]
>
> What happened is, that the CDATA sections disappeared and became TEXT_NODEs
> instead. Why?
Because this is, what you normally want.
The probably major technical reason is, that the XPath data model
doesn't know anything about CDATA Sections.
I'm faced with the question the other way around: Why do you insist in
having the CDATA Sections in the serialized output? CDATA Sections are
just a fancy or crude alternative way to write XML Element pcdata
content. If you process your XML data with XML tools, there is no
difference between the two ways of writing.
> I wrote a workaround like this:
> -------------------------------------------------------
> foreach node [$root selectNodes //EquationSource] {
> set mynode [$node selectNodes text()]
> $doc createCDATASection [$mynode data] cdata
> $node appendChild $cdata
> $mynode delete
> }
> ------------------------------------------------------
> and got the output I wanted. Why this workaround. There could be other
> CDATA_SECTION_NODEs which I do not know before.
You seem to insist in getting the serialized XML output in one certain
way. Again: From the XML viewpoint there is no difference between the
serialization, you get by default from tDOM and the one, you force
with your workaround code - both serializations are exactly the same
data.
You don't have much control about the details of serialization with
tDOM (as for most other tools). In case of CDATA Sections, you may
have luck. If you use a half-way recent tDOM cvs head version (which
is, what you should do anyway), then the documentNode method
cdataSectionElements may help you. With that, your work around code
from above gets down to
$doc cdataSectionElements EquationSource 1
rolf
|