Forum Moderators: open

Message Too Old, No Replies

Including HTML formatting in XML content

How can I get a <b> tag to bold a word within an XML element?

         

StupidScript

10:16 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



journal.dtd

<!ELEMENT journal (entry+)>
<!ELEMENT entry (date,text)>
<!ELEMENT date (#PCDATA)>
<!ELEMENT text (#PCDATA)>

journal.xml


<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="journal.xsl"?>
<!DOCTYPE journal SYSTEM "journal.dtd">
<journal>
<entry>
<date>03/31/2007</date>
<text>entry with [b]<b>bold</b>[/b] word</text>
</entry>
</journal>

journal.xsl


<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>XSLT Test</title></head>
<body>
<xsl:for-each select="journal/entry">
<em><xsl:value-of select="date" /></em><br />
<xsl:value-of select="text" />
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>
</xsl:transform>

As you can see from this test, wrapping the entire element (in the XSL file) with EM tags formats that entire element as italics. However the <b> tags within the TEXT element are not acted on by the browser ... it just ignores them. I've tried including the B tag in various places within the DTD with no luck.

Am I correct in that a < character is illegal within an element's content, anyway?

I'm trying to discover if I can use XML files for storing this type of data with modest formatting, or if I should continue to use the HTML files I've been using.

Thank you in advance for any thoughts on this.

cmarshall

10:54 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've got it all loaded up in <oXygen/>, and I'll check it out later when I get back. You should be able to do it, and the answer is probably in the stylesheet.

You may want to try an <xsl:apply-templates/> instead of a value-of

cmarshall

11:02 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This works, but there's probably a more graceful way to do it.

Gotta run.

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<html>
<head><title>XSLT Test</title></head>
<body>
<xsl:for-each select="journal/entry">
<em><xsl:value-of select="date" disable-output-escaping="yes" /></em><br />
<xsl:apply-templates/>
<hr />
</xsl:for-each>
</body>
</html>
</xsl:template>

<xsl:template match="text">
<em><xsl:apply-templates/></em>
</xsl:template>

<xsl:template match="b">
<b><xsl:apply-templates/></b>
</xsl:template>
</xsl:transform>

StupidScript

11:18 pm on Apr 3, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That sure does work! Thanks a lot.

cmarshall

1:38 am on Apr 4, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that it will only work for <b></b> elements. You'll have to do a template for each formatter. The plus side is that you can implement BBCodes with an XSL stylesheet, or filter for only certain codes.