Jump to content

Transformation of Numerical Data


Guest FirefoxRocks
 Share

Recommended Posts

Guest FirefoxRocks

Ok now that I have my XSLT file set up, I want to be able to do stuff with it.I want numerical months (01-12) to be textual strings (January - December). And I don't want zeros in front of my days (01-31) to (1-31). How do I do that? I tried reading through some DOM stuff, but didn't really understand it.And I want the user to be able to view specific months of data. How do I use JavaScript to change the <xsl:if> thing?Any help would be appreciated. Thank you.

Link to comment
Share on other sites

It's kind of hard without seeing your current XSLT and a sample XML against it, but anyway...The month-number-to-month-name should be easy. Just add something like

<xsl:choose>  <xsl:when test="$month = 1">January</xsl:when>  <xsl:when test="$month = 2">February</xsl:when>  <!-- The other months --></xsl:choose>

but replace $month (or add a variable) with whatever would match the month number.For the removal of leading zeroes, it should be even easier. Instead of saying value-of $day, say value-of number($day), i.e.

<xsl:value-of select="number($day)"/>

again replacing $day (or adding a variable) with whatever would match the day number.

Link to comment
Share on other sites

Guest FirefoxRocks

I think that is correct, but here is the XML/XSL file to verify:The XML file:

<?phpecho "<?xml version='1.0' encoding='iso-8859-1'?>\n<?xml-stylesheet type='text/xsl' href='THE_XSL_FILE.xsl'?>\n<ROOT>";$con = mysql_connect("DB_DOMAIN","DB_USER","DB_PASSWORD");if(!$con){die('Database error:' . mysql_error());}mysql_select_db("DB_NAME", $con);$result = mysql_query("SELECT * FROM tableName");$y=substr($row["date"],0,4);$m=substr($row["date"],5,2);$d=substr($row["date"],-2,2);while($row = mysql_fetch_array($result))  {  echo "<ITEM><name>" . $row['itemName'] . "</name><amount>" . $row['amount'] . "</amount><date><month>" . $m . "</month><day>" . $d . "</day><year>" . $y . "</year></date></ITEM>";  }echo "</ROOT>";mysql_close();?>

*Some names have been changed for privacy reasons.The XSL file:

<?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 xmlns="http://www.w3.org/1999/xhtml" xml:lang="en-CA" lang="en-CA">		<head>		<title>THE DOCUMENT TITLE</title>		<style type="text/css">	/*<![CDATA[*/	h1{color:#00f;background-color:gold;text-align:center}	/* SOME MORE STYLE INFORMATION HERE*/	/*]]>*/		</style>		</head>	<body>	<h1>THE DOCUMENT HEADING</h1>	<h2><a href="SOMEPAGE.html">Click here to read about SOME STUFF!</a></h2>	<table summary="SOME DATA">	<caption>List of SOME DATA</caption>	<colgroup>		<col class="ITEM"></col>		<col class="amount"></col>		<col class="date"></col>	</colgroup>	<thead><tr><th>ITEM</th><th>Amount</th><th>Date</th></tr></thead>	<tbody>	<xsl:for-each select="ROOT/ITEM">	  <tr>		<td><xsl:value-of select="name"/></td>		<td><xsl:value-of select="amount"/></td>		<td>			<xsl:choose>			<xsl:when test="date/month=1">January</xsl:when>			<xsl:when test="date/month=2">February</xsl:when>			<xsl:when test="date/month=3">March</xsl:when>			<xsl:when test="date/month=4">April</xsl:when>			<xsl:when test="date/month=5">May</xsl:when>			<xsl:when test="date/month=6">June</xsl:when>			<xsl:when test="date/month=7">July</xsl:when>			<xsl:when test="date/month=8">August</xsl:when>			<xsl:when test="date/month=9">September</xsl:when>			<xsl:when test="date/month=10">October</xsl:when>			<xsl:when test="date/month=11">November</xsl:when>			<xsl:when test="date/month=12">December</xsl:when>			</xsl:choose>			<xsl:value-of select="number(date/day)"/>, <xsl:value-of select="date/year"/></td>	  </tr>	</xsl:for-each>	</tbody></table></body></html></xsl:template></xsl:stylesheet>

Now as for my second part, allowing users to select certain months to display. Do I just use a text node to change the <xsl:if> thing?

Link to comment
Share on other sites

The easiest approach is to do it on the server. Make the PHP with a GET variable that depending on the value will generate an XML with only the targeted month(s).XSLT will always transform everything it has.Now, I don't know MySQL enough to really tell you how you'd do this, but if what you have now is a display of all months, then I assume it will be something like:

while($row = mysql_fetch_array($result))  {if ($m == $_GET['month']) {  echo "<ITEM><name>" . $row['itemName'] . "</name><amount>" . $row['amount'] . "</amount><date><month>" . $m . "</month><day>" . $d . "</day><year>" . $y . "</year></date></ITEM>";}  }

Where the month variable will always have to be present and given a numeric value with a leading zero i.e.

index.php?month=01

for January and so on.

Link to comment
Share on other sites

Guest FirefoxRocks

Sorry, but I forgot to say that I want a specific month+year. Like all of the August 2007 results, or January 2006 results. But I think I can modify the above code for that to work.

Link to comment
Share on other sites

Yeah, the only fix you need is to make

if ($m == $_GET['month']) {

into something like

if ($m == $_GET['month'] && $y == $_GET['year']) {

in which case of course the URL would become

index.php?month=01&year=2007

but I guess you can figure it out if you need something more complicated (like allow both this and all years for a certain month).

Link to comment
Share on other sites

I'd highly advise that you don't use JavaScript to manipulate an xsl:if. Instead, add a parameter and in the condition, test against the parameter. You could then execute the transformation again but with the new parameter in mind.This old post (and the ones below it) show a JavaScript method of rerunning an XSLT transformation with a new parameter. Only one new parameter at a time could be passed with this, but it doesn't seem like you need many either. More complicated approaches require deeper understanding of JavaScript which I don't yet have.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...