Solved

Transform xml address as CDATA

Posted on 2010-08-21
5
630 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
  • 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
File names with & character 6 60
I starting with php 12 94
XML namaspace 2 37
Problem to ToolkitScriptManager 2 30
Introduction Knockoutjs (Knockout) is a JavaScript framework (Model View ViewModel or MVVM framework).   The main ideology behind Knockout is to control from JavaScript how a page looks whilst creating an engaging user experience in the least …
Browsers only know CSS so your awesome SASS code needs to be translated into normal CSS. Here I'll try to explain what you should aim for in order to take full advantage of SASS.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now