У меня есть следующий XML:
<DropDownList id="Dropdown">
<Label text="Dropdown"/>
<ListItem value="Test1"/>
<ListItem value="Test2"/>
</DropDownList>
<ListBox id="Listbox1" >
<Label text="SingleSelect"/>
<ListItem value="Test1"/>
<ListItem value="Test2"/>
</ListBox>
Затем у меня есть следующий XSLT для списка:
<xsl:template match="ListBox">
<th>
<xsl:apply-templates select="./Label" />
</th>
<td>
<asp:ListBox runat="server" ID="{@id}">
<Items>
<xsl:for-each select="./ListItem">
<asp:ListItem Value="{@value}">
<xsl:attribute name="Text">
<!-- fill text accordingly to text attribute or same as value when not specified-->
<xsl:choose>
<xsl:when test="@text">
<xsl:value-of select="@text"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="@selected">
<xsl:attribute name="selected">
<xsl:value-of select="@selected"/>
</xsl:attribute>
</xsl:if>
</asp:ListItem>
</xsl:for-each>
</Items>
</asp:ListBox>
</td>
<td>
<xsl:apply-templates select="./*[contains(name(), 'Validation')]" />
</td>
<xsl:copy-of select="$br"/>
</xsl:template>
Используя этот подход, мне пришлось бы дублировать весь цикл и для элемента DropDownList
.
Теперь, чтобы избежать дублирования, я понимаю, что могу сделать что-то вроде этого:
<xsl:template match="ListBox">
<th>
<xsl:apply-templates select="./Label" />
</th>
<td>
<asp:ListBox runat="server" ID="{@id}">
<Items>
<xsl:apply-templates select="./ListItem" />
</Items>
</asp:ListBox>
</td>
<td>
<xsl:apply-templates select="./*[contains(name(), 'Validation')]" />
</td>
<xsl:copy-of select="$br"/>
</xsl:template>
<!-- Helper template for list items -->
<xsl:template match="ListItem">
<asp:ListItem Value="{@value}">
<xsl:attribute name="Text">
<!-- fill text accordingly to text attribute or same as value when not specified-->
<xsl:choose>
<xsl:when test="@text">
<xsl:value-of select="@text"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="@value"/>
</xsl:otherwise>
</xsl:choose>
</xsl:attribute>
<xsl:if test="@selected">
<xsl:attribute name="selected">
<xsl:value-of select="@selected"/>
</xsl:attribute>
</xsl:if>
</asp:ListItem>
</xsl:template>
Но что мне не нравится в этом, так это
<Items>
<xsl:apply-templates select="./ListItem" />
</Items>
образец, который я должен был бы продублировать. Есть ли способ полностью поместить часть <Items> {loop through ListItems}</Items>
в шаблон и использовать <xsl:apply-templates select="??" />
для группировки всех дочерних узлов ListItem вместе и включения их в шаблон цикла?
<xsl:template match="ListBox|Dropdown">...</xsl:template>
? - person Boldewyn   schedule 26.08.2011<asp:DropDown>
и имеет другие атрибуты. Существуют также элементы CheckBoxList и RadioButtonList, которые имеют другие особенности. - person Sebastian P.R. Gingter   schedule 26.08.2011