[XSL-LIST Mailing List Archive Home] [By Thread] [By Date]

Re: [xsl] Converting attribute value to XML!


Subject: Re: [xsl] Converting attribute value to XML!
From: "G. Ken Holman" <gkholman@xxxxxxxxxxxxxxxxxxxx>
Date: Fri, 23 Jan 2004 11:19:49 -0500

At 2004-01-23 15:28 +0000, Rui Alberto L." Gonçalves wrote:
I have a document that looks like:

<result>
 <filed name="xpto" value="&lt;elem&gt;Hello&lt;elem&gt;"/>
</result>

and I want to transform to :
<result>
 <filed name="xpto">Hello</elem>
</result>

That isn't well-formed so it isn't XML ... do you mean


<result>
 <filed name="xpto"><elem>Hello</elem></filed>
</result>

If so, it is a small change to your stylesheet:

I tried:

<xsl:template match="*">
  <xsl:element name="{name()}">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
</xsl:template>

<xsl:template match="filed">
 <xsl:element name="filed">

You are missing the preservation of the name attribute:


<xsl:copy-of select="@name"/>

<xsl:value-of select ="@value"/>

Change the above to:


<xsl:value-of disable-output-escaping="yes" select="@value"/>

 </xsl:element>
</xsl:template>

but entities are not resolved. Any idea?

Resolved? The entities represent text characters and in your old stylesheet they went out as text characters so they had to be escaped. By turning off escaping you get what you want, but two caveats:


(1) - a processor is not obliged to respect d-o-e=

 (2) - you can produce a non-well-formed result that isn't XML if you
       have "bad" data in your text of your input

If you avoid d-o-e= then your output will *always* be well-formed XML.

I hope this helps.

.................... Ken

t:\ftemp>type rui.xml
<result>
 <filed name="xpto" value="&lt;elem&gt;Hello&lt;elem&gt;"/>
</result>

t:\ftemp>type rui.xsl
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                version="1.0">
<xsl:template match="*">
  <xsl:element name="{name()}">
    <xsl:apply-templates select="node()|@*"/>
  </xsl:element>
</xsl:template>

<xsl:template match="filed">
 <xsl:element name="filed">
   <xsl:copy-of select="@name"/>
   <xsl:value-of select ="@value" disable-output-escaping="yes"/>
 </xsl:element>
</xsl:template>

</xsl:stylesheet>
t:\ftemp>saxon rui.xml rui.xsl
<?xml version="1.0" encoding="utf-8"?><result>
 <filed name="xpto"><elem>Hello<elem></filed>
</result>
t:\ftemp>


-- Public courses: sign up for one or both soon to reserve your seat! Each week: Monday-Wednesday: XSLT/XPath; Thursday-Friday: XSL-FO Washington, DC: 2004-03-15 San Francisco, CA: 2004-03-22 Hong Kong, China: 2004-05-17 Bremen, Germany: 2004-05-24 World-wide on-site corporate, government & user group XML training

G. Ken Holman                 mailto:gkholman@xxxxxxxxxxxxxxxxxxxx
Crane Softwrights Ltd.          http://www.CraneSoftwrights.com/s/
Box 266, Kars, Ontario CANADA K0A-2E0    +1(613)489-0999 (F:-0995)
Male Breast Cancer Awareness  http://www.CraneSoftwrights.com/s/bc


XSL-List info and archive: http://www.mulberrytech.com/xsl/xsl-list




Current Thread
Keywords
xml