2010-03-31 17 views
1

有一個代碼:XSLT:在 「長」 字的附加空間

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

的意義在於劃分「長「詞與空間。在這個詞的每個字母后加空格。然後有必要總結這個詞在一個類「間隔」的標籤。

如果該單詞中的字母數量大於10(例如,可以設置任意值),則該單詞被認爲是「長」。

如何解決這個問題意味着xslt

回答

0

非常聰明的人都促使我決定,巨大的對他們的感謝。 工作在xslt 1.0沒有任何extansions。這不是我的解決方案。

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

這裏,你可以與Saxon 9AltovaXML tools運行XSLT 2.0樣式表:

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

@Martin Honnen,這段代碼只能在支持XSLT 2.0的處理器上工作嗎? XSLT 1.0有沒有解決方案?我們的服務器上沒有XSLT 2.0。 – Kalinin 2010-04-01 10:18:24

+0

您確實需要使用XSLT 2.0處理器來運行該樣式表。至於純粹的XSLT 1.0解決方案,我沒有一個。正如您所看到的,XSLT 2.0解決方案使用純XSLT 1.0根本不支持的正則表達式。如果你真的想用XSLT 1.0來解決這個問題,那麼我至少會檢查你的XSLT 1.0處理器是否支持正則表達式作爲擴展。 – 2010-04-01 10:25:30

+0

libxslt版本\t 1.1.17, 針對libxml編譯的libxslt版本\t 2.6.26, EXSLT已啓用, libexslt版本 - 1.1.17。 這是我們在服務器上的xslt設置。 – Kalinin 2010-04-01 11:29:06