1

I need to get ASCII value of character and Convert ASCII code back to character if it satisfies certain conditions.

So I came across these functions:

string-to-codepoints(string)

and

codepoints-to-string((int,int,...))

provided in XSLT 2.0 (Or Rather XPATH 2.0) But unfortunately I need to use XSLT 1.0 for this task.

So My question is

Is there any equivalent of these functions in XSLT 1.0? If not Can we design it? Can experts here help me in that?

Thanks in advance

15
  • Well, there are only a few ASCII characters so if you are really only interested in ASCII characters in XSLT 1.0 then create an XML file that maps the 127 ASCII characters to their ASCII value and use that with substring. string-to-codepoints however works with Unicode and not only ASCII, so there your mapping file would need to be rather large. Commented Aug 5, 2016 at 9:22
  • 1
    What XSLT processor are you using, and in what environment? Most have a mechanism for extensions, possibly you can use this to your advantage. Commented Aug 5, 2016 at 10:01
  • 1
    Why don't you explain what exactly are you trying to accomplish, not how you think it needs to be accomplished - see: xyproblem.info Commented Aug 5, 2016 at 10:13
  • @Martin and All Sorry for delay in reply.. Was stuck in some work. I've edited question now, Can you please provide a solution Commented Aug 5, 2016 at 11:04
  • Well, to replace a single character with another you can use e.g. translate('AAABBBCCC', 'B', 'D') even in XPath 1.0 without any extension Commented Aug 5, 2016 at 11:08

2 Answers 2

3

It is possible to replace all characters with codepoints above 255 by "?" using pure XSLT 1.0 without extensions.

Define a variable

<xsl:variable name="upto255">&#x9;&#xa;&#xd; !"#$%.../01234...ABC...abc...úûüýþÿ</xsl:variable>

whose value is a string containing all the characters in the range 0..255 that are legal in XML.

Then use the double-translate trick:

<xsl:variable name="above255" select="translate($input, $upto255, '')"/>

This variable is a string containing all the non-Latin-1 characters present in the input string. Then use the recursive template

<xsl:template name="pad">
   <xsl:param name="char"/>
   <xsl:param name="count"/>
   <xsl:choose>
    <xsl:when test="$count=0"/>
    <xsl:otherwise>
      <xsl:value-of select="$char"/>
      <xsl:call-template name="pad">
        <xsl:with-param name="char" select="$char"/>
        <xsl:with-param name="count" select="$count - 1"/>
      </xsl:call-template>
   </xsl:otherwise>
  </xsl:choose>
</xsl:template>

to create a string of the right number of question marks:

<xsl:variable name="qqq">
  <xsl:call-template name="pad">
    <xsl:with-param name="char" select="'?'"/>
    <xsl:with-param name="count" select="string-length($above255)"/>
  </xsl:call-template>
</xsl:variable>

and then do the substitution:

<xsl:value-of select="translate($input, $above255, $qqq)"/>

But of course since you are in Java there is no excuse for writing all this XSLT 1.0 code which could be replaced by a single line of code if you switched to an XSLT 2.0 processor such as Saxon.

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

4 Comments

Thanks for you time. I've edited my question as I can't use java extension any more. Can you please Explain Me how it works.
Specially The part $above255 how are you calculating that
The key part of my answer was invisible because I failed to tag it as code. Disappointed that it should have taken you 6 weeks to respond.
Sorry for delayed response.. Accepting this one. You are a XSLT genius. Thanks a lot. You saved me :).
0

Based on your comments you want to perform a string replacement based on a regular expression. If you are using Java and Xalan then I think you can use e.g. java:replaceAll($inputString, $regExpPattern, $replacementString) to call the Java String method replaceAll, here is a simple example

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:java="http://xml.apache.org/xalan/java"
    version="1.0"
    exclude-result-prefixes="java">

    <xsl:template match="/">
        <xsl:value-of select="java:replaceAll('abc-123-def','\w+', '?')"/>
    </xsl:template>

</xsl:stylesheet>

which outputs ?-?-? for me with Xalan.

On the other hand if you are using Java then you should consider moving to Saxon 9 and XSLT 2.0 as that way you can use the XPath 2.0 replace function (replace('abc-123-def', '\w+', '?')) without any need for extensions.

I am not sure what that has to do with your original question about string-to-codepoints and the ASCII code of characters.

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.