You are viewing a plain text version of this content. The canonical link for it is here.
Posted to j-users@xalan.apache.org by cyfer zhang <cy...@hotmail.com> on 2001/10/04 20:07:03 UTC

I am sorry for the mess information about xalan extension

hello every body.
Sorry the hotmail make my email a mess, and I am resending it.

I met some thing strange when I am trying to extend Xalan with Javascript.
And I am asking why?
And the code is below
*************************************************************
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt" xmlns:my-ext="ext1"
extension-element-prefixes="my-ext">
<xsl:output encoding="GB2312"/>
<lxslt:component prefix="my-ext" elements="out"    functions="displaySex">
<lxslt:script lang="javascript">
function out(xslProcessorContext, elem){
   var o = elem.getChildNodes();
   var oTransformer = xslProcessorContext.getTransformer();
   try{
     for(var i = 0 ; i &lt; o.getLength() ; i ++ ){
       o.item(i).execute( oTransformer );
     }
   }catch( e ){ return e; }
   return ",吃了吗?";
}

function displaySex( sex ){
   var sexStr = new String(sex.item(0).getNodeValue());
   if( "m" == sexStr ) return "Sir";
   else if( "f" ==sexStr ) return "Madam";
   else return new String(sex.item(0).getNodeValue());
}
</lxslt:script>
</lxslt:component>
<xsl:template match="person">
    <result>
        <my-ext:out>
             <xsl:value-of select="."/>
             <xsl:value-of select="my-ext:displaySex(@sex)
"/>你好</my-ext:out>
    </result>
</xsl:template>
</xsl:stylesheet>
************************************************************
I called function displaySex in this way, <xsl:value-of
select="my-ext:displaySex(@sex) "/>, and I think the argument @sex should be
a string. However it turn out to be a DTM object equivelant to NodeList. I
am not sure why. And whem I implement the same funtion in Java, the argument
called in the same way is a String. And the java code is below.
**************************************************************
package a;
import org.apache.xalan.extensions.XSLProcessorContext;
import org.apache.xalan.templates.ElemExtensionCall;
import org.apache.xalan.templates.ElemTemplateElement;
public class A{
public String out( XSLProcessorContext xslProcContext
,                                           ElemExtensionCall elmExtCall){
org.apache.xalan.transformer.TransformerImpl oTrsfImpl =
            xslProcContext.getTransformer();
org.w3c.dom.NodeList oList = elmExtCall.getChildNodes();
try{
   for( int i = 0 ; i < oList.getLength() ; i ++ ){
    ((ElemTemplateElement)oList.item(i)).execute(oTrsfImpl);
   }
}catch(Exception e){
   e.printStackTrace();
}
return ",吃了吗?";
}
public String displaySex( String strSex){
if( strSex.equals("m") ) return "先生";
else if( strSex.equals("f") ) return "女士";
else return strSex;
}
}
*******************************************************************
And the xsl document that call the java code below
******************************************************************
<?xml version="1.0" encoding="GB2312"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0"
xmlns:lxslt="http://xml.apache.org/xslt" xmlns:my-ext="ext1"
extension-element-prefixes="my-ext">
<xsl:output encoding="GB2312"/>
<lxslt:component prefix="my-ext" elements="out"   functions="displaySex">
<lxslt:script lang="javaclass" src="xalan://a.A">       </lxslt:script>
</lxslt:component><xsl:template match="person">
<results>
   <my-ext:out>
     <xsl:value-of select="."/>
     <xsl:value-of select="my-ext:displaySex(@sex)"/>
     你好
     </my-ext:out>
   </results>
</xsl:template>
</xsl:stylesheet>
**********************************************************

And my question is why there are such difference between Javascript and 
Java?

Cyfer(Zhang, Xin)

_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com/intl.asp