Solved

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

Posted on 2009-07-03
9
1,028 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
Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Collapseable list of URLs. 10 128
Problem to file 3 74
Optimum versions of Selenium Webdriver with Python On Windows 7 1 106
The Best Website Creation Tools 14 53
There are two main kinds of selectors in CSS: One is base selector like h1, h2, body, table or any existing HTML tags.  For instance, the following rule sets all paragraphs (<p> elements) to red: (CODE) CSS also allows us to define our own custom …
Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

813 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now