XML/XSL HTML problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • ChrisEvans

    #1

    XML/XSL HTML problem

    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>Hell o, 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

  • Martin Honnen

    #2
    Re: XML/XSL HTML problem



    ChrisEvans wrote:

    [color=blue]
    > 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>Hell o, 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.[/color]

    <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/

    Comment

    • Luke Dalessandro

      #3
      Re: XML/XSL HTML problem

      Martin Honnen wrote:[color=blue]
      >
      >
      > ChrisEvans wrote:
      >
      >[color=green]
      >> 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>Hell o, 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.[/color]
      >
      >
      > <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>
      >[/color]

      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

      Comment

      • ChrisEvans

        #4
        Re: XML/XSL HTML problem

        Hi there,
        Brilliant. looks like what I'm after! :-) Thanks.

        Comment

        • ChrisEvans

          #5
          Re: XML/XSL HTML problem

          Hi there,
          Brilliant. looks like what I'm after! :-) Thanks.

          Comment

          • ChrisEvans

            #6
            Re: XML/XSL HTML problem

            Hi there,
            Brilliant. looks like what I'm after! :-) Thanks.

            Comment

            Working...