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 Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XSLT: Adding some days on the date 1-1-1900 1

Status
Not open for further replies.

Bilberry

Programmer
Dec 17, 2007
111
NL
Hi All,

I have a table which will be filled in automatically based on the following XSLT part:

Code:
	<table border="1" cellpadding="3" cellspacing="0">
	<tr>
		<th>FieldName</th>
		<th>Value</th>
	</tr>
	<xsl:for-each select="="/DATA_ROOT/BASIC/DATA_ITEM">
	<xsl:if test="position() &gt; 0 and position()*2 &lt;= last()+1">

	<tr>
		<td><xsl:value-of select="CAPTION"/> &#160;</td>
		<td><xsl:value-of select="VALUE"/> &#160;</td>
	</tr>
	</xsl:if>
	</xsl:for-each>
	</table>

I want to change this part, because the caption "Due date" contains a number, i want to add this number to the date 01-01-1900. In SQL you can do that with the dateadd function, but how to handle that within XSLT. Now i get:

Fieldname|Value
Due date|39450

and i want to have something like:

Fieldname|Value
Due date|2008-01-05

We need to do something like
select DATEADD("d",39450,'01-01-1900')
as in SQL server...

How to change the code, i think we need to use a choose funtion, below an example, can somebody assist me in the code?

Code:
	<table border="1" cellpadding="3" cellspacing="0">
	<tr>
		<th>FieldName</th>
		<th>Value</th>
	</tr>
	<xsl:for-each select="/DATA_ROOT/BASIC/DATA_ITEM">
	<xsl:if test="position() &gt; 0 and position()*2 &lt;= last()+1">

	<tr>
		<td><xsl:value-of select="CAPTION"/> &#160;</td>
		<xsl:choose>
          	<xsl: when CAPTION = 'Due date'"> then I need to do 1-1-1900 + VALUE</xsl:when> 
          	<xsl:otherwise><td><xsl:value-of select="VALUE"/> &#160;</td></xsl:otherwise>
        	</xsl:choose>
		

	</tr>
	</xsl:if>
	</xsl:for-each>
	</table>

Its the first time that im doing something with XSLT, i hope that the proffesionals here can give me the right way..

Thanks a lot.
Bill
 
I suppose you use ms-related os/tools. In that case, you can use ms xslt extension to implement vbsript function. Like this.

[1] Add some namespace declarations etc... at the root.
[tt]
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:tt-1453929:vbs"
exclude-result-prefixes="msxsl user"
>
<xsl:eek:utput method="html" indent="yes" omit-xml-declaration="yes" />
[/tt]
[2] Add the block containing the user custome function implemented in vbs.
[tt]
<msxsl:script language="vbscript" implements-prefix="user">
<![CDATA[
function renddate(x)
dim n,dt
if isnumeric(x) then
n=ccur(s)
else
n=-1 'arbitrary convention or anything else
end if
if n<>-1 then
dt=dateserial(1900,1,1)+n
renddate=right("00" & month(dt),2) & "-" & right("00" & day(dt),2) & "-" & year(dt)
else
rendate="error: non numeric data found"
end if
end function
]]>
</msxsl:script>
[/tt]
[3] The template rending VALUE looks like this.
[tt]
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<th>FieldName</th>
<th>Value</th>
</tr>
<xsl:for-each select="/DATA_ROOT/BASIC/DATA_ITEM">
<xsl:if test="position() &gt; 0 and position()*2 &lt;= last()+1">

<tr>
<td><xsl:value-of select="CAPTION"/> &#160;</td>
<xsl:choose>
<xsl:when test="normalize-space(CAPTION) = 'Due date'">
<td><xsl:value-of select="user:renddate(normalize-space(VALUE))"/> &#160;</td>
</xsl:when>
<xsl:eek:therwise>
<td><xsl:value-of select="normalize-space(VALUE)"/> &#160;</td>
</xsl:eek:therwise>
</xsl:choose>
</tr>

</xsl:if>
</xsl:for-each>
</table>
[/tt]
 
Thanks tsuji, I want to give you 3 stars -:). I have test this but, it doesnt work i get the function on my ASP Page like:

function renddate(x) dim n,dt if isnumeric(x) then n=ccur(s) else n=-1 'arbitrary convention or anything else end if if n<>-1 then dt=dateserial(1900,1,1)+n renddate=right("00" & month(dt),2) & "-" & right("00" & day(dt),2) & "-" & year(dt) else rendate="error: non numeric data found" end if end function

How to handle?
 
Is the xslt functioning on the server (within the scope of <% ... %>?
 
Also, I had a typo here.
>[self]rendate="error: non numeric data found"
[tt]ren[red]d[/red]date="error: non numeric data found"[/tt]
 
Hi thanks again. I'm adding al those stuff in a XSL document.
I have added the <% and %> around the function but im getting the error message:

msxml3.dll ->clsXML::CreateHtml error '80004005'

The stylesheet does not contain a document element. The stylesheet may be empty, or it may not be a well-formed XML document.

Code:
<%
<msxsl:script language="vbscript" implements-prefix="user">
<![CDATA[
function renddate(x)
    dim n,dt
    if isnumeric(x) then
        n=ccur(s)
    else
        n=-1    'arbitrary convention or anything else
    end if
    if n<>-1 then
        dt=dateserial(1900,1,1)+n
        renddate=right("00" & month(dt),2) & "-" & right("00" & day(dt),2) & "-" & year(dt)
    else
        renddate="error: non numeric data found"
    end if
end function
]]>
</msxsl:script>
%>

Im viewing the information from an ASP page, which uses a XSL file...
 
Amendment-2
Upon re-read, I had another typo here.
>[self]n=ccur(s)
[tt] n=ccur([red]x[/red])[/tt]
 
[4] In response to you latest posting. No! That script block is supposed to be _inside_ the xsl document!!
 
[5] In case you don't quite get the big picture, this should be a viable complete xsl document.
[tt]
<?xml version="1.0" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="[ignore][/ignore]"
xmlns:msxsl="urn:schemas-microsoft-com:xslt"
xmlns:user="urn:tt-1453929:vbs"
exclude-result-prefixes="msxsl user"
>
<xsl:eek:utput method="html" indent="yes" omit-xml-declaration="yes" />

<msxsl:script language="vbscript" implements-prefix="user">
<![CDATA[
function renddate(x)
dim n,dt
if isnumeric(x) then
n=ccur(x)
else
n=-1 'arbitrary convention or anything else
end if
if n<>-1 then
dt=dateserial(1900,1,1)+n
renddate=right("00" & month(dt),2) & "-" & right("00" & day(dt),2) & "-" & year(dt)
else
renddate="error: non numeric data found"
end if
end function
]]>
</msxsl:script>

<xsl:template match="/">
<html>
<body>
<table border="1" cellpadding="3" cellspacing="0">
<tr>
<th>FieldName</th>
<th>Value</th>
</tr>
<xsl:for-each select="/DATA_ROOT/BASIC/DATA_ITEM">
<xsl:if test="position() &gt; 0 and position()*2 &lt;= last()+1">

<tr>
<td><xsl:value-of select="CAPTION"/> &#160;</td>
<xsl:choose>
<xsl:when test="normalize-space(CAPTION) = 'Due date'">
<td><xsl:value-of select="user:renddate(normalize-space(VALUE))"/> &#160;</td>
</xsl:when>
<xsl:eek:therwise>
<td><xsl:value-of select="normalize-space(VALUE)"/> &#160;</td>
</xsl:eek:therwise>
</xsl:choose>
</tr>

</xsl:if>
</xsl:for-each>
</table>

</body>
</html>
</xsl:template>
</xsl:stylesheet>
[/tt]
 
Thanks a lot it works, i want to give you 3 stars, how to do that? :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top