【发布时间】:2010-07-01 16:39:13
【问题描述】:
我需要获取一些 XML 并将其转换为固定宽度的加载文件,以便加载到 SAP 系统。我的算法工作正常,除了一些奇怪的欧洲字符,例如 Ã,在字符串中为每个字符实例返回 +1 的字符串长度。因此,例如文本 Ãbcd 的字符串长度($value)将是 5 而不是 4。
这是一个问题,因为我的代码检查属性的长度是多少,然后从固定长度输出格式的最大长度中减去它(即对于 30 宽度的字段,如果它读取 Ãbcd 它会认为它需要 25 个空格而不是 26 个)。
有谁知道更好的方法来做到这一点,或者我在算法中做错了什么?
以下是我的 xsl 模板(大多数情况下......无法将它们完全正确地放在这里......)
写出属性的模板:
<xsl:param name="value"/>
<xsl:param name="width"/>
<!-- find the current length of the field-->
<xsl:variable name="valueWidth" select="string-length($value)" />
<xsl:variable name="difference" select="$width - $valueWidth" />
<xsl:if test="$difference > 0">
<xsl:value-of select="$value"/>
<!-- run this for loop x times outputing space for each -->
<xsl:call-template name="for-loop-spaces">
<xsl:with-param name="count" select="$difference - 1" />
</xsl:call-template>
</xsl:if>
<xsl:if test="($difference < 0)">
<xsl:value-of select="substring($value,0,$width)"/>
</xsl:if>
<xsl:if test="$difference = 0">
<xsl:value-of select="$value"/>
</xsl:if>
</xsl:template>
For-loop-spaces 模板(它不会复制粘贴): 每次调用时输出一个空格。接受参数“计数”。如果 count 大于零,递归调用 count-1 直到 0。
任何输入都会非常有用:)
【问题讨论】:
标签: xml xslt character-encoding