469,300 Members | 2,203 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,300 developers. It's quick & easy.

How to import javascript in xsl

13
Hi,

I write a javascript function in xsl (UOMConversion.xsl) as follow:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.  
  5. <script language="Javascript">
  6. <![CDATA[
  7. var ISOUOM= new Array("KGM","MTR","LTR","CT");
  8. var MESUOM= new Array("KG","M","L","KAR");
  9.  
  10. function UOMISOTOMES(UOMIn) {
  11.     var i;
  12.     var UOMOut;
  13.     for (i=0; i<=ISOUOM.length; i++)
  14.     {
  15.         if (UOMIn == ISOUOM[i]) {
  16.             UOMOut = MESUOM[i];
  17.         }
  18.     }
  19.     return UOMOut;
  20. }
  21. ]]>
  22. </script>    
  23. </xsl:stylesheet>
I would like to import above UOMConversion.xsl into a xsl (LibUserFunctions.xsl). I write:

Expand|Select|Wrap|Line Numbers
  1. <xsl:stylesheet version="1.0" 
  2.     xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  3.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  4.  
  5. <xsl:import href="UOMConversion.xsl"/> 
  6. ....
  7. </xsl:stylesheet>
I got an error when I use LibUserFunctions.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
Jan 18 '09 #1
15 8114
Dormilich
8,651 Expert Mod 8TB
@maxin
as stated in the error message, <script> is not an allowed child element of <xsl:stylesheet> see XSL Transformations (XSLT) for the allowed elements.
Jan 18 '09 #2
maxin
13
I also used date function EXSLT. The date.msxsl.xsl has also some javascript as
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3.                 xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4.                 xmlns:date="http://exslt.org/dates-and-times"
  5.                 xmlns:msxsl="urn:schemas-microsoft-com:xslt"
  6.                 extension-element-prefixes="date msxsl">
  7.  
  8. <msxsl:script language="Javascript" implements-prefix="date">
  9. <![CDATA[
  10. ...
  11. ]]>
  12. </msxsl:script>
  13. </xsl:stylesheet>
The different is I dont have the namespace. I don't have any problem to import date.msxsl.xsl.

Regards
maxin
Jan 18 '09 #3
maxin
13
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:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8" ?> 
  2. <?xml-stylesheet type="text/xsl" href="LOIPRO01_2_B2MML.xsl"?> 
  3. <LOIPRO01>
  4. <MEINS>MTR</MEINS> 
  5. </LOIPRO01>
  6.  
The LOIPRO01_2_B2MML.xsl is:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" 
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  5. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  6. xmlns="http://www.yyy.com/ad/mes/b2mt-1.0" 
  7. xmlns:bml="http://www.wbf.org/xml/b2mml-v02" 
  8. xmlns:LibUserFunctions="http://www.yyy.com/UDF/LibUserFunctions" 
  9.     exclude-result-prefixes="xs xsl xsi LibUserFunctions">
  10.  
  11. <xsl:import href="LibUserFunctions.xsl"/>
  12.  
  13. <xsl:template match="/LOIPRO01">
  14.        <bml:UnitOfMeasure>
  15.     <xsl:call-template name="LibUserFunctions:ISOUOMToMESUOM">
  16.           <xsl:with-param name="UOM" select="MEINS"/>
  17.                </xsl:call-template>
  18.        </bml:UnitOfMeasure>
  19. </xsl:template>
  20. </xsl:stylesheet>
  21.  
and function ISOUOMToMESUOM is in the imported xsl LibUserFunctions.xsl:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" 
  3. xmlns:xs="http://www.w3.org/2001/XMLSchema" 
  4. xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
  5. xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="date" 
  6. xmlns:log="http://www.xxx.com" 
  7. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
  8. xmlns:LibUserFunctions="http://www.yyy.com/UDF/LibUserFunctions" 
  9.     exclude-result-prefixes="xs xsl xsi LibUserFunctions">
  10.  
  11. <xsl:import href="UOMConversion.xsl"/> 
  12.  
  13. <xsl:template name="LibUserFunctions:ISOUOMToMESUOM">
  14.     <xsl:param name="UOM"/>
  15.     <xsl:value-of select="log:UOMISOTOMES($UOM)"/>
  16. </xsl:template>
  17. </xsl:stylesheet>
  18.  
the function log:UOMISOTOMES is in the imported UOMConversion.xsl:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0"?>
  2. <xsl:stylesheet version="1.0"
  3.     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  4.     xmlns:log="http://www.xxx.com"
  5.     xmlns:msxsl="urn:schemas-microsoft-com:xslt">
  6.  
  7. <msxsl:script language="Javascript" implements-prefix="log">
  8. <![CDATA[
  9. var ISOUOM= new Array("KGM","MTR","LTR","CT");
  10. var MESUOM= new Array("KG","M","L","KAR");
  11. function UOMISOTOMES(UOMIn) {
  12.     var i;
  13.     var UOMOut;
  14.     for (i=0; i<=ISOUOM.length; i++)
  15.     {
  16.         if (UOMIn == ISOUOM[i]) {
  17.             UOMOut = MESUOM[i];
  18.         }
  19.     }
  20.     return UOMOut;
  21. }
  22. ]]>
  23. </msxsl:script>    
  24. </xsl:stylesheet>
  25.  
Regards
maxin
Jan 18 '09 #4
Dormilich
8,651 Expert Mod 8TB
@maxin
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)
Expand|Select|Wrap|Line Numbers
  1. l = array.length;
  2. for (i=0; i<l; i++) { ... }
Jan 18 '09 #5
maxin
13
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
Jan 18 '09 #6
maxin
13
The problem seems in <![CDATA[ ... ]]> of the UOMConversion.xsl. 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
Jan 18 '09 #7
Dormilich
8,651 Expert Mod 8TB
@maxin
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.........
Jan 18 '09 #8
maxin
13
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.
Jan 19 '09 #9
Dormilich
8,651 Expert Mod 8TB
just had a quick lookup of IDoc & B2MML. where does the javascript fit in, since both types are xml?
Jan 19 '09 #10
maxin
13
MES will use LOIPRO01_2_B2MML.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.
Jan 19 '09 #11
Dormilich
8,651 Expert Mod 8TB
do these units have their own node?
Jan 19 '09 #12
maxin
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","MTR","LTR","CT");
var MESUOM= new Array("KG","M","L","KAR");
Jan 19 '09 #13
Dormilich
8,651 Expert Mod 8TB
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....
Jan 19 '09 #14
maxin
13
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
Jan 19 '09 #15
Dormilich
8,651 Expert Mod 8TB
@maxin
that's probably best explained with an example. I use PHP as programming language (because I'm used to it, but it works for every other language too, except that the names & functions differ)
Expand|Select|Wrap|Line Numbers
  1. // xml input file
  2. $xml_in = "idoc.xml";
  3.  
  4. // now do the unit replacement
  5. // loading file to a string
  6. $xml_in_cont = file_get_contents($xml_in);
  7. // replace the unit strings (works with arrays too)
  8. $xml_in_conv = str_replace("MTR", "M", $xml_in_cont);
  9.  
  10. // loading the xslt processor
  11. $proc = new XSLTProcessor;
  12. // ... here comes the loading of the required files (left out)
  13. $xml_out_cont = $proc->transformToXML();
  14.  
  15. // saving file to disc
  16. $xml_out = file_put_contents("b2mml.xml", $xml_out_cont);
you use a programm to get the file into string, use the appropriate functions to replace the units, do the xsl transformation, and save the result as file.
Jan 20 '09 #16

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

3 posts views Thread by Daniel | last post: by
2 posts views Thread by Ricardo Garcia | last post: by
7 posts views Thread by Kevin Newman | last post: by
1 post views Thread by Joseph Scoccimaro | last post: by
1 post views Thread by Frits v/d Laan | last post: by
1 post views Thread by Praveen | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by harlem98 | last post: by
1 post views Thread by Geralt96 | last post: by
reply views Thread by harlem98 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.