Conditional CDATA

Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
Reply

Join Date: Jun 2009
Posts: 1
Reputation: metinh is an unknown quantity at this point 
Solved Threads: 0
metinh metinh is offline Offline
Newbie Poster

Conditional CDATA

 
0
  #1
Jun 25th, 2009
Hello everyone. I am trying to implement CDATA wrapping of values conditionally in XSLT.
My requirements are:
- If the value contains <, >, &, ", ' then it must be wrapped in CDATA in the transformed output.
- if the value does not contain any of the above characters, it will not be wrapped in CDATA
- This logic will need to be implemented on many fields

Does anyone have any strategies on how to do this?
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Conditional CDATA

 
0
  #2
Jun 28th, 2009
What you want to do is not supported by XSL because including these characters in your XML document renders the XML document invalid.
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 44
Reputation: xml_looser is an unknown quantity at this point 
Solved Threads: 2
xml_looser xml_looser is offline Offline
Light Poster

Re: Conditional CDATA

 
0
  #3
Jun 29th, 2009
for testing

XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <root>
  4. <item><![CDATA[<]]></item>
  5. <item><![CDATA[>]]></item>
  6. <item><![CDATA[&]]></item>
  7. <item><![CDATA["]]></item>
  8. <item><![CDATA[']]></item>
  9. <item>Mc Donold's
  10. </item>
  11. <item>Siegfried &amp; Roy
  12. </item>
  13. <item>a &gt;b
  14. </item>
  15. <item>a &lt;b
  16. </item>
  17. </root>

xsl
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1.  
  2. <?xml version="1.0" encoding="UTF-8" ?>
  3.  
  4. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  5. <xsl:output method="xml" indent="yes"/>
  6. <xsl:template match="/">
  7. <xsl:apply-templates select="root"/>
  8. </xsl:template>
  9. <xsl:template match="root">
  10. <root>
  11. <xsl:apply-templates select="item"/>
  12. </root>
  13. </xsl:template>
  14. <xsl:template match="item">
  15. <item>
  16. <xsl:variable name="pos">'</xsl:variable>
  17. <xsl:choose>
  18. <xsl:when test="contains(.,'&lt;')">
  19. <xsl:value-of select="concat('&lt;![CDATA[',.,']]&gt;')"/>
  20. </xsl:when>
  21. <xsl:when test="contains(.,'&gt;')">
  22. <xsl:value-of select="concat('&lt;![CDATA[',.,']]&gt;')"/>
  23. </xsl:when>
  24. <xsl:when test="contains(.,'&amp;')">
  25. <xsl:value-of select="concat('&lt;![CDATA[',.,']]&gt;')"/>
  26. </xsl:when>
  27. <xsl:when test="contains(.,'&quot;')">
  28. <xsl:value-of select="concat('&lt;![CDATA[',.,']]&gt;')"/>
  29. </xsl:when>
  30. <xsl:when test="contains(.,$pos)">
  31. <xsl:value-of select="concat('&lt;![CDATA[',.,']]&gt;')"/>
  32. </xsl:when>
  33. <xsl:otherwise>
  34. <xsl:value-of select="."/>
  35. </xsl:otherwise>
  36. </xsl:choose>
  37. </item>
  38. </xsl:template>
  39. </xsl:stylesheet>

result
XML, XSLT and XPATH Syntax (Toggle Plain Text)
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3. <root>
  4. <item>&lt;![CDATA[&lt;]]&gt;</item>
  5. <item>&lt;![CDATA[&gt;]]&gt;</item>
  6. <item>&lt;![CDATA[&amp;]]&gt;</item>
  7. <item>&lt;![CDATA["]]&gt;</item>
  8. <item>&lt;![CDATA[']]&gt;</item>
  9. <item>&lt;![CDATA[Mc Donold's
  10. ]]&gt;</item>
  11. <item>&lt;![CDATA[Siegfried &amp; Roy
  12. ]]&gt;</item>
  13. <item>&lt;![CDATA[a &gt;b
  14. ]]&gt;</item>
  15. <item>&lt;![CDATA[a &lt;b
  16. ]]&gt;</item>
  17. </root>

Helmut Hagemann
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 95
Reputation: fpmurphy is an unknown quantity at this point 
Solved Threads: 5
fpmurphy fpmurphy is offline Offline
Junior Poster in Training

Re: Conditional CDATA

 
0
  #4
Jul 1st, 2009
Originally Posted by fpmurphy View Post
What you want to do is not supported by XSL because including these characters in your XML document renders the XML document invalid.
Sorry that I was not clearer. I should have added "It is possble however to include these characters if you guard them with <![CDATA[ ... ]]. "

As I understand it, the OP wants to know how to detect these characters in a document using XSL and, if found, then add "<![CDATA[ ... ]]" guards. A totally different issue than you show in your example.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC