XSLT 1.0 Kludge cleanup

一个人想着一个人 提交于 2021-02-17 05:46:31

问题


I am printing a line in reverse font (black background, white text), spread over the width of my receipt tape (41 characters). I have functional code, but I am wondering if there is a better way to do this:

Here's my existing code:

<text lang="en" align="center" smooth="true" reverse="1"><xsl:value-of select="substring($spaces21, 1, (string-length($spaces21) - ((string-length(SavingsSegment) div 2) + string-length(SavingsSegment) mod 2)))"/>
  <xsl:value-of select="SavingsSegment"/><xsl:value-of select="substring($spaces21, 1, (string-length($spaces21) - ((string-length(SavingsSegment) div 2) + string-length(SavingsSegment) mod 2)))"/>
</text>

This code results in this XML:

 <text lang="en" align="center" smooth="true" reverse="1" xmlns="">             YOU SAVED $2.00             </text>

Clarification: spaces21 is a text field with 21 spaces in it, if that wasn't already obvious.

The xml prints correctly, no matter the value I put in ($1,275,824.00 worked just as well as $2.00).

My problem is that this is the kludgiest kludge that ever did kludge. Is there a way to do this in a cleaner manner?

Thanks!


回答1:


If you're using libxslt or Xalan or another processor that supports the EXSLT str:align() and str:padding() extension functions, then you could do simply:

<text>
    <xsl:value-of select="str:align(SavingsSegment, str:padding(41, ' '), 'center')"/>
</text> 

To do it in pure XSLT 1.0, I believe this would be more elegant:

<xsl:variable name="spaces21" select="'                     '"/>
<xsl:variable name="pad" select="(41 - string-length(SavingsSegment)) div 2"/>
<text>
    <xsl:value-of select="substring($spaces21, 1, floor($pad))"/>
    <xsl:value-of select="SavingsSegment"/>
    <xsl:value-of select="substring($spaces21, 1, ceiling($pad))"/>
</text> 


来源:https://stackoverflow.com/questions/54487461/xslt-1-0-kludge-cleanup

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