Jake
Jake

Reputation: 13

Displaying an XML file with XSL to look like HTML

I'm brand new to markup languages and need to display an HTML table, convert it to XML then display it using XSL looking the same as the HTML. Here's a bit of the HTML table:

<table border="1"
cellpadding="5"
summary="Obesity and other statistics">
<tr>
    <th>State</th>
    <th>Obese adults</th>
    <th>Obese children and adolescents</th>
    <th>Median Household Income</th>
    <th>H.S Graduation rate</th>
</tr>
<tr>
    <td>Mississippi</td>
    <td>34.4%</td>
    <td>17.8%</td>
    <td>$36,919</td>
    <td>80.4%</td>
</tr>

4 columns, and several rows with 1 title row. Has a summary (not necessary, can be removed for ease), cell padding and a border. Here's the start of the XML table:

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="mg.xsl"?>

<data>
  <columns>
    <State>State</State>
    <Obese_adults>Obese adults</Obese_adults>
    <Obese_children_and_adolescents>Obese children and adolescents</Obese_children_and_adolescents>
    <Median_Household_Income>Median Household Income</Median_Household_Income>
    <H_S_Graduation_rate>H.S Graduation rate</H_S_Graduation_rate>
  </columns>
  <records>
    <record>
      <State>Mississippi</State>
      <Obese_adults>34.4%</Obese_adults>
      <Obese_children_and_adolescents>17.8%</Obese_children_and_adolescents>
      <Median_Household_Income>$36,919</Median_Household_Income>
      <H_S_Graduation_rate>80.4%</H_S_Graduation_rate>
    </record>

That was converted following a guideline which may or may not be any good for what I'm trying to do. Couldn't find any clear explanation on what it all does or how to use it with the xsl either.

Here's what I have of the XSL

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2></h2>
  <table border="1">
      <th>State</th>
      <th>Obese adults</th>
      <th>Obese children and adolescents</th>
      <th>Median Household Income</th>
      <th>H.S Graduation rate</th>
    </tr>
    <xsl:for-each select="data/records/record">
    <tr>
      <td><xsl:value-of select="State"/></td>
      <td><xsl:value-of select="Obese adults"/></td>
      <td><xsl:value-of select="Obese children and adolescents"/></td>
      <td><xsl:value-of select="Median Household Income"/></td>
      <td><xsl:value-of select="H.S Graduation rate"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

which was again formed following a template I found. I have an idea of what it's doing, but not what it's doing wrong and why it isn't working. I would appreciate some help, especially if I am far off of what is the correct way to go about doing this. Thanks

edit: error I'm getting is "Error loading stylesheet: XPath parse failure: operator expected:" by the way.

Upvotes: 1

Views: 373

Answers (1)

Hash
Hash

Reputation: 8050

You have some small mistakes in ur XSLT here is the edited one of yours;

<?xml version="1.0" encoding="ISO-8859-1"?>

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

<xsl:template match="/">
  <html>
  <body>
  <h2></h2>
  <table border="1">
<tr>
      <th>State</th>
      <th>Obese adults</th>
      <th>Obese children and adolescents</th>
      <th>Median Household Income</th>
      <th>H.S Graduation rate</th>
    </tr>
    <xsl:for-each select="data/records/record">
    <tr>
      <td><xsl:value-of select="State"/></td>
      <td><xsl:value-of select="Obese_adults"/></td>
      <td><xsl:value-of select="Obese_children_and_adolescents"/></td>
      <td><xsl:value-of select="Median_Household_Income"/></td>
      <td><xsl:value-of select="H_S_Graduation_rate"/></td>
    </tr>
    </xsl:for-each>
  </table>
  </body>
  </html>
</xsl:template>

</xsl:stylesheet> 

the output of this applied to your XML will be;

<html>
   <body>
      <h2></h2>
      <table border="1">
         <tr>
            <th>State</th>
            <th>Obese adults</th>
            <th>Obese children and adolescents</th>
            <th>Median Household Income</th>
            <th>H.S Graduation rate</th>
         </tr>
         <tr>
            <td>Mississippi</td>
            <td>34.4%</td>
            <td>17.8%</td>
            <td>$36,919</td>
            <td>80.4%</td>
         </tr>
      </table>
   </body>
</html>

Upvotes: 3

Related Questions