Solved

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

Posted on 2009-07-03
9
1,040 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

SASS allows you to treat your CSS code in a more OOP way. Let's have a look on how you can structure your code in order for it to be easily maintained and reused.
JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

688 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