是否可以将标签拆分为较低至大写的边界,即 例如,标签'UserLicenseCode'应转换为'用户许可代码' 这样列标题看起来更好一些。

过去我使用Perl的正则表达式做过类似的事情, 但XSLT对我来说是一个全新的球赛。

非常感谢创建这样一个模板的任何指示!

由于 克里希纳

有帮助吗?

解决方案

使用递归,可以在XSLT中遍历字符串以评估每个字符。为此,请创建一个只接受一个字符串参数的新模板。检查第一个字符,如果它是一个大写字符,请写一个空格。然后写下角色。然后再次使用单个字符串中的剩余字符调用模板。这将导致您想要做的事情。

那将是你的指针。我需要一些时间来制定模板。 :-)


它花了一些测试,特别是为了获得整个空间。 (我误用了一个字符!)但是这段代码应该给你一个想法...

我使用了这个XML:

<?xml version="1.0" encoding="UTF-8"?>
<blah>UserLicenseCode</blah>

然后是这个样式表:

<?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>

我得到了这个结果:

User License Code

请记住,空格和其他空格字符确实会从XML中删除,这就是我使用'*'代替的原因,我将其转换为空格。

当然,这段代码可以改进。这是我在10分钟的工作中想出来的。在其他语言中,它需要更少的代码行,但在XSLT中,考虑到它包含的代码行数量,它仍然非常快。

其他提示

XSLT + FXSL解决方案(在XSLT 2.0中,但几乎相同的代码适用于XSLT 1.0和FXSL 1.x

<xsl:stylesheet version="2.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:f="http://fxsl.sf.net/"
xmlns:testmap="testmap"
exclude-result-prefixes="f testmap"
>
   <xsl:import href="../f/func-str-dvc-map.xsl"/>
   <testmap:testmap/>

   <xsl:output omit-xml-declaration="yes" indent="yes"/>

   <xsl:template match="/">
     <xsl:variable name="vTestMap" select="document('')/*/testmap:*[1]"/>

     '<xsl:value-of select="f:str-map($vTestMap, 'UserLicenseCode')"
       />'
   </xsl:template>

    <xsl:template name="mySplit" match="*[namespace-uri() = 'testmap']"
     mode="f:FXSL">
      <xsl:param name="arg1"/>

      <xsl:value-of select=
       "if(lower-case($arg1) ne $arg1)
         then concat(' ', $arg1)
         else $arg1
       "/>
    </xsl:template>
</xsl:stylesheet>

当对任何源XML文档(未使用)应用上述转换时,会产生预期的正确结果:

'用户许可代码'

请注意

  1. 我们正在使用FXC功能/模板的DVC版本 str-map() 。这是一个高阶函数(HOF),它有两个参数:另一个函数和一个字符串。 str-map() 在字符串的每个字符上应用该函数,并返回结果的串联。

  2. 因为使用了小写()函数(在XSLT 2.0版本中),所以我们不仅限于拉丁字母。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top