We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you two Citrix podcasts. Learn about 2020 trends and get answers to your biggest Citrix questions!Listen Now

x

Converting Operators into Hexadecimal

Medium Priority
454 Views
Last Modified: 2013-11-18
Hello Experts,

I have an input xml file which contains Operators like below....

<test symbol=","/>
<test symbol=":"/>
<test symbol="!"/>
<test symbol="!="/>
<test symbol="!=="/>
<test symbol="?:"/>
<test symbol="//"/>
<test symbol="/"/>
<test symbol="/*..*/"/>
<test symbol="/="/>
<test symbol="."/>
<test symbol="^"/>
<test symbol="^="/>
<test symbol="~"/>
<test symbol="&quot;"/>
<test symbol="()"/>
<test symbol="--"/>
<test symbol="-"/>
<test symbol="[]"/>
<test symbol="{}"/>
<test symbol="*"/>
<test symbol="*="/>
<test symbol="&amp;"/>
<test symbol="&amp;&amp;"/>
<test symbol="&amp;&amp;="/>
<test symbol="&amp;="/>
<test symbol="%"/>
<test symbol="%="/>
<test symbol="+"/>
<test symbol="++"/>
<test symbol="+="/>
<test symbol="&lt;"/>
<test symbol="&lt;&lt;"/>
<test symbol="&lt;&lt;="/>
<test symbol="&lt;="/>
<test symbol="="/>
<test symbol="-="/>
<test symbol="=="/>
<test symbol="==="/>
<test symbol="&gt;"/>
<test symbol="&gt;="/>
<test symbol="&gt;&gt;"/>
<test symbol="&gt;&gt;="/>
<test symbol="&gt;&gt;&gt;"/>
<test symbol="&gt;&gt;&gt;="/>
<test symbol="|"/>
<test symbol="|="/>
<test symbol="||"/>
<test symbol="||="/>
<test symbol="/"/>
<test symbol=".."/>
<test symbol="@"/>
<test symbol="{ }"/>
<test symbol="[ ]"/>
<test symbol="&lt; &gt;"/>
<test symbol=".."/>
<test symbol="( )"/>
<test symbol="+"/>
<test symbol="+="/>
<test symbol="::"/>


I would like to output xml file which will contains these operators hexadecimal values like below for example...

<conv>&#x2C;</conv>
<conv>&#x3A;</conv>
<conv>&#x21;</conv>


Comment
Watch Question

zc2
CERTIFIED EXPERT

Commented:
if you use MSXML processor, you can embed a JavaScript section to convert your input data to the hex form.

Author

Commented:
Hello Experts,

Can you pls show some sample for this?

Is this possible using xslt 2.0 only?

Thanks,
Shailesh
CERTIFIED EXPERT
Commented:
I'm sorry, I don't know is it possible to do that in XSLT 2.0
If you still interested in a MSXML based script implementation, then below you can find a sample:
<xsl:stylesheet exclude-result-prefixes="msxsl js"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
      xmlns:msxsl="urn:schemas-microsoft-com:xslt"
      xmlns:js="http://mycompany.com/mynamespace" version="1.0">
      
<xsl:output method="xml" indent="yes" encoding="iso-8859-1"/>
<msxsl:script language="JScript" implements-prefix="js">
<![CDATA[
 
   function convert( c ) {
      var h="";
      for(var i=0;i<c.length;i++) h+="&#x"+c.charCodeAt(i).toString(0x10)+";";
      return h;
   }
 
]]>
</msxsl:script>
 
<xsl:template match="test">
<conv>
<xsl:value-of disable-output-escaping="yes" select="js:convert(string(@symbol))"/>
</conv>
</xsl:template>
    
</xsl:stylesheet>

Open in new window

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Hello Experts,

This looks good for me. I will test it and let you know.

Thanks,
Shailesh

Author

Commented:
Hello Experts,

While transforming I am getting following errors....

Error at xsl:value-of on line 291 of file:/D:/parse.xslt:
  XPST0017: XPath syntax error at char 27 on line 291 in {js:convert(string(@symbol))}:
    Cannot find a matching 1-argument function named {http://mycompany.com/mynamespace}convert()
Failed to compile stylesheet. 1 error detected.

Can you please suggest what's wrong with this or do I missed something?

Thanks,
Shailesh
Gertone (Geert Bormans)Information Architect
CERTIFIED EXPERT
Top Expert 2006

Commented:
There is no XSLT2 implementation in msxml.
The javascript solution will not work for you

You can in XSLT2 create a lookup table as some XML in a variable
and use XPath to access the correct Hex value

In XSLT2 it is worthwhile to evaluate character maps to do what you need
zc2
CERTIFIED EXPERT

Commented:
the sample I provided works only in MSXML implementation
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.