XSLT java functions in curly braces - java

I am learning XSLT and I see some codes like the following:
<node id="{java:next($idgen)}:state" pred="should" mood="dcl">
where next is a java function. What are the curly braces used for?
Besides I am quite confused at how java functions are called in XSLT. In my sample code, XSLT just calls java functions like above without specifying the location of the java class file, even the class name is not specified.

What are the curly braces used for?
The curly braces have nothing to do with Java. They indicate to the processor that the contents are to be evaluated as an expression - see: https://www.w3.org/TR/xslt/#attribute-value-templates
Besides I am quite confused at how java functions are called in XSLT.
That depends on your specific processor. Calling Java functions is not part of the XSLT standard, and only some processors support it as an extension.

You have shown a literal result element with an attribute value template where the XPath expression inside the attribute value template uses some XSLT processor specific mechanism to call a Java method of the variable $idgen.

Related

Call Java Function in XSLT2

I have the Java method ...
public static Object parseXMLtoXLSX(File xmlFile, String path)
So I want to call the method from XSLT.
I understand, that I have to introduce the class in my XSLT File e.g. like this:
<xsl:stylesheet version="2.0" xmlns:trans="pathToMyJavaClass">
But how can I call the method?
Is this the right way?:
<xsl:value-of select="trans:parseXMLtoXLSX($xmlFIle,$path)" />
But how can I store the Java File Object, that I get back from the Method in a Variable?
Edit: I can't show the < > in this question...
The calling conventions from XSLT to other languages depend entirely on which XSLT processor you are using, so you need to provide this information.
If you're using XSLT 2.0 under Java then it's likely that the processor you are using is Saxon, in which case the calling conventions are documented at http://saxonica.com/documentation/index.html#!extensibility/functions
In cases where you're handling objects (like a Java java.util.File) that have no equivalent in the XDM data model used by XSLT, the calling conventions can be quite complicated. It's simpler if you organize things so that you only need to pass simple values like strings and integers. For example, write another method in Java that accepts a String (containing a file name) rather than a File.

How to get value of the attribute of the xml file using java

I am getting following code in response in xml
<.........>
<stsuuser:Attribute name="authorized" type="urn:ibm:names:ITFIM:oauth:response:decision">
<stsuuser:Value>TRUE</stsuuser:Value>
</stsuuser:Attribute>
<.........>
Now how I can get <stsuuser:Value> is true or false using Java?
There are quite literally dozens of libraries for Java that parse XML. But, I find JOOX by far the simplest to use. It lacks quite a few common features of other libraries, but unlike them, it's a joy to use. It provides you with a jQuery-like API (even has the $ function) and, in your case, you'd use it as follows:
import static org.joox.JOOX.$;
...
//Get the value of "authorized" attribute
$(...).xpath("//Attribute[#name='authorized']/Value").text();
The $ function will accepts a String, a file, a stream, a reader etc, so you can initialize with pretty much anything.

What is the purpose of com.sun.org.apache.xpath.internal.operations.String?

I was writing a Java program, and when I tried to use a string, a suggestion that popped up was com.sun.org.apache.xpath.internal.operations.String.
What is com.sun.org.apache.xpath.internal.operations.String, and when should I use it?
I have looked online, but I cannot find the documentation.
Thank you!
You can work backwards based on the package name. The first piece that gives us a clue to what it does is Xpath. Then there's Apache, which is a the Apache Software Foundation. So it's an API called Xpath from Apache.
XPath is a language for parsing and processing XML content. It has some simple capabilities such as math or data conversions. In this case, String is a unary operator that converts text to a String, as opposed to a Number or a boolean. It has one static method operate:
public XObject operate(XObject right)
throws TransformerException
It takes an XObject of some unknown type and converts it to a String type.
An example snippet of XPath for String conversion would be, at its barest:
string($x) //convert variable x to a String
You probably would never need to call this, seeing as it is part of an internal package. It is most probably used when interpreting XML to get the result as a String for use with other Xpath internal components. Indeed, the returned result is an XObject with a type associated to it, so interpreting it as a normal Java String would break something. It is nice to helpful to know that it's there, and it is public per design, but you'd ever use this in the real world unless you're expanding on the xpath API yourself.
Source Package Tree
This is a class used by some Apache implementation that unfortunately shows up in your Java CodeAssist.
If you are using Eclipse you can check that out:
http://www.eclipse.org/forums/index.php/t/80401/

How is this a valid Java method invocation?

I've just been reading Marko Rodriguez's excellent blog post about different types of databases. Whilst reading I noticed some syntax...
// put data
db.put('marko');
db.put(31);
db.put(true);
// get data
Iterator results = db.get();
Iterator filteredResults = db.get{it.startsWith('ma')};
...which I presumed was a snippet of Java, but I've never seen a method invoked using curly braces like this before - db.get{it.startsWith('ma')}.
Any details/thoughts on this would be appreciated!
That looks like it's probably Groovy (using closures) rather than Java. Note that it also uses 'marko' which isn't valid Java. (Java uses single quotes for character literals, not string literals.)
That would also fit with the author's involvement in Gremlin, which is written in Groovy.

Best sandboxed expression language for JVM

I want an expression language that runs on the JVM and includes support for
math expressions, including operator priority
string expressions, like substring, etc
supports named functions
this allows me to decorate and control exactly who and what functions can be executed.
read/write variables that are "typeless" / allow type conversion in a controlled manner.
does not allow arbitary java scriptlets.
it should not be possible to include constructs like new Someclass()
cannot execute arbitrary static or otherwise method
does not allow any OGNL like expressions.
I only want to functions I map to be available.
support for control constructs like if this then that is for the moment optional.
must be embeddable.
This previous stackoverflow question is similar, but:
does not really answer "how" or "what" as does the above,
allows java object expressions, throwing an exception from a SecurityManager to stop method execution, which is nasty and wrong.
java object like expressions should be an error at parse time.
jexel seem to be closest possible match, but License is a bit horrible (GPL/Commercial).
If you only want the scripts to output text, then Apache Velocity fit's your constraints quite well. It runs in an environment where it only has access to the objects you give it, but can do things like basic math.
The Apache license is a bit friendlier than GPL too.

Categories

Resources