formatting text with XSLT

Discussion in 'XML' started by Steven, Apr 1, 2005.

  1. Steven

    Steven Guest

    I have song lyrics typed out in my XML file in paragraphcs (verse,
    chorus, verse, etc) but when I load them with my XSLT stylesheet, the
    paragraphs are lost and all the words are jumbled together. I've tried
    everything I could find in Microsoft's SDK help file, but nothing
    worked. any ideas?
     
    Steven, Apr 1, 2005
    #1
    1. Advertising

  2. In message <>,
    Steven <> writes
    >I have song lyrics typed out in my XML file in paragraphcs (verse,
    >chorus, verse, etc) but when I load them with my XSLT stylesheet, the
    >paragraphs are lost and all the words are jumbled together. I've tried
    >everything I could find in Microsoft's SDK help file, but nothing
    >worked. any ideas?


    Are you willing to share examples of your XML, and your XSLT stylesheet,
    with the group? We might then be able to help.

    --
    Richard Light
    SGML/XML and Museum Information Consultancy
     
    Richard Light, Apr 1, 2005
    #2
    1. Advertising

  3. Steven

    Steven Guest

    -------------------XML File-----------------------

    <?xml version="1.0"?>
    <?xml-stylesheet type="text/xsl" href="/BBN/songs.xsl"?>

    <data>
    <artist>Hoobastank</artist>
    <album>The Reason</album>
    <release>2004</release>
    <song title="The Reason" track="08">
    I'm not a perfect person
    As many things I wish I didn't do
    But I continue learning
    I never meant to do those things to you
    And so I have to say before I go
    That I just want you to know

    I've found a reason for me
    To change who I used to be
    A reason to start over new
    And the reason is you
    </song>
    </data>

    ^^^^^^^^^^^^^^^^^^^^^^^^^^XML File^^^^^^^^^^^^^^^^^^

    ----------------------XSL File----------------

    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="data">
    <html>
    <head>
    <title>
    <xsl:value-of select="artist"/> -
    <xsl:value-of select="album"/>
    </title>
    </head>
    <body bgcolor="#FFFFCC">
    <table border="0" width="835" cellspacing="3"
    cellpadding="2">
    <tr>
    <td align="center">
    <font size="5"><i>
    <xsl:value-of
    select="artist"/> - <xsl:value-of select="album"/>
    </i></font>
    </td>
    </tr>
    <xsl:for-each select="song">
    <xsl:sort select="./@track"/>
    <tr>
    <td>

    <xsl:apply-templates select="."/>
    </td>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="song">
    <font size="4"><b>
    <xsl:value-of select="@title"/>
    </b></font>
    <br/>
    <xsl:value-of select="."/>
    <p/>
    </xsl:template>

    </xsl:stylesheet>
    ^^^^^^^^^^^^^^^^^^^^^^^^XSL File^^^^^^^^^^^^^^^^^^^^^^^
     
    Steven, Apr 1, 2005
    #3
  4. The specific problem is that this code:

    <xsl:value-of select="."/>
    <p/>

    outputs the text of your <song> element, followed by an empty paragraph.
    If you want the text _inside_ the paragraph, do:

    <p>
    <xsl:value-of select="."/>
    </p>

    However, this won't get you a lot further: the basic markup of each song
    is a bit on the light side. If you had e.g.:

    <verse>
    <l>I'm not a perfect person</l>
    <l>As many things I wish I didn't do</l>
    ....
    </verse>
    <verse>
    <l>I've found a reason for me</l>
    ....

    it would be possible to make much better use of the information, and
    specifically it would make it easy to format it nicely using XSLT.

    Richard

    In message <>,
    Steven <> writes
    >-------------------XML File-----------------------
    >
    ><?xml version="1.0"?>
    ><?xml-stylesheet type="text/xsl" href="/BBN/songs.xsl"?>
    >
    ><data>
    > <artist>Hoobastank</artist>
    > <album>The Reason</album>
    > <release>2004</release>
    > <song title="The Reason" track="08">
    > I'm not a perfect person
    > As many things I wish I didn't do
    > But I continue learning
    > I never meant to do those things to you
    > And so I have to say before I go
    > That I just want you to know
    >
    > I've found a reason for me
    > To change who I used to be
    > A reason to start over new
    > And the reason is you
    > </song>
    ></data>
    >
    >^^^^^^^^^^^^^^^^^^^^^^^^^^XML File^^^^^^^^^^^^^^^^^^
    >
    >----------------------XSL File----------------
    >
    ><?xml version="1.0"?>
    ><xsl:stylesheet version="1.0"
    >xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    ><xsl:template match="data">
    > <html>
    > <head>
    > <title>
    > <xsl:value-of select="artist"/> -
    ><xsl:value-of select="album"/>
    > </title>
    > </head>
    > <body bgcolor="#FFFFCC">
    > <table border="0" width="835" cellspacing="3"
    >cellpadding="2">
    > <tr>
    > <td align="center">
    > <font size="5"><i>
    > <xsl:value-of
    >select="artist"/> - <xsl:value-of select="album"/>
    > </i></font>
    > </td>
    > </tr>
    > <xsl:for-each select="song">
    > <xsl:sort select="./@track"/>
    > <tr>
    > <td>
    >
    ><xsl:apply-templates select="."/>
    > </td>
    > </tr>
    > </xsl:for-each>
    > </table>
    > </body>
    > </html>
    ></xsl:template>
    >
    ><xsl:template match="song">
    > <font size="4"><b>
    > <xsl:value-of select="@title"/>
    > </b></font>
    > <br/>
    > <xsl:value-of select="."/>
    > <p/>
    ></xsl:template>
    >
    ></xsl:stylesheet>
    >^^^^^^^^^^^^^^^^^^^^^^^^XSL File^^^^^^^^^^^^^^^^^^^^^^^
    >


    --
    Richard Light
    SGML/XML and Museum Information Consultancy
     
    Richard Light, Apr 1, 2005
    #4
  5. Steven

    Steven Guest

    so there's no way to just have it displayed/copied exactly the way it's
    typed in the XML?
     
    Steven, Apr 1, 2005
    #5
  6. In message <>,
    Steven <> writes
    >so there's no way to just have it displayed/copied exactly the way it's
    >typed in the XML?


    Remember you are outputting this to HTML, so you are stuck with what a
    browser will do to your output. If there isn't, as a minimum, a <br/>
    tag at the end of each line, it will simply render your lyrics as one
    big paragraph.

    It _is_ possible to write XSLT to convert newlines in your source to
    <br/> tags in your output (there was a thread about this a day or two
    back over in microsoft.public.xsl), but marking up the source sensibly
    in the first place is (in my view) a much cleaner way of going about the
    job.

    Richard
    --
    Richard Light
    SGML/XML and Museum Information Consultancy
     
    Richard Light, Apr 1, 2005
    #6
  7. Steven wrote:

    > so there's no way to just have it displayed/copied exactly the way it's
    > typed in the XML?


    If you are transforming to HTML you can use the <pre> element e.g.

    <xsl:template match="song">
    <h2><xsl:value-of select="@title" /></h2>
    <pre><xsl:value-of select="." /></pre>
    </xsl:template>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Apr 1, 2005
    #7
  8. Steven

    Steven Guest

    hey, the <pre> tag did it! Thanks Martin and Richard. That's what I
    needed, because I didn't want the lyrics marked up any more than they
    were.
     
    Steven, Apr 1, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Stylus Studio
    Replies:
    0
    Views:
    681
    Stylus Studio
    Aug 3, 2004
  2. kevin bailey

    conditional formatting in XSLT

    kevin bailey, Aug 13, 2003, in forum: XML
    Replies:
    0
    Views:
    1,255
    kevin bailey
    Aug 13, 2003
  3. Michael Reiche
    Replies:
    3
    Views:
    10,853
    Michael Reiche
    Feb 5, 2004
  4. Replies:
    1
    Views:
    403
    Joseph Kesselman
    Jun 7, 2007
  5. cp
    Replies:
    2
    Views:
    403
Loading...

Share This Page