2016-07-04 6 views
0

XSLTに値とラベルがコロン(:)と次の項目の改行で区切られたドロップダウン要素を作成しようとしています。選択オプションを作成するXSLT分割文字列

第1回私はプレーンxmlを持っています。その後、私は次のだかわからない

<root> 
<text> 
<item>test1 : Test 1</item> 
<item>test2 : Test 2</item> 
<item>test3 : Test 3</item> 
</text> 
</root> 

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

    <xsl:template match="@*|node()"> 
     <xsl:copy> 
      <xsl:apply-templates select="@*|node()"/> 
     </xsl:copy> 
    </xsl:template> 


    <xsl:template match="text/text()" name="tokenize"> 

     <xsl:param name="text" select="."/> 
     <xsl:param name="separator" select="'&#x0a;'"/> 

     <xsl:choose> 
      <xsl:when test="not(contains($text, $separator))"> 
       <item><xsl:value-of select="normalize-space($text)"/></item> 
      </xsl:when> 
      <xsl:otherwise> 
       <item> 
        <xsl:value-of select="normalize-space(substring-before($text, $separator))"/> 
       </item> 

       <xsl:call-template name="tokenize"> 
        <xsl:with-param name="text" select="substring-after($text, $separator)"/> 
       </xsl:call-template> 

      </xsl:otherwise> 
     </xsl:choose> 
    </xsl:template> 
</xsl:stylesheet> 

出力:(システム生成)

<root> 
<item> 
test1 : Test 1 
test2 : Test 2 
test3 : Test 3 
</item> 
</root> 

はその後、私はそれを分割しましたか? 2番目のテンプレートを作成してhtml selectを作成することは可能ですか?

<select> 
<option value="test1">Test 1</option> 
<option value="test2">Test 2</option> 
<option value="test3">Test 3</option> 
</select> 

くださいhelppppp ....: '(

答えて

1

まあ、あなたのような何かにあなたのスタイルシートを変更した場合:

XSLT 1.0

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

<xsl:template match="/root"> 
    <html> 
     <body> 
      <xsl:apply-templates/> 
     </body> 
    </html> 
</xsl:template> 

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

<xsl:template name="tokenize"> 
    <xsl:param name="text"/> 
    <xsl:param name="delimiter" select="'&#10;'"/> 
     <xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" /> 
     <xsl:if test="$token"> 
      <option value="{substring-before($token, ' : ')}"> 
       <xsl:value-of select="substring-after($token, ' : ')"/> 
      </option> 
     </xsl:if> 
     <xsl:if test="contains($text, $delimiter)"> 
      <!-- recursive call --> 
      <xsl:call-template name="tokenize"> 
       <xsl:with-param name="text" select="substring-after($text, $delimiter)"/> 
      </xsl:call-template> 
     </xsl:if> 
</xsl:template> 

</xsl:stylesheet> 

あなたが届きます。

<html> 
    <body> 
     <select> 
     <option value="test1">Test 1</option> 
     <option value="test2">Test 2</option> 
     <option value="test3">Test 3</option> 
     </select> 
    </body> 
</html> 
+0

うわー!それは速い!あなたは私の日を救う!名誉! – mrrsb

関連する問題