问题描述
限时送ChatGPT账号..我正在打印一条反向字体(黑色背景,白色文本),分布在我的收据带(41 个字符)的宽度上.我有功能代码,但我想知道是否有更好的方法来做到这一点:
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:
这是我现有的代码:
<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>
此代码生成此 XML:
This code results in this XML:
<text lang="en" align="center" smooth="true" reverse="1" xmlns=""> YOU SAVED $2.00 </text>
说明:spaces21 是一个包含 21 个空格的文本字段,如果这还不是很明显的话.
Clarification: spaces21 is a text field with 21 spaces in it, if that wasn't already obvious.
无论我输入的值是多少,xml 都能正确打印($1,275,824.00 和 $2.00 一样有效).
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?
谢谢!
推荐答案
如果您使用 libxslt
或 Xalan
或其他支持 EXSLT 的处理器 str:align() 和 str:padding() 扩展函数,那么你可以简单地做:
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>
<小时>
在纯 XSLT 1.0 中做,我相信这样会更优雅:
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>
这篇关于XSLT 1.0 Kludge 清理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论