0

<wish>Hi jony</wish> ウィッシュ要素をトラバースする必要がある変数 があり、要素内の文字列の最初の文字のリンクを作成する必要があるとします。出力は<a href="#H">H</a> <a href="#j">j</a>.

4

4 に答える 4

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>
于 2012-05-02T05:29:09.633 に答える
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>

トークン化の使用に基づいて、harpoの再帰的ソリューションの必要性を排除するXSLT2.0を使用していると想定しています。

于 2012-05-02T10:45:31.540 に答える
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 ドキュメントに適用した場合:

<wish>Hi jony</wish>

必要な結果を生成します:

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

注意: を使用するconcat()必要はありません。

于 2012-05-02T12:52:52.847 に答える
0

文字列を分割して要素を構築するには、再帰的なテンプレートが必要です。適用できるテンプレートは次のとおりです。

  <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>  
于 2012-05-02T05:44:22.007 に答える