2

I have a problem with my XML that I am trying to display on my ASP.NET page that I could do with some help with. What I would like to do is display it on a multi-line so I have an XML file that looks like this:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="News.xslt" ?>
<newslist>
  <news>
    <date>20th June 2010</date>
    <detail>Detail line 1.
            Detail Line 2</detail>
  </news>
  <news>
    <date>18th June 2010</date>
    <detail>Some more details</detail>
  </news>
</newslist>

And I have an XSLT file that looks like this:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:template match="/">
    <HTML>
      <BODY>
        <xsl:for-each select="newslist/news">
          <xsl:sort select="date" order="descending"/>
          <br />
          <h3><xsl:value-of select="date" /></h3>
          <ul>
            <p><xsl:value-of select="detail" /></p>
          </ul>
        </xsl:for-each>
      </BODY>
    </HTML>
  </xsl:template>
</xsl:stylesheet>

When it displays the first detail line everything is on the same line. I've done some digging about and I have tried the following:

  1. xml:space="preserve" in the XSLT file
  2. in the XML file
  3. <br />
  4. I've even tried leaving it as it is.

I am using Microsoft Visual Web Developer 2010. The control I am using is the XML control under the standard tab, and the language I am using is C#, if that helps any.

If this has already been answered and I haven't found it yet can you please point me at it.

Thanks for your help.

2
  • Did you add the <br/> to the XML source file or to the XSLT file? You should be able to put hte <br/> into your XML source file without any problems. Commented Jun 21, 2010 at 21:38
  • Good question (+1). See my answer for a complete solution. Commented Jun 21, 2010 at 21:48

1 Answer 1

3

This transformation:

<xsl:stylesheet version="1.0"
 xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

  <xsl:template match="/">
    <HTML>
      <BODY>
        <xsl:for-each select="newslist/news">
          <xsl:sort select="date" order="descending"/>
          <br />
          <h3><xsl:value-of select="date" /></h3>
          <ul>
            <p><xsl:apply-templates select="detail"/></p>
          </ul>
        </xsl:for-each>
      </BODY>
    </HTML>
  </xsl:template>

  <xsl:template match="detail/text()" name="textLines">
   <xsl:param name="pText" select="."/>

    <xsl:choose>
        <xsl:when test="contains($pText, '&#xA;')">
          <xsl:value-of select="substring-before($pText, '&#xA;')"/>
          <br />
          <xsl:call-template name="textLines">
            <xsl:with-param name="pText" select=
             "substring-after($pText, '&#xA;')"
             />
          </xsl:call-template>
        </xsl:when>
        <xsl:otherwise><xsl:value-of select="$pText"/></xsl:otherwise>
    </xsl:choose>
  </xsl:template>
</xsl:stylesheet>

when applied on the provided XML document:

<newslist>
  <news>
    <date>20th June 2010</date>
    <detail>Detail line 1.
            Detail Line 2</detail>
  </news>
  <news>
    <date>18th June 2010</date>
    <detail>Some more details</detail>
  </news>
</newslist>

produces the wanted, correct result:

<HTML>
    <BODY><br><h3>20th June 2010</h3>
        <ul>
            <p>Detail line 1.<br>            Detail Line 2</p>
        </ul><br><h3>18th June 2010</h3>
        <ul>
            <p>Some more details</p>
        </ul>
    </BODY>
</HTML>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.