| | |
Conditional CDATA
Please support our XML, XSLT and XPATH advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Jun 2009
Posts: 1
Reputation:
Solved Threads: 0
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?
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?
•
•
Join Date: Apr 2009
Posts: 44
Reputation:
Solved Threads: 2
for testing
xsl
result
Helmut Hagemann
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8"?> <root> <item><![CDATA[<]]></item> <item><![CDATA[>]]></item> <item><![CDATA[&]]></item> <item><![CDATA["]]></item> <item><![CDATA[']]></item> <item>Mc Donold's </item> <item>Siegfried & Roy </item> <item>a >b </item> <item>a <b </item> </root>
xsl
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output method="xml" indent="yes"/> <xsl:template match="/"> <xsl:apply-templates select="root"/> </xsl:template> <xsl:template match="root"> <root> <xsl:apply-templates select="item"/> </root> </xsl:template> <xsl:template match="item"> <item> <xsl:variable name="pos">'</xsl:variable> <xsl:choose> <xsl:when test="contains(.,'<')"> <xsl:value-of select="concat('<![CDATA[',.,']]>')"/> </xsl:when> <xsl:when test="contains(.,'>')"> <xsl:value-of select="concat('<![CDATA[',.,']]>')"/> </xsl:when> <xsl:when test="contains(.,'&')"> <xsl:value-of select="concat('<![CDATA[',.,']]>')"/> </xsl:when> <xsl:when test="contains(.,'"')"> <xsl:value-of select="concat('<![CDATA[',.,']]>')"/> </xsl:when> <xsl:when test="contains(.,$pos)"> <xsl:value-of select="concat('<![CDATA[',.,']]>')"/> </xsl:when> <xsl:otherwise> <xsl:value-of select="."/> </xsl:otherwise> </xsl:choose> </item> </xsl:template> </xsl:stylesheet>
result
XML, XSLT and XPATH Syntax (Toggle Plain Text)
<?xml version="1.0" encoding="utf-8"?> <root> <item><![CDATA[<]]></item> <item><![CDATA[>]]></item> <item><![CDATA[&]]></item> <item><![CDATA["]]></item> <item><![CDATA[']]></item> <item><![CDATA[Mc Donold's ]]></item> <item><![CDATA[Siegfried & Roy ]]></item> <item><![CDATA[a >b ]]></item> <item><![CDATA[a <b ]]></item> </root>
Helmut Hagemann
•
•
Join Date: Oct 2008
Posts: 95
Reputation:
Solved Threads: 5
•
•
•
•
What you want to do is not supported by XSL because including these characters in your XML document renders the XML document invalid.
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.
![]() |
Similar Threads
- Conditional Expression (Python)
- Conditional Compiler Directive? (C#)
- conditional database query (ColdFusion)
- Conditional Expression in JAVA (Java)
- probem with code of conditional operators (C)
Other Threads in the XML, XSLT and XPATH Forum
- Previous Thread: need help in xslt
- Next Thread: Parse JavaScript Variable to a XSLT Variable
| Thread Tools | Search this Thread |
api blogger blogging code delete development dynamiccreationofnvariablesinxslt error firstthreecharacterofastringrequired flipbook gdata google html include java link linspire linux microsoft news node openoffice overwrite precedence programming rss standards swf template transform variable w3c web xml xmlnotloading xmlonserver xsl xslt





