XSLT: processing embedded (X)HTML

J

je

Slightly newbie question: I have some XML to be transformed which
contains (inter
alia) tags containing text:
<tag>Foo</tag>
which sould appear unchanged in the transformed output. Using xopy-of
would seem
to be the thing to use. However, the enclosed text sometimes needs to
contain
HTML:
<tag>Click <a href="foo.html">here</a></tag>
At the momemt I work around this by wrapping the contents in
<html>...</html>,
and using templates like this:
<xsl:template match='tag'>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match='html'>
<xsl:copy-of select="*"/>
</xsl:template>
This means I always have to generate <tag><html> ... </html></tag>,
which strikes
me as seriously ugly. Can anyone tell me a better way to deal with
this?

TIA...
 
P

Peter Flynn

Slightly newbie question: I have some XML to be transformed which
contains (inter
alia) tags containing text:
<tag>Foo</tag>
which sould appear unchanged in the transformed output. Using xopy-of
would seem
to be the thing to use. However, the enclosed text sometimes needs to
contain
HTML:
<tag>Click <a href="foo.html">here</a></tag>

But copy-of will preserve everything inside the enclosing element.
At the momemt I work around this by wrapping the contents in
<html>...</html>,
and using templates like this:
<xsl:template match='tag'>
<xsl:apply-templates/>
</xsl:template>
<xsl:template match='html'>
<xsl:copy-of select="*"/>
</xsl:template>
This means I always have to generate <tag><html> ... </html></tag>,
which strikes
me as seriously ugly. Can anyone tell me a better way to deal with
this?

Don't add the extra <html>...</html>, just use copy-of:

<xsl:template match="tag">
<xsl:copy-of select="."/>
</xsl:template>

///Peter
 
A

Andy Dingley

Slightly newbie question: I have some XML to be transformed which
contains (inter
alia) tags containing text:
<tag>Foo</tag>
which sould appear unchanged in the transformed output.

Change the design. That sort of input is a bad solution, especially if
it's an external feed.

Go read up on RSS and its many approaches to this problem. Google for
"Dive into mark" and "myth of rss compatibility" for a good article on
this topic. Also read the Atom spec.

Generally entity-encoding is the better way to go.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top