2012-05-02 7 views
0

変数があるとします。 <wish>Hi jony</wish> 私はwish要素を横断しなければならず、要素内の文字列の最初の文字のリンクを作成する必要があります。 出力は、私は、これはあなたがのために行くているものだと思います<a href="#H">H</a> <a href="#j">j</a>.xsltの文章で各文字列の最初の文字のリンクを作成する方法は?

+0

私はこれをしました。出てきました。EJ。 #hrefの値だけを取得する方法は? –

答えて

0

次のようになります。

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

文字列を分割するには再帰的テンプレートが必要です。次にb要素を追加します。適用できるテンプレートは次のとおりです。

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

あなたは正しい道を歩いています。クリーンアップと、それは次のようになり簡素化:トークン化の使用に基づいて

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

、私はあなたがハーポの再帰的なソリューションを不要にするXSLT 2.0を使用していると仮定しています。

0

この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文書に適用される:

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

<wish>Hi jony</wish> 

募集結果を生成します

注記concat()の使用は必要ありません。

+0

Dimitre、あなたの値下げはXSLTから生成されます。そうではありません。 – harpo

+0

@harpo:「マークダウン」とはどういう意味ですか? –

+0

"Markdown"は、回答の提出に使用される形式の名前です。 – harpo

関連する問題