Re: AW: AW: xalan-j extension passing the value of a xsl:variable to the extension class

Nathan Nadeau <[email protected]> Mon, 04 Oct 2010 11:18:34 -0400
Newsgroups gmane.text.xml.xalan.java.user
Organization Gleim Publications
Message-ID <[email protected]>
Markus,

See http://xml.apache.org/xalan-j/extensions.html#ext-elements. Your 
java code must use the 
org.apache.xalan.templates.ElemExtensionCall.getAttribute() function, 
which has 3 arguments, in order to get the "evaluated" version of the 
attribute, which will properly process the curly braces "{}" that I 
originally mentioned. The getAttribute() function you're currently using 
just returns the raw value of your select attribute.

The below java code worked for me (note elem is of type 
org.apache.xalan.templates, not org.w3c.dom.Element):
-------
public void lineWeight(org.apache.xalan.extensions.XSLProcessorContext 
context, org.apache.xalan.templates.ElemExtensionCall elem) {
   
       String value = "";
       try{
          value = 
elem.getAttribute("select",context.getContextNode(),context.getTransformer());
       } catch (TransformerException e){}
       int val;
       try
       {
          val = Integer.parseInt (value);
       } catch (NumberFormatException e)
       {
          e.printStackTrace ();
          val = 0;
       }       
       System.out.println("in lineWeight(), value: " + value);
    }

Sticker, Markus / Kuehne + Nagel / Ham MI-EC /external wrote:
>
> Sticker, Markus / Kuehne + Nagel / Ham MI-EC /external wrote:
>> Hello Nathan,
>>
>> no, I'm sorry it doesn't work:
>>
>>  java.lang.NumberFormatException: For input string: "{exslt:node-set($def)/bo/totels}"
>> 	at java.lang.NumberFormatException.forInputString(Unknown Source)
>>
>> -----Urspruengliche Nachricht-----
>> Von: Nathan Nadeau [mailto:[email protected]] 
>> Gesendet: Montag, 4. Oktober 2010 14:54
>> An: Sticker, Markus / Kuehne + Nagel / Ham MI-EC /external
>> Cc: [email protected]
>> Betreff: Re: xalan-j extension passing the value of a xsl:variable to the extension class
>>
>> Markus,
>>
>> The expression "exslt:node-set($def)/bo/totals" is not getting evaluated because you list it in a non-xsl (<xsl:>) element. Because <my:lineWeight> is not using the XSL namespace, the XSLT processor thinks it is just an element that is to be copied to the output. 
>> Therefore the value of your select attribute is just the string "exslt:node-set($def)/bo/totals", and that is what your extension function is receiving.
>>
>> Try using
>>
>> <my:lineWeight select="{exslt:node-set($def)/bo/totals}" />
>>
>> in your XSLT code, which will tell the processor to evaluate the expression (note the curly braces).
>>
>> Sticker, Markus / Kuehne + Nagel / Ham MI-EC /external wrote:
>>   
>>> Hello,
>>>
>>> I have a problem with my extension, because I didn't get it working to 
>>> pass a value of a xsl:variable to my extension.
>>>
>>> -------8<-------- XSLT-CODE ----------------------
>>> 		...
>>> 		<xsl:variable name="def">
>>> 	           <bo>
>>> 			<item>1</item>
>>> 			<line>4</line>
>>> 			<totals>6</totals>
>>>                 </bo>
>>> 		</xsl:variable>
>>> 		<my:lineWeight select="exslt:node-set($def)/bo/totals" />
>>> 		
>>> 		....
>>> 		
>>> 		
>>>
>>> -------8<-------- Java-CODE ----------------------
>>>
>>> 	public void lineWeight(org.apache.xalan.extensions.XSLProcessorContext context,
>>> 			org.w3c.dom.Element elem) {
>>> 		
>>> 		  String value = elem.getAttribute("select");
>>> 				    int val;
>>> 		    try 
>>> 		    {
>>> 		      val = Integer.parseInt (value);
>>> 		    } catch (NumberFormatException e) 
>>> 		    {
>>> 		        e.printStackTrace ();
>>> 		        val = 0;
>>> 		      }
>>> 		    
>>> 		lineCount += val;
>>> 	}
>>>
>>>
>>> -------8<-------- Error ----------------------
>>>
>>> java.lang.NumberFormatException: For input string: "exslt:node-set($def)/bo/totals"
>>> 	at java.lang.NumberFormatException.forInputString(Unknown Source)
>>> 	at java.lang.Integer.parseInt(Unknown Source)
>>> 	at java.lang.Integer.parseInt(Unknown Source)
>>> 	at kn.iopm.xalan.ext.textFiller.line(textFiller.java:51)
>>> 	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
>>> 	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
>>> 	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
>>> 	at java.lang.reflect.Method.invoke(Unknown Source)
>>> 	at org.apache.xalan.extensions.ExtensionHandlerJavaClass.processElement(ExtensionHandlerJavaClass.java:517)
>>> 	at org.apache.xalan.templates.ElemExtensionCall.execute(ElemExtensionCall.java:234)
>>> 	at org.apache.xalan.templates.ElemForEach.transformSelectedNodes(ElemForEach.java:425)
>>> 	at org.apache.xalan.templates.ElemForEach.execute(ElemForEach.java:265)
>>> 	at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
>>> 	at org.apache.xalan.templates.ElemTemplate.execute(ElemTemplate.java:394)
>>> 	at org.apache.xalan.templates.ElemCallTemplate.execute(ElemCallTemplate.java:248)
>>> 	at org.apache.xalan.transformer.TransformerImpl.executeChildTemplates(TransformerImpl.java:2400)
>>> 	at org.apache.xalan.transformer.TransformerImpl.applyTemplateToNode(TransformerImpl.java:2270)
>>> 	at org.apache.xalan.transformer.TransformerImpl.transformNode(TransformerImpl.java:1356)
>>> 	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:709)
>>> 	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1273)
>>> 	at org.apache.xalan.transformer.TransformerImpl.transform(TransformerImpl.java:1251)
>>> 	at kn.iopm.xalan.ext.run_test.main(run_test.java:28)
>>>
>>>
>>>
>>> Does somebody know what I'm doing wrong? It seams that the SELECT-Attribute wasn't executed.
>>> thanks for your help
>>>
>>> best regards
>>>
>>> Markus
>>>
>>> Kuehne + Nagel (AG & Co.) KG, Geschaeftsleitung: Hans-Georg Brinkmann 
>>> (Vors.), Dirk Blesius, Reiner Heiken, Bruno Mang, Alfred Manke, 
>>> Christian Marnetté, Mark Reinhardt, Jens Wollesen, Klaus Jaeger 
>>> (stellv.), Sitz: Bremen, Registergericht: Bremen, HRA 21928, 
>>> USt-IdNr.: DE 812773878, Persoenlich haftende Gesellschaft: Kuehne & 
>>> Nagel A.G., Sitz: Contern/Luxemburg Geschaeftsfuehrender 
>>> Verwaltungsrat: Klaus-Michael Kuehne
>>>
>>>
>>>       

-- 
Nathan Nadeau
[email protected]
Software Development
Gleim Publications, Inc.
http://www.gleim.com