Solved

Why can I not have  <xsl:analyze-string> in <xsl:template>

Posted on 2009-07-03
9
1,029 Views
Last Modified: 2013-11-18
When I try and transform my data with my stylesheet I get the error:

"Keyword xsl:template may not contain xsl:analyze-string"

My template looks like this:

<xsl:template name="CheckValid">        
  <xsl:param name="Textstr"/>
  <xsl:param name="Expression"/>
  <xsl:param name="Replacestr"/>
   <xsl:param name="Type"/>
    <xsl:analyze-string select="$Textstr" regex="$Expression">
          <xsl:matching-substring>
              <xsl:choose>
                 <xsl:when test="$Type = 'date'">
                      <xsl:call-template name="InternationalizeDate">
                         <xsl:with-param name="Datestr" select="$Textstr"/>
                      </xsl:call-template>
                 </xsl:when>
                 <xsl:otherwise>
         <xsl:value-of select="$Textstr"/>    
                 </xsl:otherwise>
              </xsl:choose>
            </xsl:matching-substring>
            <xsl:non-matching-substring>
                <xsl:value-of select="$Replacestr"/>      
            </xsl:non-matching-substring>
         </xsl:analyze-string>
       </xsl:template>

The InternationalizeDate template is as follows:

<xsl:template name="InternationalizeDate">
  <xsl:param name="Datestr"/>
       <xsl:value-of select="concat(substring($Datestr, 7, 4), '/', substring($Datestr, 4, 2), '/',
                                  substring($Datestr, 1, 2))"/>
</xsl:template>    

and finally I call my CheckValid template as follows:

<DateofBirth>
<xsl:if test="normalize-space(Cell[12]/Data) != ''">                  
  <xsl:call-template name="CheckValid">                  
      <xsl:with-param name="Textstr" select="Cell[12]/Data"/>            
     <xsl:with-param name="Expression" select="$dateregexp"/>            
     <xsl:with-param name="Replacestr" select="''"/>
     <xsl:with-param name="Type" select="date"/>                  
  </xsl:call-template>                  
</xsl:if>
</DateofBirth>

Finally the dateregexp param is defined as follows:
<xsl:param name="dateregexp" select="'([0]?[1-9]|[12][0-9]|3[01])[-/ .]([0]?[1-9]|1[012])[-/ .](19|20)\d\d'"></xsl:param>
      
Can anyone see what I am doing wrong?  I have defined my stylesheet as using version 2.0.

Any help much appreciated.
Thanks
Sam


0
Comment
Question by:Samm1502
  • 5
  • 4
9 Comments
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24770938
I will have a look in a minute,
but are you sure that you are using a XSLT2 processor?
you should use Saxon9 (or some other XSLT2 processor- for that
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24770991
My bet is that you are not using an XSLT2 processor,
there is nothing wrong with your code

analyze-string does not like empty strings as an input, som make sure that $Textstr can not be empty
0
 

Author Comment

by:Samm1502
ID: 24771175
How do I find out what XSLT processor I am using?  I didn't install one I am using whatever came with XP as far as I am aware.

Cheers
Sam
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 

Author Comment

by:Samm1502
ID: 24771198
I should add that I calling the Transform method from a VB6 executable (not VB.NET) so not sure I can use Saxon9 as for Java or .NET and my VB calls are like this:

Dim objXSL As MSXML2.DOMDocument
Dim objXML As MSXML2.DOMDocument
Dim strXSLFilePath As String

strXSLFilePath = Get_StyleSheet(strFormat)

Set objXSL = CreateObject("MSXML2.DOMDocument.4.0")
   
'Turn off asyncronous file loading.
objXSL.async = False
 objXSL.Load (strXSLFilePath)
   
Transform_FileData = objXML.transformNode(objXSL)
   
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24771255
OK, you are using msxml. That is a XSLT1 processor, you can not use XSLT2 functionality with msxml
You can use Saxon in .net,
you can not use saxon in classic vb.

So you either port your application to .net or you port your stylesheet to XSLT1

Note that inside msxsl you can use JavaScript.
Given that the regex you have could be a daunting task in XSLT1,
I might recommend that you develop that part in JavaScript inside the XSLT
0
 

Author Comment

by:Samm1502
ID: 24779420
Do you possibly have any good examples showing how to use Javascript within XSLT as porting my application to .NET is out o fthe question within the current available time frame.

Muh apreciated.
Thanks
Sam
0
 
LVL 60

Accepted Solution

by:
Geert Bormans earned 500 total points
ID: 24779863
Here is a simple straightforward example of using JavaScript in an msxml xslt processor

you need to declare
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
and a namespace for your javascript function
      xmlns:myjs="urn:internal:my-javascript">
(could be a different prefix and namespace, as long as you have one it is OK)
Then have a script tag in the msxsl namespace and implement the namespace by setting the prefix
      <msxsl:script language="JavaScript" implements-prefix="myjs">
in this block, define your javascript functions (here you make a function that processes the text by a regex)
works well, the reason I don't do this very often is that it binds your xslt to one specific processor
<?xml version='1.0'?>
<xsl:stylesheet version="1.0" 
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:myjs="urn:internal:my-javascript">
	<msxsl:script language="JavaScript" implements-prefix="myjs">
	<![CDATA[
		function GetCurrentDateTime()
		{
		var currentTime = new Date();
		var month = currentTime.getMonth() + 1;
		var day = currentTime.getDate();
		var year = currentTime.getFullYear();
		return(month + "/" + day + "/" + year);
		}
	]]>
	</msxsl:script>
 
<xsl:template match="/">
	<xsl:value-of select="myjs:GetCurrentDateTime()"/>
</xsl:template>
 
</xsl:stylesheet>

Open in new window

0
 

Author Closing Comment

by:Samm1502
ID: 31599490
Thanks a million that's just what I need.  Much appreciated.
Sam
0
 
LVL 60

Expert Comment

by:Geert Bormans
ID: 24791681
welcome
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
Shoutout to Emily Plummer (http://www.experts-exchange.com/members/eplummer26.html) for giving me this article! She did most of it, I just finished it up and posted it for her :)    Introduction In a previous article (http://www.experts-exchang…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

860 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question