How to import javascript in xsl

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • maxin
    New Member
    • Jan 2009
    • 13

    How to import javascript in xsl

    Hi,

    I write a javascript function in xsl (UOMConversion. xsl) as follow:
    Code:
    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0"
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <script language="Javascript">
    <![CDATA[
    var ISOUOM= new Array("KGM","MTR","LTR","CT");
    var MESUOM= new Array("KG","M","L","KAR");
    
    function UOMISOTOMES(UOMIn) {
        var i;
        var UOMOut;
        for (i=0; i<=ISOUOM.length; i++)
        {
            if (UOMIn == ISOUOM[i]) {
                UOMOut = MESUOM[i];
            }
        }
        return UOMOut;
    }
    ]]>
    </script>    
    </xsl:stylesheet>
    I would like to import above UOMConversion.x sl into a xsl (LibUserFunctio ns.xsl). I write:

    Code:
    <xsl:stylesheet version="1.0" 
        xmlns:xs="http://www.w3.org/2001/XMLSchema" 
        xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        
    <xsl:import href="UOMConversion.xsl"/> 
    ....
    </xsl:stylesheet>
    I got an error when I use LibUserFunction s.xsl to transform an xml - "Keyword xsl:stylesheet may not contain script. Error occurred during compilation of included or imported stylesheet 'file..."

    Where is the problem?

    Regards
    maxin
    Last edited by Dormilich; Jan 18 '09, 01:05 PM. Reason: added [code] tags
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    Originally posted by maxin
    Where is the problem?
    as stated in the error message, <script> is not an allowed child element of <xsl:stylesheet > see XSL Transformations (XSLT) for the allowed elements.

    Comment

    • maxin
      New Member
      • Jan 2009
      • 13

      #3
      I also used date function EXSLT. The date.msxsl.xsl has also some javascript as
      Code:
      <?xml version="1.0"?>
      <xsl:stylesheet version="1.0"
                      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                      xmlns:date="http://exslt.org/dates-and-times"
                      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
                      extension-element-prefixes="date msxsl">
      
      <msxsl:script language="Javascript" implements-prefix="date">
      <![CDATA[
      ...
      ]]>
      </msxsl:script>
      </xsl:stylesheet>
      The different is I dont have the namespace. I don't have any problem to import date.msxsl.xsl.

      Regards
      maxin
      Last edited by Dormilich; Jan 18 '09, 02:12 PM. Reason: added [code] tags, please read your PMs

      Comment

      • maxin
        New Member
        • Jan 2009
        • 13

        #4
        xsl with javascript is imported successfully, however, ...

        Hi,
        Thanks for you answer.
        It seems I need the namespace. I can import the xsl file successfully. However, I still get an error in the above javascript - "Microsoft JScript runtime error Wong number of arguments or invalid property..." I attached some codes.

        Here is a xml which will be transformed:
        [code=xml]
        <?xml version="1.0" encoding="UTF-8" ?>
        <?xml-stylesheet type="text/xsl" href="LOIPRO01_ 2_B2MML.xsl"?>
        <LOIPRO01>
        <MEINS>MTR</MEINS>
        </LOIPRO01>
        [/code]

        The LOIPRO01_2_B2MM L.xsl is:
        [code=xml]
        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:styleshe et version="1.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
        xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
        xmlns="http://www.yyy.com/ad/mes/b2mt-1.0"
        xmlns:bml="http ://www.wbf.org/xml/b2mml-v02"
        xmlns:LibUserFu nctions="http://www.yyy.com/UDF/LibUserFunction s"
        exclude-result-prefixes="xs xsl xsi LibUserFunction s">

        <xsl:import href="LibUserFu nctions.xsl"/>

        <xsl:template match="/LOIPRO01">
        <bml:UnitOfMeas ure>
        <xsl:call-template name="LibUserFu nctions:ISOUOMT oMESUOM">
        <xsl:with-param name="UOM" select="MEINS"/>
        </xsl:call-template>
        </bml:UnitOfMeasu re>
        </xsl:template>
        </xsl:stylesheet>
        [/code]

        and function ISOUOMToMESUOM is in the imported xsl LibUserFunction s.xsl:
        [code=xml]
        <?xml version="1.0" encoding="UTF-8"?>
        <xsl:styleshe et version="1.0"
        xmlns:xs="http://www.w3.org/2001/XMLSchema"
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
        xmlns:date="htt p://exslt.org/dates-and-times" extension-element-prefixes="date"
        xmlns:log="http ://www.xxx.com"
        xmlns:xsi="http ://www.w3.org/2001/XMLSchema-instance"
        xmlns:LibUserFu nctions="http://www.yyy.com/UDF/LibUserFunction s"
        exclude-result-prefixes="xs xsl xsi LibUserFunction s">

        <xsl:import href="UOMConver sion.xsl"/>

        <xsl:template name="LibUserFu nctions:ISOUOMT oMESUOM">
        <xsl:param name="UOM"/>
        <xsl:value-of select="log:UOM ISOTOMES($UOM)"/>
        </xsl:template>
        </xsl:stylesheet>
        [/code]
        the function log:UOMISOTOMES is in the imported UOMConversion.x sl:[code=xml]
        <?xml version="1.0"?>
        <xsl:styleshe et version="1.0"
        xmlns:xsl="http ://www.w3.org/1999/XSL/Transform"
        xmlns:log="http ://www.xxx.com"
        xmlns:msxsl="ur n:schemas-microsoft-com:xslt">

        <msxsl:script language="Javas cript" implements-prefix="log">
        <![CDATA[
        var ISOUOM= new Array("KGM","MT R","LTR","CT ");
        var MESUOM= new Array("KG","M", "L","KAR");
        function UOMISOTOMES(UOM In) {
        var i;
        var UOMOut;
        for (i=0; i<=ISOUOM.lengt h; i++)
        {
        if (UOMIn == ISOUOM[i]) {
        UOMOut = MESUOM[i];
        }
        }
        return UOMOut;
        }
        ]]>
        </msxsl:script>
        </xsl:stylesheet>
        [/code]

        Regards
        maxin

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          Originally posted by maxin
          However, I still get an error in the above javascript - "Microsoft JScript runtime error Wong number of arguments or invalid property..."
          you have a mistake in the for loop. the length of your array is 4, thus the last index is 3. change the end condition to "i < ISOUOM.length".

          tip: it is better coding style to compute the array length outside the loop. for larger arrays you get a performance advantage. (this is generally recommended for all programming languages)
          Code:
          l = array.length;
          for (i=0; i<l; i++) { ... }

          Comment

          • maxin
            New Member
            • Jan 2009
            • 13

            #6
            Hi Dormilich,
            Thanks so much for your answer. I changed my codes, But I stille get the same error. The interesting is if I make the javascript as .js and import it in html. No error occurs.
            Regards
            maxin

            Comment

            • maxin
              New Member
              • Jan 2009
              • 13

              #7
              The problem seems in <![CDATA[ ... ]]> of the UOMConversion.x sl. It does not like "==" in if (UOMIn == ISOUOM[i]). However, if I change it to "=", it doesnot work correctly either. Is this a special kind of javascript in CDATA?
              Regards
              maxin

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                Originally posted by maxin
                The interesting is if I make the javascript as .js and import it in html. No error occurs.
                maybe it's time to ask why you put in the JScript this way. to me it seems like an overkill to use xsl where a simple (and working) html call would suffice.

                PS: not that I'd always use the most straightforward way.........

                Comment

                • maxin
                  New Member
                  • Jan 2009
                  • 13

                  #9
                  It is not a html I will make. This xsl is used to transfer IDOC to B2MML when I import an IDOC from SAP to MES.

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    just had a quick lookup of IDoc & B2MML. where does the javascript fit in, since both types are xml?

                    Comment

                    • maxin
                      New Member
                      • Jan 2009
                      • 13

                      #11
                      MES will use LOIPRO01_2_B2MM L.xsl to transfer IDOC to B2MML, from one xml to another xml. I just nees a function to convert ISO code of UOM (Unit of measure KGM from SAP) to normal UOM (kg in MES) during the transform. However, I can not find a xpath function to do it. Therefor I made a by javascript.

                      Comment

                      • Dormilich
                        Recognized Expert Expert
                        • Aug 2008
                        • 8694

                        #12
                        do these units have their own node?

                        Comment

                        • maxin
                          New Member
                          • Jan 2009
                          • 13

                          #13
                          I just need a solution, no matter how to do it. Right now I made it as an array in javascript:
                          var ISOUOM= new Array("KGM","MT R","LTR","CT ");
                          var MESUOM= new Array("KG","M", "L","KAR");

                          Comment

                          • Dormilich
                            Recognized Expert Expert
                            • Aug 2008
                            • 8694

                            #14
                            it really depends on how these values are stored in the xml. for instance it is possible to convert them with a <choose> construct. if you use a programming language to do the xsl conversion, you may use its string replace functions to convert the units before (or after) the transformation. ...

                            Comment

                            • maxin
                              New Member
                              • Jan 2009
                              • 13

                              #15
                              Hi Dormilich,
                              It is good to discuss my problem with you.
                              However, the list of UOM can very long, so it is not so good to use <choose>. I could not find not how to use string replace functions? What do you mean?
                              Regards
                              Xin

                              Comment

                              Working...