gpt4 book ai didi

xslt - 复杂的 XSLT 分割?

转载 作者:行者123 更新时间:2023-12-02 00:26:01 24 4
gpt4 key购买 nike

是否可以按照小写到大写的边界分割标签,即例如,标签“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 - 复杂的 XSLT 分割?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1418884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com