位操作和字符串消息结构与xslt [英] Bit manipulation and string message structure with xslt

查看:138
本文介绍了位操作和字符串消息结构与xslt的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将包含在变量中的文本字符串转换为XML节点。映射到输出XML由名为'mapping.xml'的文件确定。它有点操作(检查位是否为1)。

  mapping.xml 

< Root> ;
< field no =2charlength =2>变量< / field>
< field no =3total =4>固定< / field>
< field no =21charlength =2>
< subfield no =1total =3> fixed< / subfield>
< subfield no =2charlength =2idcode =ABC> variable< / subfield>
< / field>
< field no =63charlength =2>
< format1>
< subfield no =1total =3> fixed< / subfield>
< / format1>
< format2>
< subfield no =1total =3> fixed< / subfield>
< subfield no =2total =7> fixed< / subfield>
< / format2>
< format3>
< subfield no =1total =3> fixed< / subfield>
< subfield no =2total =7> fixed< / subfield>
< subfield no =3total =6> fixed< / subfield>
< / format3>
< / field>
< / Root>

1)xml将有64个'field'元素。对于示例,我在这里包含3到4个字段,映射xml
2)总是字符串将按照顺序排列,字段1到64以升序排列。
3)这些字段可以是固定的或可变的。固定意味着字符串将由字符数决定,由与字段对应的charlength属性确定
4)某些字段可以有子字段
5)子字段可以有固定的和可变的。所有固定的都会先发生,然后是可变子域。
6)有子字段的字段,固定字段(子字段)总是出现,固定字段不具有idcode属性。
7)具有子字段,可变字段(子字段)的字段以'idcode'属性开头,后面跟着字符长度(由charlength属性确定)
8)子字段ID编码和长度始终发生,然而数据可能存在也可能不存在,具体取决于字符长度(如果由charlength确定的字符是0,那么数据将不存在)
9)字段no 63是一个例外,其中字符长度决定该字段的格式。如果字符是03(字符数2),则它是format1。如果是10,则format2,如果是16,则format3。
9)由位位置确定的字段出现(0表示无出现,1表示发生)
10)位位置由另一个变量保存十六进制值(8字节 - 64位)

我的十六进制可以是:

 < xsl:variable name =hexselect ='6000080000000000'/> <! -  16位数字,每个数字代表4位 - > 
如下所示
0110 0000 0000 0000 0000 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

6 0 0 0 0 8 0 0 0 0 0 0 0 0 0 0

这个十六进制表示字段2,3和21存在,作为那些位置,我们有1 (set)

如果字符串变量是(我的输入)

 < xsl:variable name =inputstringselect ='013112316145ABC0812345678'/> 

上面的字符串16(在1123之后)代表字段21的字符长度。



我所需的输出:

 < Root> 
< field2> 3< / field2>
<! - 当charlength是2(它是01)时值是3,>
< field3> 1123< / field3>
<! - field3值为1123,因为它是固定的,总长度为4 - >
< field21>
< subfield1> 145< / subfield1>
<! - subfield1应该是145,因为它是总共3个字符的固定长度 - >
< subfield2> 12345678< / subfield2>
<! - subfield2以'ABC'开头,长度为08个字符 - >
< / field21>
< / Root>

最后,我们如何将XML节点转换回十六进制字符串?

解决方案

使用以下模板将十六进制转换为十进制:

 < xsl:template name =singleHexToDec> 
< xsl:param name =hex/>
< / xsl:template>

< xsl:template name =hexToDec>
< xsl:param name =hexVal/>
< xsl:param name =decVal/>
< xsl:variable name =hexLengthselect =string-length($ hexVal)/>
< xsl:when test =$ hexLength& gt; 0>
< xsl:variable name =hexPos>
< xsl:call-template name =singleHexToDec>
< xsl:with-param name =hexselect =substring($ hexVal,1,1)/>
< / xsl:call-template>
< / xsl:variable>
< xsl:variable name =addToDec>
< xsl:call-template name =raiseToPower>
< xsl:with-param name =numberselect =16/>
< / xsl:call-template>
< / xsl:variable>
< xsl:call-template name =hexToDec>
< xsl:with-param name =hexValselect =substring($ hexVal,2)/>
< xsl:with-param name =decVal
select =$ decVal +($ addToDec * $ hexPos)/>
< / xsl:call-template>
< / xsl:when>
< xsl:otherwise>
< xsl:value-of select =$ decVal/>
< / xsl:otherwise>
< / xsl:template>

参考资料


I need to convert a text string contained in a variable to an XML node. The mapping to the output XML is determined by a file named 'mapping.xml'. It has bit manipulation(checking if bit is 1).

mapping.xml

<Root>
  <field no="2" charlength="2">variable</field>
  <field no="3" total="4">fixed</field>
  <field no="21" charlength="2">
    <subfield no="1" total="3">fixed</subfield>
    <subfield no="2" charlength="2" idcode="ABC">variable</subfield>
  </field>
  <field no="63" charlength="2">
    <format1>
    <subfield no="1" total="3">fixed</subfield>
    </format1>
    <format2>
      <subfield no="1" total="3">fixed</subfield>
      <subfield no="2" total="7">fixed</subfield>
    </format2>
    <format3>
      <subfield no="1" total="3">fixed</subfield>
      <subfield no="2" total="7">fixed</subfield>
      <subfield no="3" total="6">fixed</subfield>
    </format3>
  </field>
</Root>

1) The xml will have 64 'field' elements. For a sample, i have included 3 to four fields here, in the mapping xml
2) Always the string will follow the order, fields 1 to 64 in ascending.
3) The fields can be fixed or variable. Fixed means the string will be of number of characters, determined by the charlength attribute corresponding to a field
4) Some fields can have subfields
5) The subfields can have fixed and variable. All fixed ones occur first, followed by the variable subfields.
6) A field which has subfields, fixed ones(subfields) always occur, fixed one does not have idcode attributes.
7) A Field which has subfields, variable ones(subfields), start with 'idcode' attribute, followed by the charlength(determined by the charlength attribute)
8) The subfield idcodes and lengths always occur, however the data may or may not be present, depending on char length(if characters determined by charlength is 0, then the data will not be there)
9) Field no 63 is an exception, where the character length determines the format of the field. If the character is 03(number of chars 2), it is format1. If it is 10, format2, then if it is 16, format3.
9) The occurence of fields, determined by bit positions(0 means no occurence, 1 stands for occurence)
10)The bit positions is determined by another variable which holds a hex value(8 bytes - 64 bits)

My hex could be:

<xsl:variable name="hex" select="'6000080000000000'"/><!--16 digits, each digit represents fourbits-->
as illustrated below
0110 0000 0000 0000 0000 1000 0000 0000 0000 0000 0000 0000 0000 0000 0000 0000

6     0    0    0    0    8    0    0    0    0    0    0    0     0    0   0

This hex tells that fields 2, 3 and 21 are present, as those positions, we have 1(set)

If the string variable is (my input)

<xsl:variable name="inputstring" select="'013112316145ABC0812345678'"/>

The above string 16(after 1123), stands for length of chars for field 21.

My desired output:

<Root>
  <field2>3</field2>
  <!--value is 3 as the charlength is 2(which is 01)-->
  <field3>1123</field3>
  <!--field3 value is 1123 as it is fixed, total length of 4-->
  <field21>
    <subfield1>145</subfield1>
    <!--subfield1 should be 145 as it is fixed length of total 3 chars-->
    <subfield2>12345678</subfield2>
    <!--subfield2 starts with 'ABC', has length 08 chars-->
  </field21>
</Root>

Finally, how can we convert the XML node back to the hexadecimal string?

解决方案

Use the following templates to convert hexadecimal to decimal:

<xsl:template name="singleHexToDec">
  <xsl:param name="hex"/>
  <xsl:variable name="table" select="'0123456789ABCDEF'"/>
  <xsl:value-of select="string-length(substring-before($table,$hex))"/>
</xsl:template>

<xsl:template name="hexToDec">
    <xsl:param name="hexVal"/>
    <xsl:param name="decVal"/>
    <xsl:variable name="hexLength" select="string-length($hexVal)"/>
    <xsl:choose>
      <xsl:when test="$hexLength &gt; 0">
        <xsl:variable name="hexPos">
          <xsl:call-template name="singleHexToDec">
            <xsl:with-param name="hex" select="substring($hexVal,1,1)"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:variable name="addToDec">
          <xsl:call-template name="raiseToPower">
            <xsl:with-param name="number" select="16"/>
            <xsl:with-param name="power" select="$hexLength - 1"/>
          </xsl:call-template>
        </xsl:variable>
        <xsl:call-template name="hexToDec">
          <xsl:with-param name="hexVal" select="substring($hexVal,2)"/>
          <xsl:with-param name="decVal" 
     select="$decVal + ($addToDec * $hexPos)"/>
        </xsl:call-template>
      </xsl:when>
      <xsl:otherwise>
        <xsl:value-of select="$decVal"/>
      </xsl:otherwise>
    </xsl:choose>
  </xsl:template>

References

这篇关于位操作和字符串消息结构与xslt的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆