HomeUser Control Panel (unavailable in archive)ForumsTutorialsArt GalleryResourcesMaps

HTML in XML

06-04-2008, 04:32 PM#1
TheSecretArts
I'm making an XML application and the code looks like this
XML File
Code:
<?xml version="1.0" encoding="utf-8" ?>
<?xml-stylesheet type="text/xsl" href="newsfeed.xsl"?>
<feed xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="newsfeed.xsd">
	<item>
		<title>Test</title>
		<date>6/4/08</date>
		<category>Tests</category>
		<author>TSA</author>
		<body>This is a test of my XML feed application</body>
	</item>
	<item>
		<title>Test 2</title>
		<date>6/4/08</date>
		<category>Tests</category>
		<author>TSA</author>
		<body>This is a test of multiple items</body>
	</item>
	<item>
		<title>Test 4</title>
		<date>6/5/08</date>
		<category>Tests</category>
		<author>TSA</author>
		<body>This is another date-sort test for multiple items</body>
	</item>
	<item>
		<title>Test 3</title>
		<date>6/3/08</date>
		<category>Tests</category>
		<author>TSA</author>
		<body>This is a date-sort test for multiple items</body>
	</item>
	<item>
		<title>Test 6</title>
		<date>6/10/08</date>
		<category>Tests</category>
		<author>TSA</author>
		<body>This is a long text test.<br />It is also an integrated HTML code test.  It tests integrity over a large amount of <b>characters</b> and several forms of <a href="">HTML</a> code.  Test failed.   How to fix?  Requires XSLT to solve...  Large Text Block Test: Passed<br />HTML Element Test: Failed<br />Date Sort: Passed<br />App Test: Passed</body>
	</item>
</feed>

XSL File
Code:
<?xml version="1.0" encoding="utf-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
	<html>
		<head>
			<title>News Feed</title>
			<style type="text/css">
			body{font-size:"16px";background-color:"#CCCCCC"}
			#title{font-size:"20px";font-weight:"bold"}
			#date{font-size:"12px"}
			#category{font-size:"12px"}
			#author{font-size:"12px"}
			#footer{font-size:"12px";text-align="center"}
			#sorttitle{font-size:"16px";font-weight:"bold"}
			hr#divider{color:"#33AACC";height:"16px"}
			</style>
		</head>
	<body>
		<table width="100%" align="top">
		<tr>
		<td width="85%">
		<xsl:for-each select="feed/item">
		<xsl:sort select="date" order="descending" /><div name="sortmethod"></div>
			<hr id="divider" />
			<div id="title"><xsl:value-of select="title" /></div>
			<div id="date"><xsl:value-of select="date" /></div>
			<div id="category"><xsl:value-of select="category" /></div>
			<div id="author"><xsl:value-of select="author" /></div>
			<div id="body"><xsl:value-of select="body" /></div>	
		</xsl:for-each>
		<hr id="divider" />
		</td><td align="right" valign="top"><div id="title">XML News Feed</div><br /><br /><div id="sorttitle">Sort by:</div>Date<br />Title<br />Category (Ascending)<br />Category (Descending)</td></tr></table>
		<hr />
		<div id="footer">This XML News Feed Application is built by TSA</div>
	</body>
	</html>
</xsl:template>

</xsl:stylesheet>

XSD File(Schema)
Code:
<?xml version="1.0" encoding="utf-8" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!-- simple elements -->
<xs:element name="title" type="xs:string" />
<xs:element name="date" type="xs:date" />
<xs:element name="category" type="xs:string" />
<xs:element name="author" type="xs:string" />
<xs:element name="body" type="xs:string" />
<!-- complex elements -->
<xs:element name="feed">
	<xs:complexType>
		<xs:sequence>
			<xs:element name="item">
				<xs:complexType>
					<xs:sequence>
						<xs:element ref="title" />
						<xs:element ref="date" />
						<xs:element ref="category" />
						<xs:element ref="author" />
						<xs:element ref="body" />
					</xs:sequence>
				</xs:complexType>
			</xs:element>
		</xs:sequence>
	</xs:complexType>
</xs:element>
</xs:schema>
						

The problem? I don't know how to get HTML Data contained in the XML <body> tags to be parsed as HTML. IE. The <br />s cause linebreaks.
06-04-2008, 08:46 PM#2
Vexorian
parsed by what?
06-04-2008, 10:20 PM#3
TheSecretArts
okay, let me explain.

Code:
<body>This is a long text test.<br />It is also an integrated HTML code test.  It tests integrity over a large amount of <b>characters</b> and several forms of <a href="">HTML</a> code.  Test failed.   How to fix?  Requires XSLT to solve...  Large Text Block Test: Passed<br />HTML Element Test: Failed<br />Date Sort: Passed<br />App Test: Passed</body>
when transformed by the XSL file becomes this
Quote:
This is a long text test.It is also an integrated HTML code test. It tests integrity over a large amount of characters and several forms of HTML code. Test failed. How to fix? Requires XSLT to solve... Large Text Block Test: PassedHTML Element Test: FailedDate Sort: PassedApp Test: Passed
but I want it to be transformed to look like this
Quote:
This is a long text test.
It is also an integrated HTML code test. It tests integrity over a large amount of characters and several forms of HTML code. Test failed. How to fix? Requires XSLT to solve... Large Text Block Test: Passed
HTML Element Test: Failed
Date Sort: Passed
App Test: Passed
How do I achieve that.
I think I might have a solution. Make the body contain many sub elements (IE; PureString, BoldText, LinkText, Image, etc.) that get interpreted by the XSL file.
06-05-2008, 07:48 PM#4
Alevice
You have to declare an xhtml doctype for the output. Preferably within a namespace, but that's up to you.

Like this:
PHP Code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <
xsl:output encoding="UTF-8" indent="no"
    
doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN"
    
doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"
    
omit-xml-declaration="yes" />
  <
xsl:param name="html-content-type" />
  <
xsl:template match="/LundieBlog">
    <
html xmlns="http://www.w3.org/1999/xhtml">
      <
head>
        <
meta http-equiv="Content-Type"
        
content="{$html-content-type}/>
        <
title>News Feed</title>
        <
style type="text/css">
            
body{font-size:"16px";background-color:"#CCCCCC"}
            
#title{font-size:"20px";font-weight:"bold"}
            #date{font-size:"12px"}
            #category{font-size:"12px"}
            #author{font-size:"12px"}
            #footer{font-size:"12px";text-align="center"}
            #sorttitle{font-size:"16px";font-weight:"bold"}
             
hr#divider{color:"#33AACC";height:"16px"}
        
</style>
      </
head>
      <
body>
        <
xsl:for-each select="feed/item">
        <
xsl:for-each select="feed/item">
        <
xsl:sort select="date" order="descending" /><div name="sortmethod"></div>
            <
hr id="divider" />
            <
div id="title"><xsl:value-of select="title" /></div>
            <
div id="date"><xsl:value-of select="date" /></div>
            <
div id="category"><xsl:value-of select="category" /></div>
            <
div id="author"><xsl:value-of select="author" /></div>
            <
div id="body"><xsl:value-of select="body" /></div>    
        </
xsl:for-each>
        <
hr id="divider" />
        </
td><td align="right" valign="top"><div id="title">XML News Feed</div><br /><br /><div id="sorttitle">Sort by:</div>Date<br />Title<br />Category (Ascending)<br />Category (Descending)</td></tr></table>
        <
hr />
        <
div id="footer">This XML News Feed Application is built by TSA</div>
      </
body>
    </
html>
  </
xsl:template>
</
xsl:stylesheet


(and yes, I do like my xhtml strict and valid)
06-06-2008, 01:06 AM#5
TheSecretArts
Thanks, but your solution killed all of my CSS. And HTML code in the XML file is still not maintained.
and you don't have the XML declaration at the start of yours
PHP Code:
<?xml version="1.0" encoding="utf-8"?>

this is my new XSL file
PHP Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output encoding="UTF-8" indent="no" 
    doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" 
    omit-xml-declaration="yes" /> 
  <xsl:param name="html-content-type" /> 
  <xsl:template match="/" xmlns:TheSecretArts="uri:1">
    <html xmlns="http://www.w3.org/1999/xhtml">
      <head>
    <meta http-equiv="Content-Type" 
        content="{$html-content-type}" />
        <title>The Wars of Warcraft: The First War News Feed</title>
    <style type="text/css">
        body{font-size:"20px"}
        #small{font-size:"12px"}
        #title{font-size:"24px"}
        hr{height:"15px";color:"#3399ff"}
    </style>
      </head>
      <body>
        <p>This is the news feed for all information on things regarding the progress of The Wars of Warcraft: The First War</p>
    <table width="100%" cellpadding="0" height="90%"><tr><td width="90%" valign="top">
        <hr />
        <xsl:for-each select="feed/item">
    <xsl:sort select="date" order="descending" />
      <b><div id="title">
            <xsl:value-of select="title" /></div></b>
      <div id="small">
            Date: <xsl:value-of select="date" />
            <br />
            Category: <xsl:value-of select="category" />
            <br />
            Author: <xsl:value-of select="author" />
            <br />
      </div>
          <xsl:value-of select="body" />
      <br />
      <hr />
      <br />
        </xsl:for-each>    <center><div id="small">This XML News feed created entirely by TheSecretArts.  Training from <a href="www.w3schools.com">W3Schools.com</a>.  This site was written using XML Notepad 2007.<br />This feed is updated by a manually run PHP update script written by TheSecretArts.</div></center>
</td><td bgcolor="#3399ff" valign="top">Nav stuff</td></tr></table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>
06-06-2008, 03:30 PM#6
Alevice
Oooooh, I get it, you had xhtml markup within your xml file. I don't recall off hand if you can have xhtml tags outside an <html> tag, but you could try declaring an html namespace for all such elements you need, so the xsl stylesheet knows that those tags do not belong to the newsfeed schema and tries to keep them.

Either that, of you will have to transform each manually.

EDIT: Possible help: http://developers.slashdot.org/devel....shtml?tid=156

Also, "inline" info from another language/format within html should be always in a <![CDATA[ ... ]]> tag to prevent interpreters go retarded. It also makes the w3c xml/html validator act nice to you.

And sorry about my xsl stylesheet. I wrote it on a rush.

EDIT: A dirty hack I forgot about would be the disable-output-escaping parameter on the value-of tag: I would try to avoid it nevertheless. http://www.dpawson.co.uk/xsl/sect2/N2215.html