是否可以在较低到大写的边界处拆分标签,例如,标签"UserLicenseCode"应转换为"用户许可证代码",以便列标题看起来更好一些.
我过去使用Perl的正则表达式做了类似的事情,但XSLT对我来说是一个全新的球类游戏.
任何创建这样的模板的指针将不胜感激!
谢谢克里希纳
使用递归,可以遍历 XSLT 中的字符串来评估每个字符。为此,请创建一个仅接受一个字符串参数的新模板。检查第一个字符,如果是大写字符,则写一个空格。然后写出人物。然后使用单个字符串中的剩余字符再次调用模板。这将导致您想做的事情。
那将是你的指针。我需要一些时间来制定模板。:-)
我使用了这个 XML:
<?xml version="1.0" encoding="UTF-8"?>
<blah>UserLicenseCode</blah>
Run Code Online (Sandbox Code Playgroud)
然后是这个样式表:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:output method="text"/>
<xsl:variable name="Space">*</xsl:variable>
<xsl:template match="blah">
<xsl:variable name="Split">
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="."/>
<xsl:with-param name="First" select="true()"/>
</xsl:call-template></xsl:variable>
<xsl:value-of select="translate($Split, '*', ' ')" />
</xsl:template>
<xsl:template name="Split">
<xsl:param name="Value"/>
<xsl:param name="First" select="false()"/>
<xsl:if test="$Value!=''">
<xsl:variable name="FirstChar" select="substring($Value, 1, 1)"/>
<xsl:variable name="Rest" select="substring-after($Value, $FirstChar)"/>
<xsl:if test="not($First)">
<xsl:if test="translate($FirstChar, 'ABCDEFGHIJKLMNOPQRSTUVWXYZ', '..........................')= '.'">
<xsl:value-of select="$Space"/>
</xsl:if>
</xsl:if>
<xsl:value-of select="$FirstChar"/>
<xsl:call-template name="Split">
<xsl:with-param name="Value" select="$Rest"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Run Code Online (Sandbox Code Playgroud)
我得到了这个结果:
User License Code
Run Code Online (Sandbox Code Playgroud)
请记住,空格和其他空白字符往往会从 XML 中删除,这就是为什么我使用“*”来代替,并将其翻译为空格。
当然,这段代码还可以改进。这是我在 10 分钟内就能想出的东西。在其他语言中,它需要的代码行更少,但在 XSLT 中,考虑到它包含的代码行数量,它仍然相当快。