Solved

Transform xml address as CDATA

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

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 …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

822 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