2012-08-17 48 views
0

我需要將包含在變量中的文本字符串轉換爲XML節點。映射到輸出XML由名爲'mapping.xml'的文件確定。它有點操作(檢查位是否爲1)。位操作和字符串消息結構與xslt

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) 

我的十六進制可以是:

<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 

此六角告訴字段2,3和21都存在,如那些位置,我們有1(組)

如果字符串變量是(我的輸入)

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

上面的字符串16(後1123),代表字符的長度字段21

我想要的輸出:

<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> 

最後,我們如何轉換XML節點回十六進制字符串?

+1

蘇雷什,我強烈建議你分裂這個問題用一系列小的,簡單的問題,每一個都可以單獨解決 - 讓這些爲獨立的問題。在目前的形式下,這個問題需要非常多的工作量(小時),這對包括我在內的潛在求解者沒有吸引力。例如,將第一個問題限制爲:從十六進制字符串中獲取字段序列。請。 – 2012-08-17 12:05:07

+1

阿門to Dimitre的評論。另外,您嘗試過哪些XSLT,結果如何? – LarsH 2012-08-17 14:09:26

回答

0

使用下面的模板來轉換十六進制轉換爲十進制:

<xsl:template name="singleHexToDec"> 
    <xsl:param name="hex"/> 
    <xsl:variable name="table" select="'ABCDEF'"/> 
    <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> 

參考