XML/XSL HTML problem

ChrisEvans

Usenet Archive
Joined
Dec 24, 2004
Messages
4
Reaction score
0
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
 

Martin Honnen

Usenet Archive
Joined
Apr 8, 2000
Messages
6,174
Reaction score
0
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>
 

Luke Dalessandro

Usenet Archive
Joined
Sep 23, 2004
Messages
24
Reaction score
0
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

No members online now.

Forum statistics

Threads
548,208
Messages
3,104,799
Members
249,594
Latest member
henrywalsh

Latest Threads

Top