2016-09-13 5 views
0

xslt에 링크가 포함 된 알파벳순 목록을 만드는 방법;xslt에서 알파벳순 목록을 생성하는 방법

<a href="sample.htm?letter=A">A</a> 
<a href="sample.htm?letter=B">B</a> 
<a href="sample.htm?letter=C">C</a> 
...up to.. 
<a href="sample.htm?letter=Z">Z</a> 

그것은 XML은 다음

<node> 
    <letter>ABCDEFGHIJKLMNOPQRSTUVWXYZ</text> 
</node> 

아니면 그냥 varible을 변환 할 수 있습니까?

<xsl:variable name="letter">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> 

답변

0

많은 테스트를 거친 후. 마침내 얻었습니다! 나중에 참조 할 때 필요로하는 사람들을 위해. 여기 내 코드가있다.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
<xsl:output method="html"/> 

<xsl:variable name="url">sample.htm</xsl:variable> 
<xsl:template match="node"> 
    <xsl:apply-templates/> 
</xsl:template> 

<xsl:template match="letter"> 
    <xsl:call-template name="element"><xsl:with-param name="text" select="."/></xsl:call-template> 
</xsl:template> 

<xsl:template name="element"> 
    <xsl:param name="text"/> 

    <xsl:variable name="token" select="substring($text, 1, 1)" /> 

    <xsl:if test="$token"> 
     <a href="{$url}?letter={$token}"><xsl:value-of select="$token"/></a><br/> 
    </xsl:if> 

    <xsl:if test="string-length($text) > 1"> 
     <xsl:call-template name="element"> 
      <xsl:with-param name="text" select="substring($text, 2, string-length($text) - 1)"/> 
     </xsl:call-template> 
    </xsl:if> 

</xsl:template> 

</xsl:stylesheet> 

더 나은 또는 더 간단한 솔루션 친절한 의견이 있으시면.

1

XSLT 1.0

<?xml version="1.0" encoding="UTF-8"?> 
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> 

    <xsl:output method="html"/> 

    <xsl:variable name="url">sample.htm</xsl:variable> 
    <xsl:variable name="letter">ABCDEFGHIJKLMNOPQRSTUVWXYZ</xsl:variable> 

    <xsl:template match="/"> 
     <xsl:call-template name="iterate"> 
      <xsl:with-param name="string" select="$letter"/> 
     </xsl:call-template> 
    </xsl:template> 

    <xsl:template name="iterate"> 
     <xsl:param name="string"/> 
     <xsl:param name="length" select="1" /> 

     <xsl:if test="string-length($string)"> 

      <xsl:variable name="char" select="substring($string, 1, 1)" /> 

      <xsl:call-template name="createEntry"> 
       <xsl:with-param name="token" select="$char"/> 
      </xsl:call-template> 

      <xsl:call-template name="iterate"> 
       <xsl:with-param name="string" select="substring-after($string, $char)" /> 
      </xsl:call-template> 
     </xsl:if>   
    </xsl:template> 

    <xsl:template name="createEntry"> 
     <xsl:param name="token"/> 
     <a href="{$url}?letter={$token}"><xsl:value-of select="$token"/></a><br/> 
    </xsl:template> 

</xsl:stylesheet> 

설명

스토어는 제안과 같은 변수로 알파벳입니다.

는 PARAM string 같은 기능 iterate로 전체 문자열을 전달합니다. [선택 파라메터 : length]

createEntry가 기능하도록 단일 문자를 전달.

기능 createEntry 출력을한다. 당신이 좋아하는 경우

, 그것을 최대

+0

감사합니다 선생님을 제공합니다. 그것의 더 나은 생각 : 나는 그것을 포기할 것이다. – mrrsb

관련 문제