Link to home
Start Free TrialLog in
Avatar of Nigel Keith-Walker
Nigel Keith-WalkerFlag for Australia

asked on

Transform xml address as CDATA

I am using xsl to transform an xml file.  It contains an address.  This address could contain unsuitable characters eg &.  I want to output the address within CDATA.
e.g <![CDATA[Smith & Co]]>
How can Ii code this in the xsl?  Obviously the coding below does not process anything within the CDATA.  
<addr>
                <xsl:text><![CDATA[</xsl:text>
                <xsl:value-of select="AR/AR_Addr1"/>
                <xsl:text>]]></xsl:text>
              </addr>

Open in new window

Avatar of grepll
grepll
Flag of Czechia image

First, you don't have to bother whether there is CDATA section in the output, the & and other special chars will be always escaped correctly in the output file.

If you really want to have a CDATA section there, use attribute cdata-section-elements of xsl:output. See example below. If you need more elements generated using CDATA, put them there separeted by spaces, e.g. cdata-section-elements="addr email"
<?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" cdata-section-elements="addr"/>

<xsl:template match="/">
	<addr>
		<xsl:text>Smith &amp; Co</xsl:text>
	</addr>
</xsl:template>

</xsl:stylesheet>

Open in new window

Avatar of Nigel Keith-Walker

ASKER

So that I should not have to use CDATA to output addres information, because it will automatically replace unusual characters wuth &lt etc.

The alternative would be to set up matches for the adress line.  Since I have a few lines, would this mean individual matches?
<?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" cdata-section-elements="addr1" "addr2" "addr3"/>

<xsl:template match="/">
	<addr1>
		<xsl:value-of select="AR/AR_Addr1"/>
	</addr1>
</xsl:template>
<xsl:template match="/">
	<addr2>
		<xsl:value-of select="AR/AR_Addr2"/>
	</addr2>
</xsl:template>
<xsl:template match="/">
	<addr3>
		<xsl:value-of select="AR/AR_Addr3"/>
	</addr3>
</xsl:template>

</xsl:stylesheet>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of grepll
grepll
Flag of Czechia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Apologies - I thought that I had acceppted this a few days ago - but must have failed to submit/save the response.  I have removed the CDATA and the & is translated to &amp.
Glad to hear that you solved it.

While looking at my previous post, one note for the record: Any XML document must have exactly one root tag, I forgot to put it in the output document. Apology for the mistake.
<xsl:template match="/">
    <addresses>
        <addr1>
            <xsl:value-of select="AR/AR_Addr1"/>
        </addr1>
        <addr2>
            <xsl:value-of select="AR/AR_Addr2"/>
        </addr2>
        <addr3>
            <xsl:value-of select="AR/AR_Addr3"/>
        </addr3>
    </addresses>
</xsl:template>

Open in new window