0

我必须以一种特殊的方式转换我的字符串,因为字符串包含许多十六进制值,并且必须根据某些条件应用替换。

我的字符串包含十六进制值。

字符串的长度始终是 18 的倍数。字符串可以包含 1*18 到 5000*18 次(1 组到 5000 组)。

现在我的输出是基于以下条件的转换字符串

1) Check if the groups 13th character is 4e(13th and 14th character)


  Then, change such a way that starting from 7th to 10th character(4 chars) of the string, from whatever the value is to '4040'


    Also change starting from 11th till 16th(6 chars) to '4ef0f0'
    The 17th and 18th to be '4040'

   The whole group to be changed based on the above


2) If the check fails, no change for the group

例如,如果我的输入字符串是 - 一组并且第 12 和第 13 是 '4e'

<xsl:variable name="inputstringhex"
      select="'005f6f7e8f914e7175'"/>

应该改成

'005f6f40404ef0f04040'

我使用的是 xslt1.0,我只能使用分而治之的方法(如果使用任何递归),因为如果使用尾递归方法,我的 xslt 处理器会出错,对于大量。

4

1 回答 1

1

您可以使用二进制日志递归(也许这就是您所说的分而治之的意思?无论如何,这个 XSLT 1.0 样式表......

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*" />

<xsl:variable name="inputstringhex"
      select="'005f6f7e8f914e717512346f7e8f914e7175'"/>

<xsl:template match="/">
  <t>
    <xsl:call-template name="string18s">
      <xsl:with-param name="values" select="$inputstringhex" />
    </xsl:call-template>
  </t>  
</xsl:template>

<xsl:template name="string18s">
  <xsl:param name="values" />
  <xsl:variable name="value-count" select="string-length($values) div 18" />
  <xsl:choose>
    <xsl:when test="$value-count = 0" />
    <xsl:when test="$value-count &gt;= 2">
      <xsl:variable name="half-way" select="floor( $value-count div 2) * 18" />
      <xsl:call-template name="string18s">
        <xsl:with-param name="values" select="substring( $values, 1, $half-way)" />
      </xsl:call-template>
      <xsl:call-template name="string18s">
        <xsl:with-param name="values" select="substring( $values,
         $half-way+1, string-length($values) - $half-way)" />
      </xsl:call-template>      
    </xsl:when>  
    <xsl:otherwise>
      <xsl:call-template name="process-one-string18">
        <xsl:with-param name="value" select="$values" />
      </xsl:call-template>
    </xsl:otherwise>  
  </xsl:choose>
</xsl:template>

<xsl:template name="process-one-string18">
  <xsl:param name="value" />
  <v>
   <xsl:choose>
    <xsl:when test="substring( $value, 13, 2) = '4e'"> 
      <xsl:value-of select="concat( substring( $value, 1, 6),
                                    '40404ef0f04040')" />
    </xsl:when> 
    <xsl:otherwise>
      <xsl:value-of select="$value" />
    </xsl:otherwise> 
   </xsl:choose>
  </v>  
</xsl:template>

</xsl:stylesheet>

...将产生此输出...

<t>
  <v>005f6f40404ef0f04040</v>
  <v>12346f40404ef0f04040</v>
</t> 

根据需要调整或连接输出的值。输入是 $inputstringhex 变量内容。使用此解决方案只能在 13 级递归中处理 5000*18 长度的输入字符串。

于 2012-08-01T15:17:06.637 回答