复杂的 XSLT 拆分? [英] Complex XSLT split?

查看:20
本文介绍了复杂的 XSLT 拆分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在小写到大写边界处拆分标签,即例如,标签UserLicenseCode"应转换为User License Code"使列标题看起来更好一些.

Is it possible to split a tag at lower to upper case boundaries i.e. for example, tag 'UserLicenseCode' should be converted to 'User License Code' so that the column headers look a little nicer.

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

I've done something like this in the past using Perl's regular expressions, but XSLT is a whole new ball game for me.

任何创建此类模板的指针将不胜感激!

Any pointers in creating such a template would be greatly appreciated!

谢谢克里希纳

推荐答案

使用递归,可以遍历 XSLT 中的字符串来计算每个字符.为此,请创建一个仅接受一个字符串参数的新模板.检查第一个字符,如果它是大写字符,则写一个空格.然后写人物.然后使用单个字符串中的剩余字符再次调用模板.这将导致您想要做的事情.

Using recursion, it is possible to walk through a string in XSLT to evaluate every character. To do this, create a new template which accepts only one string parameter. Check the first character and if it's an uppercase character, write a space. Then write the character. Then call the template again with the remaining characters inside a single string. This would result in what you want to do.

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


需要进行一些测试,尤其是要在整个事物中留出空间.(我为此误用了一个字符!)但是这段代码应该给你一个想法......

That would be your pointer. I will need some time to work out the template. :-)


It took some testing, especially to get the space inside the whole thing. (I misused a character for this!) But this code should give you an idea...

我使用了这个 XML:

I used this 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 中剥离,这就是我使用*"的原因,我将其转换为空格.

Do keep in mind that spaces and other white-space characters do tend to be stripped away from XML, which is why I used an '*' instead, which I translated to a space.

当然,这段代码可以改进.这是我可以在 10 分钟内完成的工作.在其他语言中,它需要更少的代码行,但在 XSLT 中,考虑到它包含的代码行数,它仍然相当快.

Of course, this code could be improved. It's what I could come up with in 10 minutes of work. In other languages, it would take less lines of code but in XSLT it's still quite fast, considering the amount of code lines it contains.

这篇关于复杂的 XSLT 拆分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆