2009-09-29 5 views
3

나는 변수XSLT 1.0 : 새로운 라인 문자를 대체 _

<xsl:variable name="testvar"> 
     d 
     e 
     d 
    </xsl:variable> 

미만이 데 나는이 기능이 있습니다

<xsl:choose> 
     <xsl:when test="not($str-input)"> 
      <func:result select="false()"/> 
     </xsl:when> 
     <xsl:otherwise> 
      <func:result select="translate($str-input,$new-line,'_')"/> 
     </xsl:otherwise> 
    </xsl:choose> 
</func:function> 

을 그리고 난 내가 본 기능을 테스트 할 때 내 결과는 다음과 같습니다. _ d_e _ d_ 내 결과 만

D _ 전자 _ D

답변

3

:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 

    <xsl:variable name="new-line" select="'&#10;'" /> 

    <xsl:variable name="str-input"> 
     d 
     e 
     d 
    </xsl:variable> 

    <!-- your <xsl:choose>, slightly modified -->  
    <xsl:template match="/"> 
    <xsl:choose> 
     <xsl:when test="not($str-input)"> 
     <xsl:value-of select="false()"/> 
     </xsl:when> 
     <xsl:otherwise> 
     <xsl:variable name="temp"> 
      <xsl:call-template name="normalize-newline"> 
      <xsl:with-param name="str" select="$str-input" /> 
      </xsl:call-template> 
     </xsl:variable> 
     <xsl:value-of select="translate($temp, $new-line, '_')" /> 
     </xsl:otherwise> 
    </xsl:choose> 

    </xsl:template> 

    <!-- a template that trims leading and trailing newlines -->  
    <xsl:template name="normalize-newline"> 
    <xsl:param name="str" select="''" /> 

    <xsl:variable name="temp" select="concat($str, $new-line)" /> 
    <xsl:variable name="head" select="substring-before($temp, $new-line)" /> 
    <xsl:variable name="tail" select="substring-after($temp, $new-line)" /> 
    <xsl:variable name="hasHead" select="translate(normalize-space($head), ' ', '') != ''" /> 
    <xsl:variable name="hasTail" select="translate(normalize-space($tail), ' ', '') != ''" /> 

    <xsl:if test="$hasHead"> 
     <xsl:value-of select="$head" /> 
     <xsl:if test="$hasTail"> 
     <xsl:value-of select="$new-line" /> 
     </xsl:if> 
    </xsl:if> 
    <xsl:if test="$hasTail"> 
     <xsl:call-template name="normalize-newline"> 
     <xsl:with-param name="str" select="$tail" /> 
     </xsl:call-template> 
    </xsl:if> 
    </xsl:template> 

</xsl:stylesheet> 

복귀 :

"  d _  e _  d" 

공백 변수 값 부분이다. normalize-space()을 사용하여 제거 할 수는 있지만 실제로는 "d" 또는 "e"이 무엇인지 모르므로 변경하지 않습니다.

+0

@Tomalak 질문이 하나 있습니다. 캐리지 리턴 "\ r"은 어떨까요? d \ r \ n 또는 d \ n \ r e \ r \ n 또는 e \ n 뒤에 캐리지 리턴 (\ r)이 있는지 확실하지 않습니다. \\ d \ r \ n 또는 d \ n \ r

+0

'' 당신의 가치관에 나타나기를 기대합니다. 예를 들어'select = " ' ' ''도 괜찮습니다. – Tomalak

+0

또는 translate ($ str, ' ', '')를 사용하여 ' '을 제거하는 것과 같은 작업을 수행 할 수 있습니다. – Tomalak

0

당신이 당신의 변수를 변경할 수 있습니다

<xsl:variable name="testvar"> 
     d 
     e 
     d</xsl:variable> 

를? XSLT 1.0

+0

변경할 수는 있지만 기능이 충분히 유연하지 않습니다. 또한 변수의 내용은 미리 할당되지 않은 XML 파일에서 읽혀집니다. –