将十六进制转换为 ascii 字符 xslt

convert hex to ascii characters xslt

我想知道在 XSLT 1.0 中将十六进制字符转换为 ascii 字符的逻辑是什么样的。 我需要使用 XSLT 1.0 模板将 496e7465726e616c204d65646963696e65 转换为 Internal Medicine

我有一个将 ascii 转换为十六进制的模板,只需要帮助来反转逻辑。一些 XSLT 专家可以帮助我理解逻辑吗?

  <xsl:output method="xml" indent="yes"/>

  <!-- the next line is all on one line and the before the ! is a space -->
  <xsl:variable name="ascii"> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>
  <xsl:variable name="hex" >0123456789ABCDEF</xsl:variable>

  <xsl:template match="/">

    <xsl:variable name="foo" select="'I have ,001.'"/>

    <result>
      <string>
        <xsl:value-of select="$foo"/>
      </string>
      <hex>
        <xsl:call-template name="recurse-over-string">
          <xsl:with-param name="str" select="$foo"/>
        </xsl:call-template>
      </hex>
    </result>

  </xsl:template>

  <xsl:template name="recurse-over-string">
    <xsl:param name="str"/>   
    <xsl:if test="$str">
      <xsl:variable name="first-char" select="substring($str,1,1)"/>
      <xsl:variable name="ascii-value" select="string-length(substring-before($ascii,$first-char)) + 32"/>
      <xsl:variable name="hex-digit1" select="substring($hex,floor($ascii-value div 16) + 1,1)"/>
      <xsl:variable name="hex-digit2" select="substring($hex,$ascii-value mod 16 + 1,1)"/>
      <xsl:value-of select="concat($hex-digit1,$hex-digit2)"/>
      <xsl:if test="string-length($str) &gt; 1">
        <xsl:text> </xsl:text>
        <xsl:call-template name="recurse-over-string">
          <xsl:with-param name="str" select="substring($str,2)"/>
        </xsl:call-template>
      </xsl:if>
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

这是基本转换中的一个相当简单的练习:

XSLT 1.0

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="utf-8" indent="yes"/>

<xsl:param name="str">496e7465726e616c204d65646963696e65</xsl:param>

<xsl:variable name="hex">0123456789abcdef</xsl:variable>
<xsl:variable name="ascii"> !"#$%&amp;'()*+,-./0123456789:;&lt;=&gt;?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~</xsl:variable>

<xsl:template match="/">
    <acii>
        <xsl:call-template name="hex-to-ascii">
            <xsl:with-param name="str" select="$str"/>
        </xsl:call-template>
    </acii>
</xsl:template>

<xsl:template name="hex-to-ascii">
    <xsl:param name="str"/>   
    <xsl:if test="$str">
        <!-- extract first 2 digits -->
        <xsl:variable name="char1" select="substring($str, 1, 1)"/>
        <xsl:variable name="char2" select="substring($str, 2, 1)"/>
        <!-- get their hex values -->
        <xsl:variable name="val1" select="string-length(substring-before($hex, $char1))"/>
        <xsl:variable name="val2" select="string-length(substring-before($hex, $char2))"/>
        <!-- convert to dec value -->
        <xsl:variable name="dec-value" select="$val1 * 16 + $val2"/>
        <!-- get the corresponding ascii character -->
        <xsl:value-of select="substring($ascii, $dec-value - 31, 1)"/>
        <!-- recursive call with the rest of the hex string -->
        <xsl:call-template name="hex-to-ascii">
            <xsl:with-param name="str" select="substring($str, 3)"/>
        </xsl:call-template>
    </xsl:if>
</xsl:template>

</xsl:stylesheet>

结果

<?xml version="1.0" encoding="utf-8"?>
<acii>Internal Medicine</acii>