Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Reformat XML 1

Status
Not open for further replies.

userMikeD

Programmer
Nov 5, 2008
28
US
Hi, I'm having trouble with a simple transformation.

Here is my XML:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<translationSet>
	<key name="SMCAM1744_TITLE" translator="" translationDate="">
		<comment/>
		<contextImagePath/>
		<text>Camera 1744</text>
	</key>
	<key name="SMCAM1762_TITLE" translator="" translationDate="">
		<comment/>
		<contextImagePath/>
		<text>Camera  1762</text>
	</key>
	<key name="SMCAM1764_TITLE" translator="" translationDate="">
		<comment/>
		<contextImagePath/>
		<text>Camera  1764</text>
	</key>
</translationSet>

I want to extract the keys, and wrap the text node in CDATA like this:
Code:
<key name="SMCAM1744_TITLE"><text><![CDATA[Camera 1744]]></text></key>
<key name="SMCAM1762_TITLE"><text><![CDATA[Camera 1762]]></text></key>
<key name="SMCAM1764_TITLE"><text><![CDATA[Camera 1764]]></text></key>

But getting the CDATA tags in the output is proving difficult. Here is my XSL:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform"[/URL] cdata-section-elements="key text">
	<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

	<xsl:template match="/">
		<xsl:apply-templates select="translationSet/key"/>
	</xsl:template>

	<xsl:template match="translationSet/key">
		<xsl:element name="key">
			<xsl:attribute name="name">
				<xsl:value-of select="@name" />
			</xsl:attribute>
			<xsl:copy-of select="text" />
		</xsl:element>
	</xsl:template>

</xsl:stylesheet>

What I get is this:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<key name="SMCAM1744_TITLE">
  <text>Camera 1744</text>
</key><key name="SMCAM1762_TITLE">
  <text>Camera  1762</text>
</key><key name="SMCAM1764_TITLE">
  <text>Camera  1764</text>
</key>

I tried adding cdata-section-elements="key" to my output tag, but that didn't seem to work. Anyone see what I'm missing to ensure the text node has a cdata section? Also, is it possible to get each key on one line like in my example?

Thanks,
Mike
 
[1] It is an error:
[tt] Attribute 'cdata-section-elements' is invalid on 'xsl:stylesheet'.[/tt]

[2] Placement of the attribute should be this and should then be fine.
[tt]
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore][highlight]">[/highlight]
<xsl:eek:utput method="xml" version="1.0" encoding="UTF-8" indent="yes" [blue] cdata-section-elements="key text"[/blue] />[/tt]
 
Forgotten to edit out the key as the value of the cdata-section-elements attribute. It is not needed and it is not literally so.
[tt] cdata-section-elements=[highlight]"t[/highlight]ext"[/tt]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top