0

How can I "translate" this XSLT code to Java ?

<xsl:value-of select="number(string-to-codepoints(upper-case($char)) - string-to-codepoints('A'))+10"/>

I only know that: "The fn:string-to-codepoints function returns a sequence of xs:integer values representing the Unicode code points."

From the example that is given in (http://www.xsltfunctions.com/xsl/fn_string-to-codepoints.html) :

string-to-codepoints('a') = 97

I found this:

char ch = 'a';
System.out.println(String.format("\\u%04x", (int) ch));

But I get : \u0061

2 Answers 2

2

For a single char you can just cast it to int to get the decimal value:

System.out.println((int)ch);

For a String there's .toCharArray() to convert it to a char[] but that isn't quite the same as a "sequence of codepoints" if the String involves Unicode characters outside the BMP (i.e. above U+FFFF), which are represented in Java as a surrogate pair of two char values. To handle surrogates properly you would need to use a technique like the one described in this answer.

To answer the specific question you ask, you can do

number(string-to-codepoints(upper-case($char)) - string-to-codepoints('A'))+10

in Java as

char ch = // wherever you get $char from
int num = Character.toUpperCase(ch) - 'A' + 10;

since char is an integer type in Java and you can add or subtract char values like any other number.

But this will probably only give you a sensible answer when the initial ch is an ASCII letter.

Sign up to request clarification or add additional context in comments.

Comments

1

You print the value as unicode escape sequence; XSLT prints a decimal value.

This should work much better:

System.out.println("a".codePointAt(0));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.