How to Display XML tags and values in an HTML page?

Discussion in 'XML' started by drawbridgej@sympatico.ca, Nov 27, 2005.

  1. Guest

    I've done a little xml and xsl, but am a relative newbie.
    I have been unable to find a stylesheet to process an xml file and
    output the tagNames and tagValues in HTML. I have also posted this
    message in tek-tips.

    I can get the node names with name() and the value with xsl:value-of
    .... but I would like to get the lowest level tagNames via the xsl. I'd
    like to be able to build html for a number of different xml files.


    I'm trying to get something like:

    Flights
    Flight 1
    Flight_Number: BA123
    Origin : GLA
    Destination : LHR
    Carrier : British Airways
    Date : 01/01/2002
    .....
    other flights

    from xml as follows:
    <Flights>
    <Flight>
    <Flight_Number>BA123</Flight_Number>
    <Origin>GLA</Origin>
    <Destination>LHR</Destination>
    <Carrier>British Airways</Carrier>
    <Date>01/01/2002</Date>
    </Flight>
    <Flight>
    <Flight_Number>BA4234</Flight_Number>
    <Origin>GLA</Origin>
    <Destination>YOW</Destination>
    <Carrier>British Airways</Carrier>
    <Date>01/01/2002</Date>
    </Flight>
    </Flights>


    I've tried name() and local-name() but they return the name of the
    parent node, not the leaf element name.
    TIA

    jack
     
    , Nov 27, 2005
    #1
    1. Advertising

  2. Peter Flynn Guest

    wrote:

    > I've done a little xml and xsl, but am a relative newbie.
    > I have been unable to find a stylesheet to process an xml file and
    > output the tagNames and tagValues in HTML. I have also posted this
    > message in tek-tips.


    First of all, please read http://xml.silmaril.ie/authors/makeup/

    > I can get the node names with name() and the value with xsl:value-of
    > ... but I would like to get the lowest level tagNames via the xsl. I'd
    > like to be able to build html for a number of different xml files.


    Have you set <xsl:eek:utput method="html"/> ?

    >
    > I'm trying to get something like:
    >
    > Flights
    > Flight 1
    > Flight_Number: BA123
    > Origin : GLA
    > Destination : LHR
    > Carrier : British Airways
    > Date : 01/01/2002
    > .....
    > other flights
    >
    > from xml as follows:
    > <Flights>
    > <Flight>
    > <Flight_Number>BA123</Flight_Number>
    > <Origin>GLA</Origin>
    > <Destination>LHR</Destination>
    > <Carrier>British Airways</Carrier>
    > <Date>01/01/2002</Date>
    > </Flight>
    > <Flight>
    > <Flight_Number>BA4234</Flight_Number>
    > <Origin>GLA</Origin>
    > <Destination>YOW</Destination>
    > <Carrier>British Airways</Carrier>
    > <Date>01/01/2002</Date>
    > </Flight>
    > </Flights>
    >
    >
    > I've tried name() and local-name() but they return the name of the
    > parent node, not the leaf element name.


    As you didn't post your XSL code we can only guess at what you're doing.

    What you need is a template for each element of your XML document,
    giving the HTML element type that you want to be output for it, eg

    <xsl:template match="Flights">
    <h1>
    <xsl:apply-templates/>
    </h1>
    </xsl:template>

    <xsl:template match="Flight">
    <h2>
    <xsl:text>Flight </xsl:text>
    <xsl:number/>
    </h2>
    <table>
    <xsl:for-each select="*">
    <tr>
    <td>
    <xsl:value-of select="name()"/>
    </td>
    <td>:</td>
    <td>
    <xsl:value-of select="."/>
    </td>
    </tr>
    </xsl:for-each>
    </table>
    </xsl:template>

    The XSL List is a good place to discuss the details of XSL[T]:
    see the details of other FAQs at the address below.

    ///Peter
    --
    XML FAQ: http://xml.silmaril.ie/
     
    Peter Flynn, Nov 27, 2005
    #2
    1. Advertising

  3. Guest

    Thanks Peter,

    I have partially solved the issue. Here is the output and the xsl Used.

    Output:

    List of matching Flights

    Flight
    Flight_Number: BA123
    Origin: GLA
    Destination: LHR
    Carrier: British Airways
    Date: 01/01/2002

    Flight
    Flight_Number: BA4234
    Origin: GLA
    Destination: YOW
    Carrier: British Airways
    Date: 01/01/2002

    Flight
    Flight_Number: AA4959
    Origin: GLA
    Destination: LHR
    Carrier: American Airways
    Date: 01/01/2002

    Flight
    Flight_Number: AC23
    Origin: CAN
    Destination: LHR
    Carrier: Air Canada
    Date: 01/02/2004


    xsl:

    <xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:eek:utput method="html" indent="no"/>
    <xsl:template match="Flights">
    <html>
    <head>
    <title>Matching Request Flights</title>
    </head>
    <body>
    <h1>List of matching Flights </h1>
    <xsl:apply-templates select="Flight"/>
    </body>
    </html>
    </xsl:template>

    <!-- Templates -->
    <xsl:template match="Flight">
    <!-- Display Flight-->
    <tr><th><br/>
    <b><font color="blue"><xsl:value-of select="local-name()"/></font></b>
    </th></tr>
    <table border="0">
    <xsl:apply-templates />
    </table>
    </xsl:template>

    <xsl:template match="Flight/*">
    <tr><td><b><xsl:value-of select="local-name()"/>:</b></td>
    <td><xsl:value-of select="."/></td></tr>
    </xsl:template>

    </xsl:stylesheet>
     
    , Nov 28, 2005
    #3
    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. Dean H. Saxe
    Replies:
    0
    Views:
    1,078
    Dean H. Saxe
    Jan 3, 2004
  2. Rob Nicholson
    Replies:
    3
    Views:
    836
    Rob Nicholson
    May 28, 2005
  3. Donald Firesmith

    html tags within meta tags allowed?

    Donald Firesmith, Jan 5, 2005, in forum: XML
    Replies:
    5
    Views:
    940
    Andy Dingley
    Jan 8, 2005
  4. Dave
    Replies:
    0
    Views:
    465
  5. Marek Kurowski
    Replies:
    1
    Views:
    1,033
    Mark A. Odell
    Aug 18, 2004
Loading...

Share This Page