【问题标题】:XSLT: Append spaces in "long" wordXSLT:在“长”字中附加空格
【发布时间】:2010-03-31 11:11:52
【问题描述】:

有一个代码:

<p>
    Lorem ipsum dolor sit ametconsecteturadipisicing elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Ut enim ad minim veniam, quis nostrud exercitation ullamco 
    laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute iruredolorinreprehenderit in voluptate 
    velit esse cillum doloreeufugiatnullapariatur. Excepteur sint 
    occaecat cupidatat non proident, sunt in culpa qui officia 
    deserunt mollit anim id est laborum.
</p>

需要接收:

<p>
    Lorem ipsum dolor sit <span class="spaced">a m e t c o n s e c t e t u r a d i p i s i c i n g</span> elit, 
    sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 
    Ut enim ad minim veniam, quis nostrud exercitation ullamco 
    laboris nisi ut aliquip ex ea commodo consequat. 
    Duis aute <span class="spaced">i r u r e d o l o r i n r e p r e h e n d e r i t</span> in voluptate 
    velit esse cillum <span class="spaced">d o l o r e e u f u g i a t n u l l a p a r i a t u r</span>. Excepteur sint 
    occaecat cupidatat non proident, sunt in culpa qui officia 
    deserunt mollit anim id est laborum.
</p>

意义在于将“长”字用空格分开。在此类单词中的每个字母后放置空格。那么就需要结束这个词在一个带有“spaced”类的标签中。

如果该单词中的字母数量大于 10,则该单词被认为是“长”的(例如,可以设置任何值)。

如何解决这个问题意味着xslt?

【问题讨论】:

    标签: xslt word spaces divide


    【解决方案1】:

    这是一个 XSLT 2.0 样式表,您可以使用 Saxon 9 或 AltovaXML tools 运行它:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xsd"
      version="2.0">
    
      <xsl:param name="l" as="xsd:integer" select="10"/>
      <xsl:variable name="regex1" as="xsd:string" select="concat('\w{', $l, ',}')"/>
    
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@*, node()"/>
        </xsl:copy>
      </xsl:template>
    
      <xsl:template match="p/text()">
        <xsl:analyze-string select="." regex="{$regex1}">
          <xsl:matching-substring>
            <span class="space">
              <xsl:value-of select="for $ c in string-to-codepoints(.) return codepoints-to-string($c)"
                            separator=" "/>
            </span>
          </xsl:matching-substring>
          <xsl:non-matching-substring>
            <xsl:value-of select="."/>
          </xsl:non-matching-substring>
        </xsl:analyze-string>
      </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

    • @Martin Honnen,此代码仅适用于支持 XSLT 2.0 的处理器? XSLT 1.0 有什么解决方案吗?我们的服务器上没有 XSLT 2.0。
    • 您确实需要一个 XSLT 2.0 处理器来运行该样式表。至于纯粹的 XSLT 1.0 解决方案,我没有。正如您所见,XSLT 2.0 解决方案使用了纯 XSLT 1.0 根本不支持的正则表达式。如果你真的想用 XSLT 1.0 解决这个问题,那么我至少会检查你的 XSLT 1.0 处理器是否支持正则表达式作为扩展。
    • libxslt 版本 1.1.17,libxslt 针对 libxml 版本 2.6.26 编译,EXSLT - 启用,libexslt 版本 - 1.1.17。这是我们在服务器上的 xslt 设置。
    【解决方案2】:

    非常聪明的人促使我做出了这个决定,非常感谢他们。 在 xslt 1.0 中工作,没有任何扩展。这不是我的解决方案。

    <?xml version="1.0" encoding="UTF-8"?>
    
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
            xmlns="http://www.w3.org/1999/xhtml">
            <xsl:output method="xml" media-type="text/xhtml" version="1" doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd" doctype-public="-//W3C//DTD XHTML 1.0 Strict//EN" indent="yes" encoding="UTF-8" omit-xml-declaration="yes"/>
    
        <xsl:template match="p">
          <p>
            <xsl:call-template name="tokens">
              <xsl:with-param name="str" select="text()" />
            </xsl:call-template>
          </p>
        </xsl:template>  
    
        <xsl:template name="tokens">
          <xsl:param name="str" />
          <xsl:choose>
            <xsl:when test="contains($str, ' ')">
              <xsl:call-template name="checklength">
                <xsl:with-param name="str" select="substring-before($str, ' ')" />
              </xsl:call-template>
              <xsl:call-template name="tokens">
                <xsl:with-param name="str" select="substring-after($str, ' ')"/>
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:call-template name="checklength">
                <xsl:with-param name="str" select="$str" />
              </xsl:call-template>
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>
    
        <xsl:template name="checklength">
          <xsl:param name="str" />
          <xsl:choose>
            <xsl:when test="string-length($str) &gt; 10">
              <span class="spaced">
                <xsl:call-template name="insertspaces">
                  <xsl:with-param name="str" select="$str" />
                </xsl:call-template>
              </span>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$str" />
            </xsl:otherwise>
          </xsl:choose>
          <xsl:text> </xsl:text>
        </xsl:template>
    
        <xsl:template name="insertspaces">
          <xsl:param name="str" />
          <xsl:choose>
            <xsl:when test="string-length($str) &gt; 1">
              <xsl:value-of select="substring($str, 1, 1)" /><xsl:text> </xsl:text>
              <xsl:call-template name="insertspaces">
                <xsl:with-param name="str" select="substring($str, 2, string-length($str) - 1)" />
              </xsl:call-template>
            </xsl:when>
            <xsl:otherwise>
              <xsl:value-of select="$str" />
            </xsl:otherwise>
          </xsl:choose>
        </xsl:template>
    
    </xsl:stylesheet>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-05-26
      • 1970-01-01
      • 2019-06-13
      • 2012-03-06
      • 2018-06-29
      • 1970-01-01
      • 1970-01-01
      • 2014-07-20
      相关资源
      最近更新 更多