开发者

How to assign XML attribute values to drop down list using XSL

I have a sample xml as;

<?xml version="1.0" encoding="iso-8859-9"?>
    <DropDownControl id="dd1" name="ShowValues" choices="choice1,choice2,choice3,choice4">
</DropDownControl >开发者_如何转开发;

I need to create a UI representation of this XML using XSL. I want to fill the drop down list with values specified in choices attribute.

Does anyone have any idea about this ?

Thanks in advance :)


If you have a search of StackOverflow, you should find there are many questions relating to the splitting of strings.

Here's one, for example:

Comma Separated String Parsing

In your case, here how you would use it

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

   <xsl:template match="/DropDownControl">
      <select>
         <xsl:call-template name="split">
            <xsl:with-param name="list" select="@choices"/>
         </xsl:call-template>
      </select>
   </xsl:template>

   <xsl:template name="split">
      <xsl:param name="list" select="''"/>
      <xsl:param name="separator" select="','"/>
      <xsl:if test="not($list = '' or $separator = '')">
         <xsl:variable name="head" select="substring-before(concat($list, $separator), $separator)"/>
         <xsl:variable name="tail" select="substring-after($list, $separator)"/>
         <option>
            <xsl:value-of select="$head"/>
         </option>
         <xsl:call-template name="split">
            <xsl:with-param name="list" select="$tail"/>
            <xsl:with-param name="separator" select="$separator"/>
         </xsl:call-template>
      </xsl:if>
   </xsl:template>

</xsl:stylesheet>

This produces the following XML/HTML

<select>
<option>choice1</option>
<option>choice2</option>
<option>choice3</option>
<option>choice4</option>
</select>

If you were able to use XSLT2.0 you could make use of the tokenise function. See this page for an example

Best way to split and render comma separated text as html

However, I notice you are tagged the question asp.net and c#2.0 which suggests you won't be able to use XSLT2.0 directly.

Another alternatively, given that the transform is being done using C#, is that you do the splitting in the code behind, before the transformation is done. Write some code to read the XML, access the attribute, use .Net's string split function, and append child elements to the DropDownControl element.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜