Pad Value with fix length - xslt

浪尽此生 提交于 2019-12-30 12:23:02

问题


I have this simple xslt code:

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:output method="text" indent="yes"/>
   <xsl:template match="/racine/Requete">
        <xsl:for-each select="Row">
            <xsl:value-of select="Col[@name = 'Service Point']/." />
            <xsl:text>ME02</xsl:text>
            <xsl:value-of select="Col[@name = 'Meter Number']/." />
            <xsl:value-of select="Col[@name = 'Date']/." />
            <xsl:value-of select="translate(Col[@name = 'Import Active kWh']/.,',','.')" />
            <xsl:text>30000000000000000000</xsl:text>
            <xsl:text>&#13;</xsl:text>
        </xsl:for-each>
    </xsl:template>
</xsl:stylesheet>

The Meter Number parameter can have max 10 alphanumeric. so if for example the value is 232345, I want to display (pad with spaces):' 232345'. I have the same case for Import Active kWh, this is a numeric field fix 12 type numeric, but if the value is 56884, I want to display (pad with 0 at the begining) '000000056884'.

Thanks for your useful help as usual!

Regards.


回答1:


For the numeric field you can simply use the format-number() function, e.g.:

<xsl:value-of select="format-number($numeric-field, '000000000000')"/>

For the alpha-numeric field, try:

<xsl:value-of select="substring(concat('          ', $alphanumeric-field), 1 + string-length($alphanumeric-field))" />

If you need to do it more than once, consider re-writing this as a function.


Note: this location step /. doesn't do anything.




回答2:


XSLT has format-number function so format-number(translate(Col[@name = 'Import Active kWh']/.,',','.'), '000000000000') should solve one problem.



来源:https://stackoverflow.com/questions/31805587/pad-value-with-fix-length-xslt

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