复杂的XSLT分裂?(Complex XSLT split?)

2019-08-05 20:21发布

是否有可能在较低的为大写的边界来分割标签即例如,标签“UserLicenseCode”,使得列标题看起来更好一点应该被转换到“用户许可代码”。

我已经做了在使用Perl的正则表达式,过去这样的事情,但XSLT是一个全新的球赛我。

在建立这样一个模板任何指针将不胜感激!

感谢克里希纳

Answer 1:

使用递归,可以通过在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它仍然是相当快的,考虑到它包含的代码线的量。



Answer 2:

一个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. 我们使用FXSL函数/模板的DVC版本str-map() 这是一个高阶函数(HOF)采用两个参数:另外一个函数和一个字符串。 str-map()适用于字符串的每一个字符的功能并返回结果的串联。

  2. 因为小写()函数是用来(在XSLT版本2.0),我们就不会被限制仅仅拉丁字母。



文章来源: Complex XSLT split?
标签: xslt split word