tags:

views:

78

answers:

2

Hi, I've an XML document and I am creating another XML using XSL. I need to check some specific conditions and for that I want to use Javascript in my XSL. I tried it, however, couldn't get the desired result. As I could not change the XSL variables frequiently so i am trying to use Javascript.

XSL-

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:es="http://ucmservice"
    version="1.0" xmlns="http://filenet.com/namespaces/wcm/apps/1.0" xmlns:java="http://xml.apache.org/xalan/java" xmlns:js="urn:custom-javascript" xmlns:lxslt="http://xml.apache.org/xslt"
        xmlns:totalSys="TotalSystem"  extension-element-prefixes="totalSys">

  <lxslt:component prefix="totalSys" functions="checkFirstProp">
  <lxslt:script lang="javascript">

            var firstPropVal = "";
            var secondPropVal = "";
            var completedFirstPropVal= new Array();
            var completedSecondPropVal= new Array();
            var firstDecisionFlag = 0; 
            function checkFirstProp(xmlValue)
            {
                firstDecisionFlag = 0;

                if(firstPropVal.length == 0)
                {
                        firstPropVal = xmlValue;
                        firstDecisionFlag = 1; 
                 }
                 else
                 {
                    if(firstPropVal != xmlValue)
                    {
                         firstPropVal = xmlValue; 
                         firstDecisionFlag = 2; 
                    }
                  }

                  return firstDecisionFlag; 
            }

       </lxslt:script>
    </lxslt:component> 
    <xsl:template match="/">
     <xsl:apply-templates select="XMLTag"/>
    </xsl:template>
    <xsl:template match="XMLTag">        
        <xsl:variable name="firstPropDecisionFlag">
            <xsl:value-of select="totalSys:checkFirstProp(param)"/> 
        </xsl:variable>

        <xsl:if test="$firstPropDecisionFlag=2">
            {
                task
            } 
        </xsl:if>
       </xsl:template>
          bvk</xsl:stylesheet> 

this gave me an error message-

[7/20/10 16:41:47:106 IST] 0000002e SystemErr R org.apache.xalan.extensions.ObjectFactory$ConfigurationError: Provider org.apache.bsf.BSFManager not found

Please advise, where am i going wrong?

A: 

Try wrapping the javascript code in a CDATA section.

<script>
<![CDATA[
(function(){
  //...
})();
]]>
</script>
no
I tried that as well.. however that only work when i have to change the XML to html. in current situation i am trying to convert my xml into another required format xml.
Gotcha... so those lxslt tags actually allow you to execute the javascript on the XML document from the stylesheet. Pretty cool. It looks like you don't really need anything but plain xsl to do what you're doing with the javascript, but I don't know anything about those lxslt tags, so I could be wrong. Check skaffman's answer...
no
i tried to do the same with plain xsl, however, couldn't do it. as in my case i need to change the variable value again and again, while in xsl i have to declare and define the variable at the same place.
A: 

The error says that you need the Apache Bean Scripting Framework in your classpath. You can get it here.

skaffman
Thanks Skaffman.. it needed 3 jar files, i added them and after that it worked.