XML/XSL HTML problem

C

ChrisEvans

Hi there,
I've got an XML file which uses an XSL stylesheet. Problem is:

<br />

tags don't work for me when the content is gathered from the xml.
Example:

<main>
<title>Hi</title>
<content>Hello, how are you?<br /><br />I'm fine.</content>
</main>

Then in the stylesheet its positioned in the correct place etc and
using <xsl:value-of select="content" /> - trouble is the <br /> doesn't
do anything.

Help would be appreciated!

Thanks,

Chris
 
M

Martin Honnen

ChrisEvans wrote:

I've got an XML file which uses an XSL stylesheet. Problem is:

<br />

tags don't work for me when the content is gathered from the xml.
Example:

<main>
<title>Hi</title>
<content>Hello, how are you?<br /><br />I'm fine.</content>
</main>

Then in the stylesheet its positioned in the correct place etc and
using <xsl:value-of select="content" /> - trouble is the <br /> doesn't
do anything.

<xsl:value-of select="content" /> outputs the string value of the
element <content>, if the element has empty child elements like <br />
then these indeed do not in any way show up with value-of.
What you probably want is recursive processing of child nodes of
<content> e.g.
<xsl:template match="content">
<div>
<xsl:apply-templates />
</div>
</xsl:template>
and then a template that copies <br> elements from the input XML to the
XSLT output
<xsl:template match="br">
<xsl:copy />
</xsl:template>
 
L

Luke Dalessandro

Martin said:
ChrisEvans wrote:




<xsl:value-of select="content" /> outputs the string value of the
element <content>, if the element has empty child elements like <br />
then these indeed do not in any way show up with value-of.
What you probably want is recursive processing of child nodes of
<content> e.g.
<xsl:template match="content">
<div>
<xsl:apply-templates />
</div>
</xsl:template>
and then a template that copies <br> elements from the input XML to the
XSLT output
<xsl:template match="br">
<xsl:copy />
</xsl:template>

Logically the next thing you are going to run into is transforming
something like

<a href="somewhere">somewhere</a>

from your xml (at least I did). You use the same construct as Martin,
but instead of just copying the tag and applying templates to its
content, you need to copy the attributes as well.

<xsl:template match="a">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

Then, you are going to start accumulating a list of all of these types
of standard html markup that you want to allow in your xml (like style
etc...) and you are going to wind up with tons of these simple template
matches and think to yourself, "there must be an easier way to do this,"
and there is.

<xsl:template match="*">
<xsl:copy>
<xsl:copy-of select="@*" />
<xsl:apply-templates />
</xsl:copy>
</xsl:template>

Will match and copy ALL UNSPECIFIED TAGS, and their attributes, in your
xml. By unspecified I mean anything that you haven't explicitely written
a template for.

Hope this helps... it just looked like you were on the same path as I
was, and it took me lots of pain to figure this simple stuff out. Good luck.

Luke
 

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

Forum statistics

Threads
473,999
Messages
2,570,243
Members
46,836
Latest member
login dogas

Latest Threads

Top