Solved

Transform xml address as CDATA

Posted on 2010-08-21
5
658 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 250 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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Preface In the first article: A Better Website Login System (http://www.experts-exchange.com/A_2902.html) I introduced the EE Collaborative Login System and its intended purpose. In this article I will discuss some of the design consideratio…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Suggested Courses

634 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