Tricky XSL question (?)

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

    #1

    Tricky XSL question (?)

    I have an XML file:

    <document>

    <record>
    <element1>Tex t</element1>
    <element2>Mor e text</element2>
    <element3>Eve n more text</element3>
    <element4>Som e text <link><title>Go ogle</title><url>
    http://www.google.com</url></link> with a link.</element4>
    </record>

    <record>
    <element1>Yea h</element1>
    <element2>Now the link is here: <link><title>W3 C</title><url>
    http://www.w3c.org</url></link></element2>
    <element3>Somet hing else</element3>
    <element4>...an d more text</element4>
    </record>

    </document>

    which I want to convert to an HTML table. One possibility for the
    conversion is to use for-each loop in XSL as in

    (XSL1)

    <xsl:template match="/">
    <html>
    <head>
    <title>My HTML table</title>
    </head>
    <body>
    <table border="1">
    <head>
    <tr>
    <td><b>Element1 </b></td>
    <td><b>Element2 </b></td>
    <td><b>Element3 </b></td>
    <td><b>Element4 </b></td>
    </tr>
    </head>
    <xsl:for-each select="documen t/record">
    <tr>
    <td><xsl:valu e-of select="element 1" /></td>
    <td><xsl:valu e-of select="element 2" /></td>
    <td><xsl:valu e-of select="element 3" /></td>
    <td><xsl:valu e-of select="element 4" /></td>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    This will produce the table nicely, but what about the

    <link>
    <title>Title of the URL link</title>
    <url>http://some.url.link</url>
    </link>

    structure I have in my XML file? I would like to be able to put such a
    link structure inside any element I have in the XML record. The link
    structure itself is easily enough translated into a URL with an XSL
    transformation of

    (XSL2)

    <xsl:template match="//link">
    <xsl:element name="a">
    <xsl:attribut e name="href">
    <xsl:value-of select="url" />
    </xsl:attribute>
    <xsl:value-of select="title" />
    </xsl:element>
    </xsl:template>

    But when I want to have the possibility of having URL links anywhere in
    my HTML table, straight combining of (XSL1) and (XSL2) templates
    fails...seems to have something to do with using the value-of structure
    in (XSL1)...?

    Regards,
    Jyrki Keisala
  • Janwillem Borleffs

    #2
    Re: Tricky XSL question (?)

    Jyrki Keisala wrote:[color=blue]
    > This will produce the table nicely, but what about the
    >
    > <link>
    > <title>Title of the URL link</title>
    > <url>http://some.url.link</url>
    > </link>
    >
    > structure I have in my XML file? I would like to be able to put such
    > a link structure inside any element I have in the XML record.[/color]

    This ought to do it:

    <xsl:template match="/">
    <html>
    <head>
    <title>My HTML table</title>
    </head>
    <body>
    <table border="1">
    <head>
    <tr>
    <td><b>Element1 </b></td>
    <td><b>Element2 </b></td>
    <td><b>Element3 </b></td>
    <td><b>Element4 </b></td>
    </tr>
    </head>
    <xsl:for-each select="documen t/record">
    <tr>
    <xsl:for-each select="child:: *">
    <td><xsl:appl y-templates select="link|te xt()" /></td>
    </xsl:for-each>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

    <xsl:template match="link">
    <a href="{url}"><x sl:value-of select="title" /></a>
    </xsl:template>


    HTH,
    JW



    Comment

    • Jyrki Keisala

      #3
      Re: Tricky XSL question (?)

      "Janwillem Borleffs" <jw@jwscripts.c om> wrote in news:3ff5a8d5$0 $16882
      $1b62eedf@news. wanadoo.nl:
      [color=blue]
      > Jyrki Keisala wrote:[color=green]
      >> This will produce the table nicely, but what about the
      >>
      >> <link>
      >> <title>Title of the URL link</title>
      >> <url>http://some.url.link</url>
      >> </link>
      >>
      >> structure I have in my XML file? I would like to be able to put such
      >> a link structure inside any element I have in the XML record.[/color]
      >
      > This ought to do it:
      >
      > <xsl:template match="/">
      > <html>
      > <head>
      > <title>My HTML table</title>
      > </head>
      > <body>
      > <table border="1">
      > <head>
      > <tr>
      > <td><b>Element1 </b></td>
      > <td><b>Element2 </b></td>
      > <td><b>Element3 </b></td>
      > <td><b>Element4 </b></td>
      > </tr>
      > </head>
      > <xsl:for-each select="documen t/record">
      > <tr>
      > <xsl:for-each select="child:: *">
      > <td><xsl:appl y-templates select="link|te xt()" /></td>
      > </xsl:for-each>
      > </tr>
      > </xsl:for-each>
      > </table>
      > </body>
      > </html>
      > </xsl:template>
      >
      > <xsl:template match="link">
      > <a href="{url}"><x sl:value-of select="title" /></a>
      > </xsl:template>[/color]


      Thanks for the valuable tip. Everything seems to work just fine; I have
      just one further question: for what is the "select="link|t ext()" needed?
      Everything seems to work just fine for me even without that, so a plain

      <xsl:for-each select="child:: *">
      <td><xsl:appl y-templates /></td>
      </xsl:for-each>

      seems to be able to handle the links exactly correct...

      Regards,
      Jyrki

      Comment

      • Janwillem Borleffs

        #4
        Re: Tricky XSL question (?)

        Jyrki Keisala wrote:[color=blue]
        > Thanks for the valuable tip. Everything seems to work just fine; I
        > have just one further question: for what is the "select="link|t ext()"
        > needed? Everything seems to work just fine for me even without that,
        > so a plain
        >
        > <xsl:for-each select="child:: *">
        > <td><xsl:appl y-templates /></td>
        > </xsl:for-each>
        >
        > seems to be able to handle the links exactly correct...
        >[/color]

        It selects the <link /> element or a text node when available. I have put it
        in mainly to show you how to work with apply-templates select attribute.

        This can be very useful when you want to match specific nodes only...


        JW



        Comment

        • Jyrki Keisala

          #5
          Re: Tricky XSL question (?)

          "Janwillem Borleffs" <jw@jwscripts.c om> wrote in
          news:3ff6c163$0 $23710$1b62eedf @news.wanadoo.n l:
          [color=blue]
          > Jyrki Keisala wrote:[color=green]
          >> Thanks for the valuable tip. Everything seems to work just fine; I
          >> have just one further question: for what is the "select="link|t ext()"
          >> needed? Everything seems to work just fine for me even without that,
          >> so a plain
          >>
          >> <xsl:for-each select="child:: *">
          >> <td><xsl:appl y-templates /></td>
          >> </xsl:for-each>
          >>
          >> seems to be able to handle the links exactly correct...
          >>[/color]
          >
          > It selects the <link /> element or a text node when available. I have
          > put it in mainly to show you how to work with apply-templates select
          > attribute.
          >
          > This can be very useful when you want to match specific nodes only...
          >
          >
          > JW
          >
          >
          >
          >[/color]

          OK, thanks veeery much; it works for me now :)

          Jyrki

          Comment

          Working...