Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

CDATA syntax question 1

Status
Not open for further replies.

pie3141516

Programmer
Mar 28, 2005
13
US
Can someone tell me the correct syntax to recognize a CDATA segment?

I need to know how to tell the difference between the following two sample lines:


<Description><Text>asd asd asd</Text></Description>

and

<Description><Text><![CDATA[asd asd asd]]></Text></Description>

I have tried using the following:

<xsl:choose>
<xsl:when class="a:Description/a:Text/a:CDATA>

{then do something}

</xsl:when>
<xsl:eek:therwise>

{do something else}

</xsl:eek:therwise>
</xsl:when>

But I cannot get the syntax right....
Help would be greatly appreciated.

Thanks.
 
><xsl:when class="a:Description/a:Text/a:CDATA>
I know you know there are problems on this. "class" should be "test". But the main thing is xslt processor won't recognize CDATA section anymore. xml parser could see it. But once it is parsed and made ready to xslt processor, that info is not retained or retrievable as it would be transparent to xslt process. The internal representation of the xml document would be the same CDATA section or text node.
 
I am having great difficulty with "whitespace" and xml:

Here is an xml snippit:

<Test>
<NTESegment><Text><![CDATA[ Coronary Heart Disease Risk Table]]></Text></NTESegment>
</Test>
<Test>
<NTESegment><Text><![CDATA[ Men Women]]></Text></NTESegment>
</Test>
<Test>
<NTESegment><Text><![CDATA[ 1/2 Average Risk 3.4 3.3]]></Text></NTESegment>
</Test>


It produces the following output:


Coronary Heart Disease Risk Table

Men Women

1/2 Average Risk 3.4 3.3

Average Risk 5.0 4.4


Notice, there is no formatting.
I've tried adding a "preserve-space"
statement to the stylesheet but that did
not work, as the <!CDATA[ ]]> container is immune.
I have also tried adding a DTD header with
an ATTLIST to the stylesheet, but that too
did not pierce the <!CDATA[ ]]> - no formatting.
The formatting exists in the xml file, but
it is not rendered when viewed with a browser
and stylesheet.

Is there a way to preserve the whitespace
in a CDATA segment, and have it render
correctly when viewed with a browser and
style sheet?

Any help would be appreciated.
~LCarroll
 
The xslt would output the value preserving the whitespaces; only that, I would guess, your output is probably be html and in that case the user-agent would collapse the whitespace.

Do this with the proper prefix (a: in your first post) and xpath.
[tt] <xsl:value-of select="translate(a:NTESegment/a:Text,' ','&#xa0;')" />[/tt]
 
Thanks a gazillion!!!
It worked!
It is always amazing to me how a few well chosen letters of the alphabet can move mountains.
Thanks again!
~LCarroll
 
Could you please answer one last question...

My defaultCCS sets the font-size and font-family
of the entire document:

*{
font-size: small;
font-family: Arial, sans-serif;
}
h1{
font-size: 150%;
}

Is there a way to set the font family in just
the NTESegment element, inside the <!CDATA[ ]]> ?

I need it to be a fixed font, like Courier or
teletype.

I looked for a "translate" font type function,
but I did not find one.

Any help would be appreciated.

I promise, this is my last question,
as I know your time is valuable.
~LCarroll

 
[1] If you are not in a position to change the default css, you can always override it with inline style (modulo detail). Like this for the xsl:value-of selecting NTESegment/Text.
[tt]
<span style="font-family:courier,monospace;">
<xsl:value-of select="translate(a:NTESegment/a:Text,' ','&#xa0;')" />
</span>
[/tt]
[2] If you're in a position to augment the default css, you can do this.

[2.1] Adding a class.
[tt]
span.mono {font-family:courier,monospace;}
[/tt]
[2.2] Similar construction for the xsl:value-of block.
[tt]
<span class="mono">
<xsl:value-of select="translate(a:NTESegment/a:Text,' ','&#xa0;')" />
</span>
[/tt]
 
Any help would be so greatly appreciated.

Here are the two problems.
1. From a previous post, I use a CDATA with a "translate" statement to output formatted text. It works well in another segment of the style sheet. However, it does not work in the snippit below. The CDATA text is not fomatted
when viewed with the browser.

2. The data cells do not hold the same "width" from row to row. ie, the <Type> column is wider in the second record, than in the first.

Any ideas why the CDATA won't respond to the style sheet snippit? I think it might have to do with the for-each statement, but I don't know...the CDATA will not respond to the translate statement.

Please please help..
Thanks.
~LCarroll



xml follows:
---------------------------------------------------------------------------

<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="ccr_20060420.xsl"?>

<ContinuityOfCareRecord
xmlns:xsi="xsi:schemaLocation="urn:astm-org:CCR CCR_20051109.xsd"
xmlns="urn:astm-org:CCR">

<Body>

<MedicalNotes>

<MedicalNote>

<Component>

<Type><Text>Subjective:</Text></Type>
<Content><Text><![CDATA[[RX ] LASIX [40 MG] <00039-0060>
[RX ] LANOXIN [.125 MG] <00247-0510>
[PT ED] test
[ALERT] 05/28/2008 [Check for lab results]
[RECALL] 06/24/2008 [99213]
[CPT] 99213 - EP Level 3
]]></Text></Content>

</Component>

</MedicalNote>

<MedicalNote>

<Component>

<Type><Text>Plan:</Text></Type>
<Content><Text><![CDATA[[RX ] LASIX [40 MG] <00039-0060>
[RX ] test test test test test
[ALERT] 05/28/2008 [Check for lab results]
[RECALL] 06/24/2008 [99213]
[CPT] 99213 - EP Level 3
]]></Text></Content>

</Component>

</MedicalNote>

</MedicalNotes>

</Body>

</ContinuityOfCareRecord>

-------------------------------------------------------------------------------

Here is the snippit from the style sheet:
-------------------------------------------------------------------------------

<xsl:if test="a:ContinuityOfCareRecord/a:Body/a:MedicalNotes/a:MedicalNote[a:Component/a:Content/a:Text!='']">
<tr id="mednotesrow">

<td>
<span class="header">Medical Notes</span>
<br/>
<table id="mednotes" class="list" width="100%">

<tbody>
<tr>
<th>Type</th>
<th>Date</th>
<th>Component</th>
<th>Source</th>
</tr>

<xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:MedicalNotes/a:MedicalNote[a:Component/a:Content/a:Text!='']">
<tr>
<td width="10%">
<xsl:value-of select="a:Type/a:Text"/>
</td>

<td width="25%">
<table class="internal">
<tbody>
<xsl:call-template name="dateTime">
<xsl:with-param name="dt" select="a:DateTime"/>
<xsl:with-param name="fmt">MMM dd, yyyy ':' hh:mm aa zzz</xsl:with-param>
</xsl:call-template>
</tbody>
</table>
</td>

<td>
<table class="internal" width="100%">
<tbody>

<xsl:for-each select="a:Component">
<tr>
<td width="6%">
<xsl:value-of select="a:Type/a:Text"/>
</td>

<td width="44%">
<xsl:value-of select="translate(a:Content/a:Text,' ','&#xa0;')"/>
</td>

</tr>
</xsl:for-each>

</tbody>
</table>
</td>


<td width="15%">
<a>
<xsl:attribute name="href">#
<xsl:value-of select="a:Source/a:Actor/a:ActorID"/></xsl:attribute>
<xsl:call-template name="actorName">
<xsl:with-param name="objID" select="a:Source/a:Actor/a:ActorID"/>
</xsl:call-template>
</a>
</td>
</tr>
</xsl:for-each>


</tbody>

</table>

</td>
</tr>
</xsl:if>

---------------------------------------------------------------------------------------------------------
end of style sheet segment.
 
[3] I have not looked into the detail of you last post. By the face of it, I think you might mean to have multi-line data and align as well, in that case, translate() would not be sufficient. The only reason that it is insufficient to handle the translation is that this time it is not one-to-one mapping with character as unit.

[3.1] You need to map CDATA line break &#x0a; to html line break <br />. You can make or grab a little named template to do the substitution of all instances of '&#x0a;' by '<br >'.

[3.2] If you have it not handy, you can take a look of a not too distance past thread:
where you'll find such a template "substring-replace" and make the substitution as noted in [3.2]. It is just more lines but the idea is not too complicated.
 
I am really struggling with the following three lines of code:

<td>
<xsl:value-of select="translate(a:Content/a:Text,' ','&#xA0;')" />
</td>

My xml snippit is simple:

<Content><Text><![CDATA[ENT / EYES: normal
GI: normal
NEURO: normal
GU: normal
SKIN: normal
MS / LYMPH: normal
]]></Text></Content>

My output is always:

ENT / EYES: normal GI: normal NEURO: normal GU: normal SKIN: normal MS / LYMPH: normal

There are no linefeeds or carriage returns when viewed
with a browser, but the xml file clearly has them.

I have tried the following "replace-substring" template:

<xsl:call-template name="replace-substring">
<xsl:with-param name="value" select="a:NoteSegment/a:Text"/>
<xsl:with-param name="from">&#x0a;</xsl:with-param>
<xsl:with-param name="to"><br /></xsl:with-param>
</xsl:call-template>

I know that the <br /> is inserted, they just don't do
anything. There is no linefeed.

Am I doing something wrong?
Any help would be appreciated.

Thanks,
~LCarroll




Here is the snippit from my style sheet:
-------------------------------------------------------------------------------------------------------

<xsl:if test="a:ContinuityOfCareRecord/a:Body/a:MedicalNotes/a:MedicalNote">
<tr>

<td>
<span class="header">Medical Notes</span>
<br/>
<table class="list" width="100%">

<tbody>

<tr>
<th>Type</th>
<th>Date</th>
<th>Component</th>
<th>Source</th>
</tr>

<xsl:for-each select="a:ContinuityOfCareRecord/a:Body/a:MedicalNotes/a:MedicalNote">
<tr>
<td width="10%">
<xsl:value-of select="a:Type/a:Text"/>
</td>

<td width="15%">
<table class="internal">
<tbody>
<xsl:call-template name="dateTime">
<xsl:with-param name="dt" select="a:DateTime"/>
</xsl:call-template>
</tbody>
</table>
</td>

<td>
<table class="internal" width="100%">
<tbody>
<tr>
<xsl:for-each select="a:Description">

<td>
<strong class="clinical">
<xsl:value-of select="a:Component"/>
</strong>
</td>

<tr>
<td>
<xsl:value-of select="translate(a:Content/a:Text,' ','&#xA0;')" />
</td>
</tr>



</xsl:for-each>
</tr>
</tbody>
</table>
</td>

<td width="15%">
<a>
<xsl:attribute name="href">#
<xsl:value-of select="a:Source/a:Actor/a:ActorID"/></xsl:attribute>
<xsl:call-template name="actorName">
<xsl:with-param name="objID" select="a:Source/a:Actor/a:ActorID"/>
</xsl:call-template>
</a>
</td>
</tr>
</xsl:for-each>
</tbody>
</table>
</td>
</tr>
</xsl:if>
-------------------------------------------------------------------------------------------------------

 
[4] I think if cdata section is prepared with all the info of critical alignments etc will be at some moment in the future, sooner or later, something to be regret. That may just be my premonition.

[4.1] Translating text file alignment into html alignment may be a tedious work. To have a short cut to all that, simply use pre-tag to save that labour and sacrifice some more detail stylistic control. Like this, see if the effect please you.
[tt]
<tr>
<td>
<!-- replaced
<xsl:value-of select="translate(a:Content/a:Text,' ','&#xA0;')" />
-->
[blue]<pre>
<xsl:value-of select="a:Content/a:Text" />
</pre>[/blue]
</td>
</tr>
[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top