XSLT 分组和循环组
我有以下 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>
我必须复制的模式。有没有办法将
部分完全进入模板并使用
将所有ListItem子节点分组在一起并将它们填充到循环模板中?
I have the following 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>
Then I have the following XSLT for the listbox:
<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>
Using this approach, I would have to duplicate the whole looping for the DropDownList
element too.
Now, to avoid a lot of the duplication I understand I can do something like this:
<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>
But what I don't like about this is the
<Items>
<xsl:apply-templates select="./ListItem" />
</Items>
pattern I would have to duplicate. Is there a way to put the <Items> {loop through ListItems}</Items>
part completely into a template and use <xsl:apply-templates select="??" />
to group all ListItem child nodes together and stuff them into the looping template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用命名模板?
使用命名模板:
Why not using a named template?
with named template:
这个模板怎么样:
What about this template: