【问题标题】:String join using xquery使用 xquery 进行字符串连接
【发布时间】:2018-01-03 23:38:07
【问题描述】:

我们需要将十六进制转换为二进制。经过很好的研究,我们能够实现它,但问题是输出在每组二进制元素之间都有空格。

如何去除这些空格?

以下代码给出的输出为: 0111 0110 0010 0100

必填: 0111011000100100


xquery version "1.0" encoding "utf-8"
(:: OracleAnnotationVersion "1.0" ::)
declare namespace ns1="http://www.s2mpos.com/v1";
(:: import schema at "s2miso8583.xsd" ::)
declare namespace bin="http://expath.org/spec/binary";
declare variable $ReadLine as xs:string external;
declare function local:charsReturn($arg as xs:string) as xs:string*
{
  for $ch in string-to-codepoints($arg)
  return codepoints-to-string($ch)
};

declare function local:convertString($argx as xs:string) as xs:string*
{ 
  let $x := local:charsReturn($ReadLine)
       for $a in $x
       return
         string-join(
          for $b in (8,4,2,1)
          let $cv := xs:integer($a)
          let $xm := $cv mod ($b*2)
          return
            if ( $xm >= $b ) then "1" else "0"
        ,"")
};

declare function local:func($ReadLine as xs:string) as element() (:: schema-element(ns1:s2mMessages) ::) {
  <ns1:s2mMessages>
    {
      let $abc := local:convertString($ReadLine)
    } 
  </ns1:s2mMessages>
};

local:func($ReadLine)

【问题讨论】:

    标签: xml xquery


    【解决方案1】:

    我看不出您的代码是如何工作的。如果$Readline 包含十六进制数字,则$a 可以是任何十六进制数字,例如“C”,如果$a 是“C”,则xs:integer($a) 将失败并显示FORG0001。

    我愿意:

    string-join(
      let $hex := ('0', '1', '2', '3', ... 'E', 'F')
      let $bin := ('0001', '0010', '0011', '0100', ...., '1110', '1111')
      for $a in local:charsReturn($ReadLine)
      return $bin[index-of($hex, $a)], "")
    

    【讨论】:

    • 非常感谢 Michael 展示了一种非常简单的方法来实现这一点。希望有二进制到六进制转换的功能。非常感谢您的快速回复。欣赏它。
    • EXPath 二进制模块 (expath.org/spec/binary) 中的函数可用于许多处理器,但不是全部。但是没有函数可以将二进制值输出为“0”和“1”字符的字符串;我想没有人想出这种特殊表示的用例!
    猜你喜欢
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-20
    • 2023-03-02
    • 2011-12-23
    • 2020-03-29
    • 2018-08-16
    相关资源
    最近更新 更多