XML/XSL HTML problem

Discussion in 'XML' started by ChrisEvans, Dec 24, 2004.

  1. ChrisEvans

    ChrisEvans Guest

    Hi there,
    I've got an XML file which uses an XSL stylesheet. Problem is:

    <br />

    tags don't work for me when the content is gathered from the xml.
    Example:

    <main>
    <title>Hi</title>
    <content>Hello, how are you?<br /><br />I'm fine.</content>
    </main>

    Then in the stylesheet its positioned in the correct place etc and
    using <xsl:value-of select="content" /> - trouble is the <br /> doesn't
    do anything.

    Help would be appreciated!

    Thanks,

    Chris
     
    ChrisEvans, Dec 24, 2004
    #1
    1. Advertising

  2. ChrisEvans wrote:


    > I've got an XML file which uses an XSL stylesheet. Problem is:
    >
    > <br />
    >
    > tags don't work for me when the content is gathered from the xml.
    > Example:
    >
    > <main>
    > <title>Hi</title>
    > <content>Hello, how are you?<br /><br />I'm fine.</content>
    > </main>
    >
    > Then in the stylesheet its positioned in the correct place etc and
    > using <xsl:value-of select="content" /> - trouble is the <br /> doesn't
    > do anything.


    <xsl:value-of select="content" /> outputs the string value of the
    element <content>, if the element has empty child elements like <br />
    then these indeed do not in any way show up with value-of.
    What you probably want is recursive processing of child nodes of
    <content> e.g.
    <xsl:template match="content">
    <div>
    <xsl:apply-templates />
    </div>
    </xsl:template>
    and then a template that copies <br> elements from the input XML to the
    XSLT output
    <xsl:template match="br">
    <xsl:copy />
    </xsl:template>

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
     
    Martin Honnen, Dec 24, 2004
    #2
    1. Advertising

  3. Martin Honnen wrote:
    >
    >
    > ChrisEvans wrote:
    >
    >
    >> I've got an XML file which uses an XSL stylesheet. Problem is:
    >>
    >> <br />
    >>
    >> tags don't work for me when the content is gathered from the xml.
    >> Example:
    >>
    >> <main>
    >> <title>Hi</title>
    >> <content>Hello, how are you?<br /><br />I'm fine.</content>
    >> </main>
    >>
    >> Then in the stylesheet its positioned in the correct place etc and
    >> using <xsl:value-of select="content" /> - trouble is the <br /> doesn't
    >> do anything.

    >
    >
    > <xsl:value-of select="content" /> outputs the string value of the
    > element <content>, if the element has empty child elements like <br />
    > then these indeed do not in any way show up with value-of.
    > What you probably want is recursive processing of child nodes of
    > <content> e.g.
    > <xsl:template match="content">
    > <div>
    > <xsl:apply-templates />
    > </div>
    > </xsl:template>
    > and then a template that copies <br> elements from the input XML to the
    > XSLT output
    > <xsl:template match="br">
    > <xsl:copy />
    > </xsl:template>
    >


    Logically the next thing you are going to run into is transforming
    something like

    <a href="somewhere">somewhere</a>

    from your xml (at least I did). You use the same construct as Martin,
    but instead of just copying the tag and applying templates to its
    content, you need to copy the attributes as well.

    <xsl:template match="a">
    <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>

    Then, you are going to start accumulating a list of all of these types
    of standard html markup that you want to allow in your xml (like style
    etc...) and you are going to wind up with tons of these simple template
    matches and think to yourself, "there must be an easier way to do this,"
    and there is.

    <xsl:template match="*">
    <xsl:copy>
    <xsl:copy-of select="@*" />
    <xsl:apply-templates />
    </xsl:copy>
    </xsl:template>

    Will match and copy ALL UNSPECIFIED TAGS, and their attributes, in your
    xml. By unspecified I mean anything that you haven't explicitely written
    a template for.

    Hope this helps... it just looked like you were on the same path as I
    was, and it took me lots of pain to figure this simple stuff out. Good luck.

    Luke
     
    Luke Dalessandro, Dec 24, 2004
    #3
  4. ChrisEvans

    ChrisEvans Guest

    Hi there,
    Brilliant. looks like what I'm after! :) Thanks.
     
    ChrisEvans, Dec 24, 2004
    #4
  5. ChrisEvans

    ChrisEvans Guest

    Hi there,
    Brilliant. looks like what I'm after! :) Thanks.
     
    ChrisEvans, Dec 24, 2004
    #5
  6. ChrisEvans

    ChrisEvans Guest

    Hi there,
    Brilliant. looks like what I'm after! :) Thanks.
     
    ChrisEvans, Dec 24, 2004
    #6
    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. Johannes Koch
    Replies:
    0
    Views:
    850
    Johannes Koch
    Jul 2, 2003
  2. Marrow
    Replies:
    0
    Views:
    4,143
    Marrow
    Jul 2, 2003
  3. dSchwartz
    Replies:
    1
    Views:
    359
    Bryant Likes
    Mar 6, 2004
  4. Replies:
    1
    Views:
    3,612
    A. Bolmarcich
    May 27, 2005
  5. Kourosh
    Replies:
    6
    Views:
    855
    Peter Flynn
    May 17, 2006
Loading...

Share This Page