2009-10-14 45 views

Trả lời

6

Đây là một câu trả lời trực tiếp hơn cho câu hỏi này. Câu trả lời của bastianneu chắc chắn đặt bạn vào đúng hướng, nhưng nếu bạn muốn một mẫu cụ thể phá vỡ các chuỗi CamelCase thành các từ riêng lẻ, điều này sẽ làm điều đó cho bạn.

<xsl:template name="breakIntoWords"> 
    <xsl:param name="string" /> 
    <xsl:choose> 
    <xsl:when test="string-length($string) &lt; 2"> 
     <xsl:value-of select="$string" /> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:call-template name="breakIntoWordsHelper"> 
     <xsl:with-param name="string" select="$string" /> 
     <xsl:with-param name="token" select="substring($string, 1, 1)" /> 
     </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 

<xsl:template name="breakIntoWordsHelper"> 
    <xsl:param name="string" select="''" /> 
    <xsl:param name="token" select="''" /> 
    <xsl:choose> 
    <xsl:when test="string-length($string) = 0" /> 
    <xsl:when test="string-length($token) = 0" /> 
    <xsl:when test="string-length($string) = string-length($token)"> 
     <xsl:value-of select="$token" /> 
    </xsl:when> 
    <xsl:when test="contains('ABCDEFGHIJKLMNOPQRSTUVWXYZ',substring($string, string-length($token) + 1, 1))"> 
     <xsl:value-of select="concat($token, ' ')" /> 
     <xsl:call-template name="breakIntoWordsHelper"> 
     <xsl:with-param name="string" select="substring-after($string, $token)" /> 
     <xsl:with-param name="token" select="substring($string, string-length($token), 1)" /> 
     </xsl:call-template> 
    </xsl:when> 
    <xsl:otherwise> 
     <xsl:call-template name="breakIntoWordsHelper"> 
     <xsl:with-param name="string" select="$string" /> 
     <xsl:with-param name="token" select="substring($string, 1, string-length($token) + 1)" /> 
     </xsl:call-template> 
    </xsl:otherwise> 
    </xsl:choose> 
</xsl:template> 
Các vấn đề liên quan