How padding char using xsl

别来无恙 提交于 2021-02-17 05:21:30

问题


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 data using XSL.

In XML:
<emp>
<name>Test</name>
</emp>

Expected Output : 
<emp>
<name>Test******</name>
</emp>

Please let me know if anyone have any solution.

Thanks in advance. 

回答1:


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.




回答2:


**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***




回答3:


<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**



来源:https://stackoverflow.com/questions/31289466/how-padding-char-using-xsl

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!