Tricky XSL question (?)

Discussion in 'XML' started by Jyrki Keisala, Jan 2, 2004.

  1. I have an XML file:

    <document>

    <record>
    <element1>Text</element1>
    <element2>More text</element2>
    <element3>Even more text</element3>
    <element4>Some text <link><title>Google</title><url>
    http://www.google.com</url></link> with a link.</element4>
    </record>

    <record>
    <element1>Yeah</element1>
    <element2>Now the link is here: <link><title>W3C</title><url>
    http://www.w3c.org</url></link></element2>
    <element3>Something else</element3>
    <element4>...and 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="document/record">
    <tr>
    <td><xsl:value-of select="element1" /></td>
    <td><xsl:value-of select="element2" /></td>
    <td><xsl:value-of select="element3" /></td>
    <td><xsl:value-of select="element4" /></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:attribute 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
     
    Jyrki Keisala, Jan 2, 2004
    #1
    1. Advertising

  2. Jyrki Keisala wrote:
    > 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.


    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="document/record">
    <tr>
    <xsl:for-each select="child::*">
    <td><xsl:apply-templates select="link|text()" /></td>
    </xsl:for-each>
    </tr>
    </xsl:for-each>
    </table>
    </body>
    </html>
    </xsl:template>

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


    HTH,
    JW
     
    Janwillem Borleffs, Jan 2, 2004
    #2
    1. Advertising

  3. "Janwillem Borleffs" <> wrote in news:3ff5a8d5$0$16882
    $:

    > Jyrki Keisala wrote:
    >> 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.

    >
    > 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="document/record">
    > <tr>
    > <xsl:for-each select="child::*">
    > <td><xsl:apply-templates select="link|text()" /></td>
    > </xsl:for-each>
    > </tr>
    > </xsl:for-each>
    > </table>
    > </body>
    > </html>
    > </xsl:template>
    >
    > <xsl:template match="link">
    > <a href="{url}"><xsl:value-of select="title" /></a>
    > </xsl:template>



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

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

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

    Regards,
    Jyrki
     
    Jyrki Keisala, Jan 3, 2004
    #3
  4. Jyrki Keisala wrote:
    > Thanks for the valuable tip. Everything seems to work just fine; I
    > have just one further question: for what is the "select="link|text()"
    > needed? Everything seems to work just fine for me even without that,
    > so a plain
    >
    > <xsl:for-each select="child::*">
    > <td><xsl:apply-templates /></td>
    > </xsl:for-each>
    >
    > seems to be able to handle the links exactly correct...
    >


    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
     
    Janwillem Borleffs, Jan 3, 2004
    #4
  5. "Janwillem Borleffs" <> wrote in
    news:3ff6c163$0$23710$:

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

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


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

    Jyrki
     
    Jyrki Keisala, Jan 5, 2004
    #5
    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. rene
    Replies:
    3
    Views:
    1,279
  2. Replies:
    1
    Views:
    3,613
    A. Bolmarcich
    May 27, 2005
  3. jl
    Replies:
    3
    Views:
    420
    Andy Dingley
    Aug 23, 2006
  4. Replies:
    9
    Views:
    539
    CBFalconer
    Apr 25, 2006
  5. Replies:
    2
    Views:
    376
Loading...

Share This Page