Translate small XML file to HTML using XSL and convert date string in one go

Hi,

I'm struggling with the following problem:

I have an XML-file :

<?xml version="1.0" encoding="utf-8" ?>
<aandeel naam="ABC">
      <lang>NL</lang>
      <datetime>4/7/2005 2:29:33 PM</datetime>
      <waarde>66.00</waarde>
      <wijziging>+0,38</wijziging>
      <icon>up</icon>
</aandeel>

This contains the name of a stock quote and the current value and date of the quote.
The date is as m/d/yyyy h:mm:ss AM

I need to convert this xml file to html using an xsl style sheet and at the same time convert the datetime to the following format:
dd/mm/yyyy hh:mm:ss CET

hh should display 24 hours, so for PM I need to add 12 to the hour from the xml-file.

So far I have the following XSL-sheet :

<?xml version="1.0"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/TR/WD-xsl">

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

      <xsl:template match="aandeel">
            <xsl:value-of select="@naam"/> <b><xsl:value-of select="waarde"/> EUR</b>
            (<xsl:value-of select="wijziging"/>%)<br/>
            <span style="font-size:78%;"><xsl:value-of select="datetime"/></span>
      </xsl:template>
</xsl:stylesheet>

Which returns the following HTML:
ABC <b>66.00 EUR</b> (+0,38%)<br>
<span style="font-size:78%;">4/7/2005 2:29:33 PM</span>

The result I am looking for is :
ABC <b>66.00 EUR</b> (+0,38%)<br>
<span style="font-size:78%;">07/04/2005 14:29:33</span>
LVL 8
Jean Marie GeeraertsApplication EngineerAsked:
Who is Participating?
 
anthonywjones66Commented:
Sorry I missed this:-

<xsl:stylesheet
     version="1.0"
     xmlns:xsl="http://www.w3.org/TR/WD-xsl "
     >

I'm using:-

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

Which is the later recommended version.  The name space you are using is severely limited in functionality.  I'm not sure it can be done in that version.

Which versions of IE are you targeting?

The above namespace wil work as long as the client has msxml 3.0 installed in replace mode.  This is the default for an IE 6 install but IE 5.5 installs only 2.6 by default.  I believe 5.5 will work as long as msxml 3.0 has been installed.

One caveat is that MSXML 3.0 can be installed in side-by-side mode which doesn't replace the version that IE uses.  In fact IE 6 also fails if it's default install is overridden and 3.0 installed in side-by-side mode.

Anthony.
0
 
anthonywjones66Commented:
Here's the template:-

<xsl:template name="formatDate">
          <xsl:param name="dateTimeVal" />
          <xsl:variable name="dateVal" select="substring-before($dateTimeVal, ' ')" />
          <xsl:variable name="timeVal" select="substring-after($dateTimeVal, ' ')" />
          <xsl:variable name="month" select="substring-before($dateVal,'/')" />
          <xsl:variable name="day" select="substring-before(substring-after($dateVal, '/'), '/')" />
          <xsl:variable name="year" select="substring-after(substring-after($dateVal, '/'), '/')" />
          <xsl:variable name="hour" select="substring-before($timeVal, ':')" />
          <xsl:variable name="minute" select="substring-before(substring-after($timeVal, ':'), ' ')" />
          <xsl:variable name="ampm" select="substring-after($timeVal, ' ')" />
          <xsl:if test="string-length($day) = 1"><xsl:text>0</xsl:text></xsl:if>
          <xsl:value-of select="$day" />
          <xsl:text>/</xsl:text>          
          <xsl:if test="string-length($month) = 1"><xsl:text>0</xsl:text></xsl:if>
          <xsl:value-of select="$month" />
          <xsl:text>/</xsl:text>
          <xsl:value-of select="$year" />
          <xsl:text> </xsl:text>
          <xsl:choose>
               <xsl:when test="$ampm='PM'">
                    <xsl:value-of select="number($hour) + 12" />
               </xsl:when>
               <xsl:otherwise>
      <xsl:if test="string-length($hour) = 1"><xsl:text>0</xsl:text></xsl:if>
      <xsl:value-of select="$hour" />
               </xsl:otherwise>
          </xsl:choose>
         <xsl:text>:</xsl:text>
       <xsl:value-of select="$minute" />
     </xsl:template>

Anthony.
0
 
anthonywjones66Commented:
Call it as:-


<span style="font-size:78%;">
  <xsl:call-template name="formatDate">
    <xsl:with-param name="dateTimeVal" select="datetime" />
  </xsl:call-template>
</span>

Anthony.
0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
I must be stupid this week, since I still don't get result and for as far as I can see there's no error in the template you provided.
Is the location of where I paste your template etc. in my xsl style sheet of importance?

I've tried these two combinations: (in seperate comments for readability)
0
 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
<?xml version="1.0"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/TR/WD-xsl "
      >

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

      <xsl:template match="aandeel">
            <xsl:template name="formatDate">
                  <xsl:param name="dateTimeVal" />
                  <xsl:variable name="dateVal" select="substring-before($dateTimeVal, ' ')" />
                  <xsl:variable name="timeVal" select="substring-after($dateTimeVal, ' ')" />
                  <xsl:variable name="month" select="substring-before($dateVal,'/')" />
                  <xsl:variable name="day" select="substring-before(substring-after($dateVal, '/'), '/')" />
                  <xsl:variable name="year" select="substring-after(substring-after($dateVal, '/'), '/')" />
                  <xsl:variable name="hour" select="substring-before($timeVal, ':')" />
                  <xsl:variable name="minute" select="substring-before(substring-after($timeVal, ':'), ' ')" />
                  <xsl:variable name="ampm" select="substring-after($timeVal, ' ')" />
                  <xsl:if test="string-length($day) = 1"><xsl:text>0</xsl:text></xsl:if>
                  <xsl:value-of select="$day" />
                  <xsl:text>/</xsl:text>
                  <xsl:if test="string-length($month) = 1"><xsl:text>0</xsl:text></xsl:if>
                  <xsl:value-of select="$month" />
                  <xsl:text>/</xsl:text>
                  <xsl:value-of select="$year" />
                  <xsl:text> </xsl:text>
                  <xsl:choose>
                        <xsl:when test="$ampm='PM'">
                              <xsl:value-of select="number($hour) + 12" />
                        </xsl:when>
                        <xsl:otherwise>
                              <xsl:if test="string-length($hour) = 1"><xsl:text>0</xsl:text></xsl:if>
                              <xsl:value-of select="$hour" />
                        </xsl:otherwise>
                  </xsl:choose>
                  <xsl:text>:</xsl:text>
                  <xsl:value-of select="$minute" />
            </xsl:template>
            <xsl:value-of select="@naam"/> <b><xsl:value-of select="waarde"/> EUR</b>
            (<xsl:value-of select="wijziging"/>%)<br/>
            <span style="font-size:78%;">
                  <xsl:call-template name="formatDate">
                        <xsl:with-param name="dateTimeVal" select="datetime" />
                  </xsl:call-template>
            </span>
      </xsl:template>

</xsl:stylesheet>
0
 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
<?xml version="1.0"?>
<xsl:stylesheet
      version="1.0"
      xmlns:xsl="http://www.w3.org/TR/WD-xsl "
      >

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

      <xsl:template name="formatDate">
            <xsl:param name="dateTimeVal" />
            <xsl:variable name="dateVal" select="substring-before($dateTimeVal, ' ')" />
            <xsl:variable name="timeVal" select="substring-after($dateTimeVal, ' ')" />
            <xsl:variable name="month" select="substring-before($dateVal,'/')" />
            <xsl:variable name="day" select="substring-before(substring-after($dateVal, '/'), '/')" />
            <xsl:variable name="year" select="substring-after(substring-after($dateVal, '/'), '/')" />
            <xsl:variable name="hour" select="substring-before($timeVal, ':')" />
            <xsl:variable name="minute" select="substring-before(substring-after($timeVal, ':'), ' ')" />
            <xsl:variable name="ampm" select="substring-after($timeVal, ' ')" />
            <xsl:if test="string-length($day) = 1"><xsl:text>0</xsl:text></xsl:if>
            <xsl:value-of select="$day" />
            <xsl:text>/</xsl:text>
            <xsl:if test="string-length($month) = 1"><xsl:text>0</xsl:text></xsl:if>
            <xsl:value-of select="$month" />
            <xsl:text>/</xsl:text>
            <xsl:value-of select="$year" />
            <xsl:text> </xsl:text>
            <xsl:choose>
                  <xsl:when test="$ampm='PM'">
                        <xsl:value-of select="number($hour) + 12" />
                  </xsl:when>
                  <xsl:otherwise>
                        <xsl:if test="string-length($hour) = 1"><xsl:text>0</xsl:text></xsl:if>
                        <xsl:value-of select="$hour" />
                  </xsl:otherwise>
            </xsl:choose>
            <xsl:text>:</xsl:text>
            <xsl:value-of select="$minute" />
      </xsl:template>

      <xsl:template match="aandeel">
            <xsl:value-of select="@naam"/> <b><xsl:value-of select="waarde"/> EUR</b>
            (<xsl:value-of select="wijziging"/>%)<br/>
            <span style="font-size:78%;">
                  <xsl:call-template name="formatDate">
                        <xsl:with-param name="dateTimeVal" select="datetime" />
                  </xsl:call-template>
            </span>
      </xsl:template>

</xsl:stylesheet>
0
 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
The JavaScript that translates the XML to XHTML, using the XSL style sheet is the following :

var xmlSource = "y:\aandeel_NL.xml";
var xslSource = "/kbc/bu000003.nsf/VSYS/stock.xsl/$FILE/stock.xsl";
doTransformStock();

function doTransformStock()
{
      loadXmlIsland(this, "StockQuote", xmlSource, xslSource);
}

function loadXmlIsland(windowName, destination, xmlsource, xslsource)
{

      source = new ActiveXObject("Microsoft.XMLDOM");
      style = new ActiveXObject("Microsoft.XMLDOM");
      source.async = false;
      style.async = false;
     
      source.load(xmlsource);
      style.load(xslsource);
      
      if(source.parseError.errorCode != 0) {
            showError();
      }
      
      if(style.parseError.errorCode != 0) {
            showError();
      }

      doTransform(windowName,destination);
                 
      if(style.parseError.errorCode != 0) {
            showError();
      }
}

function doTransform( windowName, destination)
{      
      var resulting ="";
      if (getReadyState()) {      
            try{
                  resulting = source.transformNode(style);
            }  
            catch (errorke) {
                  showError();
            }
            windowName.document.all.item(destination).innerHTML = resulting;
      } else {
            alert("Could not get ready state!");
      }
}

function getReadyState()
{
      if (source.readyState == 4) {
            return true;
      }      
      setTimeout("getReadyState()", 1);
}

function showError()
{
      var strError = new String;
      var err = source.parseError;
      strError = 'Error!\n' +
      'file url: '+err.url +' \n'+
      'line no.:'+err.line +'\n'+
      'char: '+ err.linepos + '\n' +
      'source: '+err.srcText+'\n'+
      'code: '+err.errorCode+'\n'+
      'description: '+err.reason+'\n';
      //alert (strError);
}
0
 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
We are using IE6.0 under Windows NT4.0. I'll change the namespace reference and then try again.
0
 
Jean Marie GeeraertsApplication EngineerAuthor Commented:
All right, now it works perfectly.
Big thanks !!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.