Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML->HTML with ordering

Status
Not open for further replies.

klwong

Programmer
Dec 7, 2001
14
0
0
HK
Given a xml as an example:
<nitf>
...
<body-content>
<p>Paragraph1</p>
<p>Paragraph2</p>
<hl2>Subheadline1</hl2>
<p>Paragraph3</p>
<p>Paragraph4</p>
<table>
<tr><td>data1</td><td>data2</td></tr>
<tr><td>data3</td><td>data4</td></tr>
</table>
<media>
<media-reference source="abc.jpg"/>
</media>
<hl2>Subheadline2</hl2>
<p>Paragraph5</p>
</body-content>
</nitf>

I need to write a stylesheet such that it can generically form a HTML such that the content will be in order such like that in the xml?

ie. ... something like this.
<html>
<body>
<p>Paragraph1</p>
<p>Paragraph2</p>
<b>Subheadline1</b>
<p>Paragraph3</p>
<p>Paragraph4</p>
<table>
<tr><td>data1</td><td>data2</td></tr>
<tr><td>data3</td><td>data4</td></tr>
</table>
<img src="abc.jpg">
<b>Subheadline2</b>
<p>Paragraph5</p>
</body>
</html>

 
You need to use XSLT:


This is example of stylesheet that would do what you want:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="nitf">
    <htm>
      <head>
        <title>My page</title>
      </head>
      <body>
        <xsl:apply-templates select="@*|node()"/>
      </body>
    </htm>
  </xsl:template>
  <xsl:template match="hl2">
    <h2>
      <xsl:value-of select="."/>
    </h2>
  </xsl:template>
    <xsl:template match="media">
    <img src="{media-reference/@source}" alt="{media-reference/@source}"/>
  </xsl:template>
</xsl:stylesheet>
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top