开发者

Extract the first letter of every word in sentence and make it into one word via XSLT

开发者 https://www.devze.com 2023-01-07 08:44 出处:网络
开发者_开发百科I am trying to extract the first letter of every word for a sentence to form an one word via XSLT.
开发者_开发百科

I am trying to extract the first letter of every word for a sentence to form an one word via XSLT. Sample Input

`ABC HBO ORACLE 123 (Hello Person)` 

Expected Output:

AHO123HP

Thanks in advance :).

P.S. I am also using the XALAN Processor.


This stylesheet:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="text/text()" name="FirstLetterAndNumber">
        <xsl:param name="string" select="concat(normalize-space(translate(.,',.()`','')),' ')"/>
        <xsl:if test="$string != ''">
            <xsl:variable name="word" select="substring-before($string,' ')"/>
            <xsl:choose>
                <xsl:when test="number($word)=number($word)">
                    <xsl:value-of select="$word"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select="substring($word,1,1)"/>
                </xsl:otherwise>
            </xsl:choose>
            <xsl:call-template name="FirstLetterAndNumber">
                <xsl:with-param name="string" select="substring-after($string,' ')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

With this input:

<root>
<text>`ABC HBO ORACLE 123 (Hello Person)`</text>
<text>`ABC HBO ORACLE123 (Hello Person)`</text>
<text>`ABC 123 (Hello Person)`</text>
</root>

Result:

<root>
    <text>AHO123HP</text>
    <text>AHOHP</text>
    <text>A123HP</text>
</root>

Note: If you don't know in advance the special character to strip, you should do:

<xsl:param name="string" 
     select="concat(
               normalize-space(
                 translate(.,
                           translate(.,
                                    ' qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM1234567890',
                                    ''),
                          '')),' ')"/>


The following solution isn't tested in Xalan but Saxon 9B. But it might atleast give you an idea on how to solve it:

Input:

<?xml version="1.0" encoding="UTF-8"?>
<text>ABC HBO ORACLE 123 (Hello Person)</text>

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"
  xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xsl:template match="text">
    <xsl:copy>
      <xsl:variable name="tokens" select="tokenize(.,' ')" as="xs:string+"/>
      <xsl:value-of select="
        for $i in $tokens return 
          if ($i castable as xs:integer) 
            then replace($i, '[^A-z\d]', '')
            else substring(replace($i, '[^A-z\d]', ''), 1, 1)" separator=""/>
    </xsl:copy>
  </xsl:template>
</xsl:stylesheet>

Output:

<?xml version="1.0" encoding="UTF-8"?>
<text>AHO123HP</text>

There is probably a better solution for this but this solves your example case.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号