2012-08-01 28 views
0

我必須以特殊方式轉換我的字符串,因爲字符串包含許多十六進制值,並且必須根據某些條件應用替換。對十六進制字符串進行xslt轉換

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

總是字符串的長度將是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處理器錯誤,如果使用尾遞歸方法,大數。

+0

如果你使用二進制日誌遞歸而不是線性遞歸,你應該沒問題。 – 2012-08-01 14:50:01

回答

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變量的內容。A 5000 * 18長度的輸入字符串可以在僅13個級別使用該溶液的遞歸處理。

相關問題