11 Replies Latest reply on Nov 17, 2014 8:13 AM by prabhu deva

    xslt javascript

      Share This:

      Hi Seniors,

      Hope all are fine.

      Im new to BCA AO but I have a programming background.

       

      I need a simple XSLT program with javascript function builtin. so that I can call the javascript function from Value-of-select like the one below.

       

      I get nullpointer issue for the below lines.

       

      it will be greate if some one share a simple lines of code for biginners like me.

       

      <?xml version="1.0" encoding="utf-8"?>

      <xsl:stylesheet version="1.0"

                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"

                      xmlns:java="http://xml.apache.org/xalan/java"

                      xmlns:xalan="http://xml.apache.org/xalan"

                      xmlns:counter="MyCounter"

                      extension-element-prefixes="counter">

       

       

        <xalan:component prefix="counter" functions="response">

          <xalan:script lang="javascript">

      <![CDATA[

        function response(str)

        {

           str2 = 'hello' + str;

           return str2;

        }

      ]]>

          </xalan:script>

        </xalan:component>

       

       

        <xsl:template match="/">

          <xsl:value-of select="counter:response('hai ')"/>

        </xsl:template>

      </xsl:stylesheet>

        • 1. Re: xslt javascript

          Hi,

           

          what your trying to achieve here?

          Is there any specific use case to use only java script?

           

          Brief about your use case or requirement.

           

          Thanks,

          Surya

          • 2. Re: xslt javascript
            Rajeev Gupta

            I took didnt get what the exact question is about. Is there some integration with another program which uses Java script only?

            From the Dev Studio itself you can use XPATH / XSLT for transforming.

            • 3. Re: xslt javascript
              Navin Bandi

              Hi Prabhu,

               

               

              only java functions will work like java:format($dateFormat, $passedDate), also check whether you have declared java namespace "<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="java" version="1.0">"

              1 of 1 people found this helpful
              • 4. Re: xslt javascript
                Gordon McKeown

                Although Xalan itself does support Javascript, BAO as a product isn't certified or tested for this functionality. I believe there is a dependency on the js.jar and bsf.jar files which are not included in our installation, and hence this wouldn't work. I would also expect our XSLT editor to strip out certain types of data that it doesn't know how to interpret.

                1 of 1 people found this helpful
                • 5. Re: xslt javascript

                  Hi All,

                  Sorry for Late reply.

                  certain calculations or logics need to be done from AO level, where I tried using Jython and Perl, which Im still in beginer level.

                  Requirement is simple. I want to convert the string to Unicode format. for example if I give input as A I have to get the output as 97.

                  ie, I have to achieve this in AO itself. in powershell we can perform this conversion easily by using the following command.

                   

                  $bytes = [System.Text.Encoding]::Unicode.GetBytes("abcd")

                  Output:

                  97

                  0

                  98

                  0

                  99

                  0

                  100

                  0

                   

                  Provide some light please

                  • 6. Re: Re: xslt javascript
                    Gordon McKeown

                    I imagine you could do this with a bit of Jython and the script adapter. The basic Python/Jython script is:

                     

                    myString = "This is some text"
                    for c in myString:
                        print ord(c)
                    

                     

                    And you'd just have to decide how you want to pass data in and out. I've written an initial blog post on using Jython scripts in AO which might be of help.

                    • 7. Re: Re: xslt javascript

                      Hi Gordon,

                      Many Thanks for the info, SInce the Script Adapter is not enabled in our Environment ( Approval is in process ), I will not be able to test right away.

                       

                      Mean time, I wanna know whether we can acheve this through XSLT Transformation using Javascript function(By importing Java class Headers)

                      • 8. Re: Re: Re: xslt javascript
                        Gordon McKeown

                        The following will get you the codepoint for the first character of the input context item. You can extend it to loop through all characters.

                         

                        <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:java="java" version="1.0">
                          <xsl:output indent="no" />
                          <xsl:template match="/">
                            <xsl:variable name="charString" select="java:lang.String.new(.)" />
                            <xsl:variable name="charArray" select="java:toCharArray($charString)" />
                            <xsl:variable name="charPos" select="0" />
                            <result>
                              <xsl:value-of select="java:lang.Character.codePointAt($charArray, $charPos)" disable-output-escaping="no" />
                            </result>
                          </xsl:template>
                        </xsl:stylesheet>
                        
                        • 9. Re: xslt javascript

                          Hi Gordon,

                          Thanks for the Reply and it was the correct answer, I am struck in converting the output of Unicode value to Base64.

                           

                          I tried this one but not working. I get the error at the last line. any solution please.

                           

                          var2 = "**"

                          for c in var1: 

                            var2 = var2 + "\n" + "0" + "\n" + str(ord(c))

                          var2 = var2 + "\n" + "0"

                          var3 = var2.replace("**" + "\n" + "0"+"\n","")

                          import base64

                          var4 = base64.b64encode(var3)

                           

                          Regards,

                          Prabhu

                          • 10. Re: Re: xslt javascript
                            Gordon McKeown

                            This won't work because you're trying to import base64 from the standard Python libs. Currently these aren't included, and I have an open idea requesting that they get added.


                            In the meantime you'll need to switch to use the standard Java classes:


                            from org.apache.commons.codec.binary import Base64
                            from java.lang import String
                            
                            var2 = "**"
                            for c in var1:
                              var2 = var2 + "\n" + "0" + "\n" + str(ord(c))
                            var2 = var2 + "\n" + "0"
                            var3 = var2.replace("**" + "\n" + "0"+"\n","")
                            
                            var4 = String (Base64.encodeBase64(var3))
                            
                            

                             

                            Given an input of "This is a string.", this generates the output:

                             

                            ODQKMAoxMDQKMAoxMDUKMAoxMTUKMAozMgowCjEwNQowCjExNQowCjMyCjAKOTcKMAozMgowCjExNQowCjExNgowCjExNAowCjEwNQowCjExMAowCjEwMwowCjQ2CjA=
                            
                            

                             

                            Note that if you modify your Script adapter to include the Jython libs, your script works fine and gives the same output. (See the "Importing Modules" section in this article.)

                            • 11. Re: Re: xslt javascript

                              Hi Gordon,

                              Thanks a Ton for the Reply and Im in the process of including the Jython Libs.

                               

                              But the real scenario of mine is to convert the Unicode string to "ToBase64String" not Base64 Encoding ,

                              was my bad.

                               

                              Im in a process of converting the powershell commands in encoded format. so that I can run the encoded string through normal command prompt or command line adapter..(cause we dont have the powershell adapter or the remote powershell execution is banned)

                               

                              Following steps if u run in powershell , you will get the base64 string result.

                               

                              when encoded result string is executed on command prompt, we will get the expected output.

                               

                              $command = "dir 'c:\program files' "

                              $bytes = [System.Text.Encoding]::Unicode.GetBytes($command)

                              $encodedCommand = [Convert]::ToBase64String($bytes)

                              powershell.exe -encodedCommand $encodedCommand

                               

                              for ex : if you run the following copmmand in normal command prompt we will get the last restart event.

                               

                              powershell.exe -encodedCommand dwBlAHYAdAB1AHQAaQBsACAAcQBlACAAUwB5AHMAdABlAG0AIAAvAHIAZAA6AHQAcgB1AGUAIAAvAGYAOgB0AGUAeAB0ACAALwBjADoAMQAgAC8AcQA6ACcAKgBbAFMAeQBzAHQAZQBtAFsAKABFAHYAZQBuAHQASQBEAD0AMQAwADcANAApACAAbwByACAAKABFAHYAZQBuAHQASQBEAD0ANAAxACkAXQBdACcA

                               

                              Event[0]:

                                Log Name: System

                                Source: USER32

                                Date: 2014-11-02T17:41:18.000

                                Event ID: 1074

                                Task: N/A

                                Level: Information

                                Opcode: N/A

                                Keyword: Classic

                                User: S-1-5-18

                                User Name: NT AUTHORITY\SYSTEM

                                Computer: APACSGTPAV0708.apac.nsroot.net

                                Description:

                              The process wininit.exe (127.0.0.1) has initiated the restart of computer APACSG

                              TPAV0708 on behalf of user NT AUTHORITY\SYSTEM for the following reason: Legacy

                              API shutdown

                              Reason Code: 0x80070000

                              Shutdown Type: restart

                              Comment:

                               

                              conversion of Unicode is exact as of the powershell..but when I convert to Base64, I get difference in the base64 string.

                               

                              So I require to convert the string to Byte (I think So)

                               

                              http://msdn.microsoft.com/en-us/library/dhx0d524(v=vs.110).aspx

                               

                              Regards,

                              Prabhu