How padding char using xsl

前端 未结 3 1952
萌比男神i
萌比男神i 2021-01-27 10:42

I want padding the data with extra space(any char) using XSL version 1.0.

Name field max chars length is 10 (length must be dyanamic) chars.

Need to transfer da         


        
相关标签:
3条回答
  • 2021-01-27 11:43
    <xsl:function name="functx:pad-string-to-length" as="xs:string"
                  xmlns:functx="http://www.functx.com">
      <xsl:param name="stringToPad" as="xs:string?"/>
      <xsl:param name="padChar" as="xs:string"/>
      <xsl:param name="length" as="xs:integer"/>
    
      <xsl:sequence select="
       substring(
         string-join (
           ($stringToPad, for $i in (1 to $length) return $padChar)
           ,'')
        ,1,$length)
     "/>
    
    </xsl:function>
    

    For example : functx:pad-string-to-length('Ans', '*', 5) Output : Ans**

    0 讨论(0)
  • 2021-01-27 11:45
    **Input.xml**
    <?xml version="1.0"?>
    <Data>
    <text>1234567</text>
    </Data>
    
    **StringPadding.xsl**
    
    <?xml version="1.0"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="text" />
    <xsl:template match="/Data"><xsl:variable name="new-str" select="//Data/text"></xsl:variable>
    After Padding= <xsl:call-template name="str-pad"><xsl:with-param name="string" select="$new-str"/><xsl:with-param name="str-length" select="10" /></xsl:call-template>
    </xsl:template>
    
    <xsl:template name="str-pad">
    <xsl:param name="string" />
    <xsl:param name="pad-char" select="'*'"/>
    <xsl:param name="str-length" />
    <xsl:value-of select="'  '"/>
    <xsl:choose>
    <xsl:when test="string-length($string) = $str-length">
    <xsl:value-of select="$string" />
    </xsl:when>
    <xsl:otherwise>
    <xsl:call-template name="str-pad">
    <xsl:with-param name="string" select="concat($string,$pad-char)" />
    <xsl:with-param name="pad-char" select="$pad-char" />
    <xsl:with-param name="str-length" select="$str-length" />
    </xsl:call-template>
    </xsl:otherwise>
    </xsl:choose>
    </xsl:template>
    </xsl:stylesheet>
    

    Output : After Padding = 1234567***

    0 讨论(0)
  • 2021-01-27 11:48

    Try:

    substring(concat($yourstring, '**********'), 1, 10)
    

    Example using your input:

    XSLT 1.0

    <xsl:stylesheet version="1.0" 
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:strip-space elements="*"/>
    
    <!-- identity transform -->
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="name">
        <xsl:copy>
            <xsl:value-of select="substring(concat(., '**********'), 1, 10)"/>
        </xsl:copy>
    </xsl:template>
    
    </xsl:stylesheet>
    

    Result

    <?xml version="1.0" encoding="UTF-8"?>
    <emp>
       <name>Test******</name>
    </emp>
    

    Alternatively, if your processor supports it, you could use the EXSLT str:align() function - possibly in conjunction with the str:padding() function to create the padding string dynamically.

    0 讨论(0)
提交回复
热议问题