Jump to content

Please Help! I'm New To Xsl


ChopShop

Recommended Posts

Hello All, I am new to XSL and need to convert some XML files into HTML tables for import into Word. Here is a sample of the XML: <?xml version="1.0" encoding="UTF-8" standalone="no"?><resources> <string-array name="exercise_intensities"> <item>None</item> <item>Low</item> <item>Medium</item> <item>High</item></string-array> <string-array name="reminders_list_entries"> <item>All Reminders</item> <item>After Meal Only</item> <item>None</item></string-array> </resources> I need the tables to have the header of each <string-array name> and then each <item> listed below. Anyone know how to do this? I can't get it to work. :( Any help would be greatly appreciated, as I have a tight deadline.

Link to comment
Share on other sites

<head><meta http-equiv="content-type" content="text/html;charset=utf-8"/><title>xml_to_table.html</title><style type="text/css" media="screen"><!--tbody, thead, tfoot, tr, td, th {border-style : inherit;border-width : inherit;border-color : inherit;}td {width : 10px;height : 5px;}.leftFloat {float : left;}.rightFloat {float : right;}tr.Row-Column-1 {background-color : #cccccc;}p.Basic-Paragraph {font-family : "Minion Pro", serif;font-weight : normal;font-style : normal;font-size : 1em;text-decoration : none;font-variant : normal;line-height : 1.2;text-align : left;color : #000000;text-indent : 0px;margin : 0px;}table.Basic-Table {border-collapse : collapse;border-width : 1px;border-style : solid;border-color : #000000;margin-top : 4px;margin-bottom : -4px;}table.table-style-override-1 {border-collapse : collapse;}td.cell-style-override-1 {background-color : #a3ff40;}div.Basic-Text-Frame {}--></style></head><body><div id="xml_to_table.html" xml:lang="en-US"><div class="Basic-Text-Frame"><table id="table-1" class="Basic-Table table-style-override-1"><thead><tr><td class="cell-style-override-1">exercise_intensities</td></tr></thead><tbody><tr class="Row-Column-1"><td>None</td></tr><tr><td>Low</td></tr><tr class="Row-Column-1"><td>Medium</td></tr><tr><td>High</td></tr></tbody></table><p class="Basic-Paragraph"/><table id="table-2" class="Basic-Table table-style-override-1"><thead><tr><td class="cell-style-override-1">reminders_list_entries</td></tr></thead><tbody><tr class="Row-Column-1"><td>All Reminders</td></tr><tr><td>After Meal Only</td></tr><tr class="Row-Column-1"><td>None</td></tr></tbody></table></div></div></body></html> Hope this helps. Thank you for replying.

Link to comment
Share on other sites

Well I asked for HTML markup, I still don't see any in your posts.Here is a sample XSLT 1.0 stylesheet:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">   <xsl:output indent="yes" method="html"/>   <xsl:template match="string-array">    <table>	  <thead>	    <tr>		  <th>		    <xsl:value-of select="@name"/>		  </th>	    </tr>	  </thead>	  <tbody>	    <xsl:apply-templates select="item"/>	  </tbody>    </table>  </xsl:template>   <xsl:template match="item">    <tr>	  <td>	    <xsl:value-of select="."/>	  </td>    </tr>  </xsl:template> </xsl:stylesheet>

It transforms your posted XML input into the following HTML fragment:

<table>   <thead>	  <tr>		 <th>exercise_intensities</th>	  </tr>   </thead>   <tbody>	  <tr>		 <td>None</td>	  </tr>	  <tr>		 <td>Low</td>	  </tr>	  <tr>		 <td>Medium</td>	  </tr>	  <tr>		 <td>High</td>	  </tr>   </tbody></table><table>   <thead>	  <tr>		 <th>reminders_list_entries</th>	  </tr>   </thead>   <tbody>	  <tr>		 <td>All Reminders</td>	  </tr>	  <tr>		 <td>After Meal Only</td>	  </tr>	  <tr>		 <td>None</td>	  </tr>   </tbody></table>

Link to comment
Share on other sites

Thank you! The other files I have are in this format: <?xml version="1.0" encoding="UTF-8" standalone="no"?><resources><string name="error_three_day_meal_time_sequential">Meal Times must be sequential.</string><string name="error_three_day_overlap_existing_test">Only one structured testing regimen may be performed at a time.</string><string name="error_three_day_start_time_in_past">Start time cannot be earlier than current time.</string><string name="error_three_day_start_time_in_future">Start time cannot be more than seven months from now.</string></resources> Is there a way to make a two column table with the <string name> in one column and the <string> text in the other? Is there a way to convert this to .html file? Thank you very much for your help! I truly appreciate it! :)

Link to comment
Share on other sites

Here is a sample stylesheet that takes your latest XML input and outputs a two column HTML table:

<xsl:stylesheet  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"  version="1.0">   <xsl:output indent="yes" method="html"/>   <xsl:template match="resources">    <table>	  <tbody>	    <xsl:apply-templates select="string"/>	  </tbody>    </table>  </xsl:template>   <xsl:template match="string">    <tr>	  <xsl:apply-templates select="@name | text()"/>    </tr>  </xsl:template>   <xsl:template match="string/@name | string/text()">    <td>	  <xsl:value-of select="."/>    </td>  </xsl:template> </xsl:stylesheet>

The output looks like this:

<table>   <tbody>	  <tr>		 <td>error_three_day_meal_time_sequential</td>		 <td>Meal Times must be sequential.</td>	  </tr>	  <tr>		 <td>error_three_day_overlap_existing_test</td>		 <td>Only one structured testing regimen may be performed at a time.</td>	  </tr>	  <tr>		 <td>error_three_day_start_time_in_past</td>		 <td>Start time cannot be earlier than current time.</td>	  </tr>	  <tr>		 <td>error_three_day_start_time_in_future</td>		 <td>Start time cannot be more than seven months from now.</td>	  </tr>   </tbody></table>

If you want the stylesheet to output a complete HTML document then add a template

<xsl:template match="/">  <html>    <head>	   <title>title goes here</title>   </head>   <body>	  <xsl:apply-templates/>   </body></xsl:template>

Link to comment
Share on other sites

This one is not working for me. (just produces a blank page when opened in web browser)
I tested the samples I posted, not inside the browser, but the output samples I posted are the result Saxon 6.5.5 gives. If you don't get any result then there must be some difference in the input you showed us and the one you run the stylesheet with. So you will need to show us details of the samples that don't work as intended.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...