Project

Profile

Help

Cannot add an element to a Vector from xslt

Added by Anonymous over 19 years ago

Legacy ID: #3099595 Legacy Poster: gabriel003 (gabriel003)

I am creating an instance of Vector using extension functions, but the add method does not work. The following code <?xml version="1.0" encoding="UTF-8" ?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0" xmlns:vector="java:java.util.Vector" > <xsl:template match="/"> <xsl:variable name="v" select="vector:new()"/> <xsl:value-of select="if (vector:add($v, 'element1')) then vector:size($v) else -1"/> </xsl:template> </xsl:stylesheet> should produce 1 as output, instead it gives 0


Replies (2)

RE: Cannot add an element to a Vector from xs - Added by Anonymous over 19 years ago

Legacy ID: #3099648 Legacy Poster: Michael Kay (mhkay)

What release are you using? I don't actually get as far as this with 8.4, the output I get is: Saxon 8.4 from Saxonica Java version 1.5.0_02 Stylesheet compilation time: 2594 milliseconds Processing file:/c:/temp/test.xml Building tree for file:/c:/temp/test.xml using class net.sf.saxon.tinytree.TinyBuilder Tree built in 0 milliseconds Tree size: 4 nodes, 7 characters, 0 attributes Error on line 7 of file:/c:/temp/test.xsl: Error in call to extension function {public synchronized boolean java.util.Vector.add(java.lang.Object)}: Object is null Transformation failed: Run-time errors were reported The problem here is that the Java collection classes are treated specially by the extension function API. When you pass an empty Java list to a function (in this case the call on add(), it is treated as if you passed an empty XPath sequence, which is in turn mapped to a Java null. I'm afraid that because the type systems of XPath and Java are different, there are bound to be occasional mismatches like this. The way round it is to write a wrapper class of your own in Java, and call the methods on the wrapper class. However, calling a method like Vector.add() is not recommended practice anyway: it's very hard to ensure that Saxon will call your extension functions in the order you expect, so methods that have side-effects can be very difficult to debug. Michael Kay

RE: Cannot add an element to a Vector from xs - Added by Anonymous almost 19 years ago

Legacy ID: #3260409 Legacy Poster: gabriel003 (gabriel003)

I have solved the problem using HashSet instead of Vector, as it is matched correctly by the extension function API and provides similar functionality. I actually was using release 8.3, but I made the test from Oxygen5, which embeds release 8.1

    (1-2/2)

    Please register to reply