943,693 Members | Top Members by Rank

Ad:
Jun 25th, 2009
0

Conditional CDATA

Expand Post »
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?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
metinh is offline Offline
1 posts
since Jun 2009
Jun 28th, 2009
0

Re: Conditional CDATA

What you want to do is not supported by XSL because including these characters in your XML document renders the XML document invalid.
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008
Jun 29th, 2009
0

Re: Conditional CDATA

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
Reputation Points: 16
Solved Threads: 21
Junior Poster
xml_looser is offline Offline
178 posts
since Apr 2009
Jul 1st, 2009
0

Re: Conditional CDATA

Click to Expand / Collapse  Quote originally posted by fpmurphy ...
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.
Reputation Points: 22
Solved Threads: 11
Junior Poster
fpmurphy is offline Offline
144 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in XML, XSLT and XPATH Forum Timeline: need help in xslt
Next Thread in XML, XSLT and XPATH Forum Timeline: Parse JavaScript Variable to a XSLT Variable





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC