Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Transform xml address as CDATA

Posted on 2010-08-21
5
Medium Priority
?
666 Views
Last Modified: 2013-11-18
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

0
Comment
Question by:Nigel Keith-Walker
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 3

Expert Comment

by:grepll
ID: 33493121
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

0
 

Author Comment

by:Nigel Keith-Walker
ID: 33493299
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

0
 
LVL 3

Accepted Solution

by:
grepll earned 1000 total points
ID: 33493352
> So that I should not have to use CDATA to output addres information, because it will automatically replace unusual characters wuth &lt etc.

Exactly. You can use CDATA - for example when you expect to do hand editing of the output documen - but you really don't have to.

> The alternative would be to set up matches for the adress line.  Since I have a few lines, would this mean individual matches?

Whenever you want to generate CDATA section in XSLT, you have to add desired element is cdata-section-elements. In your case it would be

cdata-section-elements="addr1 addr2 addr3"
(but not cdata-section-elements="addr1" "addr2" "addr3")

The rest is normal XSLT workflow, but the code you posted not correct.

I don't know what data you are transforming and what is the desired output. Probably you ment something like this (see below). However, that's no more matter of generating CDATA sections, so I'd suggest you to post new question if you have further issues regarding XSLT in general.
<?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>
        <addr2>
                <xsl:value-of select="AR/AR_Addr2"/>
        </addr2>
        <addr3>
                <xsl:value-of select="AR/AR_Addr3"/>
        </addr3>
</xsl:template>

</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:Nigel Keith-Walker
ID: 33526331
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.
0
 
LVL 3

Expert Comment

by:grepll
ID: 33528605
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

0

Featured Post

Give Your Engineering Team a Productivity Boost

Learn why container technology is so powerful and how it can provide your team with productivity gains and other benefits.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question