如何在 XSLT 中为句子中每个字符串的第一个字母创建链接



假设我有一个变量 <wish>Hi jony</wish>我必须遍历 wish 元素,并且必须为元素中字符串的第一个字母创建链接。输出应该<a href="#H">H</a> <a href="#j">j</a>.

我认为这就是你要做的:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
    <xsl:output method="xml" encoding="utf-8" indent="yes"/>
    <xsl:template match="*">
        <xsl:copy>
            <xsl:apply-templates/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="wish">
        <xsl:copy>
            <xsl:call-template name="link-first-letters">
                <xsl:with-param name="text" select="."/>
            </xsl:call-template>            
        </xsl:copy>
    </xsl:template>
    <xsl:template name="link-first-letters">
        <xsl:param name="text"/>
        <xsl:variable name="first-letter" select="substring($text, 1, 1)"/>
        <a href="#{$first-letter}"><xsl:value-of select="$first-letter"/></a>

        <xsl:if test="contains($text, ' ')">
            <xsl:text> </xsl:text>
            <xsl:call-template name="link-first-letters">
                <xsl:with-param name="text" select="substring-after($text, ' ')"/>
            </xsl:call-template>
        </xsl:if>
    </xsl:template>
</xsl:stylesheet>

应用于此输入文档

<?xml version="1.0"?>
<root>
    <wish>Hi jony</wish>
</root>

它产生以下输出:

<root>
    <wish><a href="#H">H</a> <a href="#j">j</a></wish>
</root>

您需要一个递归模板来拆分字符串,然后构建元素。以下是您可以应用的模板:

  <xsl:template match="Wish">
    <xsl:call-template name="links">
      <xsl:with-param name="text" select="." />
    </xsl:call-template>
  </xsl:template>
  <xsl:template name="links">
    <xsl:param name="text" />
    <xsl:variable name="newtext" select="concat(normalize-space($text), ' ')" />
    <xsl:variable name="first" select="substring-before($newtext, ' ')" />
    <xsl:variable name="remaining" select="normalize-space(substring-after($newtext, ' '))" />
    <xsl:element name="a">
      <xsl:attribute name="href">#<xsl:value-of select="substring($first, 1, 1)"/></xsl:attribute>
      <xsl:value-of select="substring($first, 1, 1)"/>
    </xsl:element>
    <xsl:if test="$remaining != ''">
      <xsl:call-template name="links">
        <xsl:with-param name="text" select="$remaining" />
      </xsl:call-template>
    </xsl:if>
  </xsl:template>  

你走在正确的轨道上。清理和简化后,它看起来像这样:

<xsl:for-each select="tokenize($keyword,' ')">
    <xsl:variable name="letter" select="substring(.,1,1)"/>
    <a href="{concat('#',$letter)}">
        <xsl:value-of select="$letter"/>
    </a>
</xsl:for-each>

根据您对tokenize的使用,我假设您使用的是XSLT 2.0,它消除了对harpo递归解决方案的需求。

此 XSLT 2.0 转换

<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output omit-xml-declaration="yes" indent="yes"/>
 <xsl:template match="wish">
     <xsl:for-each select="tokenize(., 'W+')">
      <xsl:variable name="vFirst" select="substring(.,1,1)"/>
      <a href="#{$vFirst}"><xsl:value-of select="$vFirst"/></a>
     </xsl:for-each>
 </xsl:template>
</xsl:stylesheet>

应用于提供的 XML 文档时:

<wish>Hi jony</wish>

产生所需的结果

<a href="#H">H</a>
<a href="#j">j</a>

请注意:无需使用concat()

最新更新