|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Outputting XHTML from XML using XSLT
Hello!
I'm having a few difficulties taking my xml document, applying a XSLT style to it and having it come out as XHTML in my browser. What I keep getting it just a formatted XML document when I view the source, but according to all the examples and references I've looked at it this is wrong. Here are the key parts of the files I'm using: XML Code:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="./styles/xsl/index.xsl"?>
<news>
<post>
<author>author</author>
<subject>subject</subject>
<body>text</body>
</post>
</news>
XSL Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns="http://www.w3.org/1999/xhtml">
<xsl:output method="xml" version="1.0" encoding="UTF-8" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" indent="no" omit-xml-declaration="no" />
<xsl:template match="/news">
<html>
<head>
<title>title</title>
</head>
<body>
<xsl:for-each select="post">
<h2>Posted on Monday, August 23rd 2004 @ 5:06pm by <a href="#"><xsl:value-of select="author" /></a></h2>
<h1><xsl:value-of select="subject" /></h1>
<p><xsl:value-of select="body" /></p>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
and what I should see output by my browser: Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html> <body> ...etc... </body> </html> but what I actually see is something like this: Code:
<?xml version="1.0" encoding="UTF-8"?> <?xml-stylesheet type="text/xsl" href="./styles/xsl/index.xsl"?> <news> <post> <author>author</author> <subject>subject</subject> <body>text</body> </post> </news> Any help or tips with this would be greatly appreciated. I've been purging every resource available to try and find out my mistake, but to no avail. As an added note, I'm using PHP to send "content-type:text/xml" to the header while I'm generating my XML file. Would this be overiding the XSL file in any way when it's trying to output XHTML? Thanks, raisin |
|
#2
|
||||
|
||||
|
Quote:
http://www.w3schools.com/xsl/el_output.asp
__________________
# Jeremy Explain your problem instead of asking how to do what you decided was the solution. |
|
#3
|
|||
|
|||
|
I tried using html for the method again, but it's still outputting the file the exact same way as before. The only thing that seems to have changed is that the page I'm using it on isn't rendering correctly (but I'm not really worried about that).
|
|
#4
|
||||
|
||||
|
First, make sure the xsl stylesheet is really being applied.
Move the stylesheet to the same directory as the xml and change the href appropriately. Also, you may wish to rename your <body> tag in the xml so it's not duplicating the name of an HTML tag. It did not cause a problem for me, but it's good to eliminate possible problems. Your xsl applies correctly in FireFox and IE6 (after I added the missing </body></html> tags). What browser are you using to view it in, and are you letting the browser do the transformation (e.g. you are typing the xml file location into the address bar)? IE browsers earlier than 6 may not render the document correctly, if at all, as it supports only the working draft of xslt, not the current recommendation. Current Gecko browsers (like FireFox, Mozilla, Netscape 7) render correctly, as does IE6. Opera has no XSLT support at all, although I understand the next release will have it. In that browser all you would see (from your xml) is: author subject text If none of this helps, answer the questions and, if you can, provide the full xml/xsl or a link to them-sometimes the 'buggy' part is hard to spot. |
|
#5
|
|||
|
|||
|
Thanks for your replies!
I figured out that the xml-stylesheet was where I was going wrong. My browser was trying to parse the xsl file, but it failed because xml-stylesheet is really made more specifically for CSS (to replace the <link> in html 4.01), or so it seems at w3. I'm generating the xml and then parsing it through the stylesheet via php now, and it's working beautifully. A reference is here for anyone who might be interested. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages - More > XML Programming > Outputting XHTML from XML using XSLT |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|