提取句子中每个单词的第一个字母,并通过 XSLT 将其变成一个单词

发布于 2024-09-09 11:53:38 字数 211 浏览 3 评论 0原文

我试图通过 XSLT 提取句子中每个单词的第一个字母以形成一个单词。 示例输入

`ABC HBO ORACLE 123 (Hello Person)` 

预期输出:

AHO123HP

提前致谢:)。

PS 我也使用 XALAN 处理器。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

甜心小果奶 2024-09-16 11:53:38

此样式表:

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

使用此输入:

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

结果:

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

注意:如果您事先不知道要删除的特殊字符,您应该这样做:

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

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',
                                    ''),
                          '')),' ')"/>
东京女 2024-09-16 11:53:38

以下解决方案未在 Xalan 中测试,而是在 Saxon 9B 中测试。但它至少可以让您了解如何解决它:

输入:

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

输出:

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

对于这个问题可能有更好的解决方案,但是这解决了您的示例案例。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文