Displaying XML document in ASP.NET page using XSL Transform

  • Thread starter Barry Anderberg
  • Start date
B

Barry Anderberg

I have an XML document that I am trying to display in my ASP.NET page.

I am using an XSL Transform to display repeating XML data in a
specific format.

It reads the data, and displays it on my page, but there's a problem.

It runs all the data together on one line.

The XSL file is as follows:

-- begin xml ---

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/guestbook">
<xsl:apply-templates select="guestbook" />
<xsl:value-of select="name" />
<xsl:apply-templates select="guestbook" />
<xsl:value-of select="comment" />
</xsl:template>
</xsl:stylesheet>

-- end xml ---

I've tried putting <BR> in between the name and comment in the XSL
file and ASP.NET strips it out. I view the source in my browser and
for some reason ASP.NET just sends all the XML data as one string on
one line. I can't even figure out how to put spaces between the two
items.

I'd like to see:

Joe Blow

Hello, My Name is Joe.



Instead, what I get is:

Joe BlowHello, My Name is Joe.



Please help!!!
 
C

Chris Barber

First of all, that's all your XSLT will ever do irrespective of the XSLT
engine or framework.

You need to either generate HTML (the preferred method of output for XSLT
when trying to present data) or add the newline characters in yourself as
unicode character codes:

eg. newline is
[I think]

Try this example to see what should be happening (generates HTML):

XML:

<root>
<guestbook>
<entries>
<entry>
<name>Joe</name>
<comment>Nice place</comment>
</entry>
<entry>
<name>Jan</name>
<comment>Thanks for the lovely food</comment>
</entry>
<entry>
<name>Tim</name>
<comment>Thanks</comment>
</entry>
</entries>
</guestbook>
</root>

XSLT:

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:eek:utput indent="yes" method="xml" omit-xml-declaration="yes"/>
<!-- Generate the root html node -->
<xsl:template match="/">
<html>
<xsl:apply-templates/>
</html>
</xsl:template>
<!-- Match for the guestbook element -->
<xsl:template match="guestbook">
<h1>My Guestbook</h1>
<!-- Allow the default templates to process the remainder of the
document -->
<xsl:apply-templates/>
</xsl:template>
<!-- Match for the entry element -->
<xsl:template match="entry">
<h2>
<xsl:value-of select="name" />'s comment was:
</h2>
<p>
<xsl:value-of select="comment" />
</p>
<xsl:if test="not(position()=last())">
<hr/>
</xsl:if>
<!-- No need to apply further templates -->
</xsl:template>
</xsl:stylesheet>

Gives (XHTML):

<html>
<h1>My Guestbook</h1>
<h2>Joe's comment was:
</h2>
<p>Nice place</p>
<hr />
<h2>Jan's comment was:
</h2>
<p>Thanks for the lovely food</p>
<hr />
<h2>Tim's comment was:
</h2>
<p>Thanks</p>
</html>

Hope this helps.

Chris.

I have an XML document that I am trying to display in my ASP.NET page.

I am using an XSL Transform to display repeating XML data in a
specific format.

It reads the data, and displays it on my page, but there's a problem.

It runs all the data together on one line.

The XSL file is as follows:

-- begin xml ---

<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/guestbook">
<xsl:apply-templates select="guestbook" />
<xsl:value-of select="name" />
<xsl:apply-templates select="guestbook" />
<xsl:value-of select="comment" />
</xsl:template>
</xsl:stylesheet>

-- end xml ---

I've tried putting <BR> in between the name and comment in the XSL
file and ASP.NET strips it out. I view the source in my browser and
for some reason ASP.NET just sends all the XML data as one string on
one line. I can't even figure out how to put spaces between the two
items.

I'd like to see:

Joe Blow

Hello, My Name is Joe.



Instead, what I get is:

Joe BlowHello, My Name is Joe.



Please help!!!
 

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
473,755
Messages
2,569,537
Members
45,021
Latest member
AkilahJaim

Latest Threads

Top