使用 Xslt 转换 Xml
我有以下 XML:
<?xml version="1.0" encoding="utf-8" ?>
<ApplicationSettingCategories>
<Category>Cat1</Category>
<Category>Cat2</Category>
<Category>Cat3</Category>
<Category>Cat4</Category>
<Category>Cat5</Category>
<Category>Cat6</Category>
</ApplicationSettingCategories>
我正在尝试使用 XmlDataSource 和 Xslt 将此 Xml 绑定到 ASP.net 中的下拉列表。 这是我第一次这样做。 下拉列表显示了正确数量的空白项目,使我相信迭代正在工作,但值和文本是空白的。
任何有助于识别我的错误的帮助将不胜感激。
谢谢
我的 XLST
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="ApplicationSettingCategories">
<Categories>
<xsl:apply-templates select="Category"/>
</Categories>
</xsl:template>
<xsl:template match="Category">
<Category>
<xsl:attribute name="Category">
<xsl:value-of select="Category"/>
</xsl:attribute>
</Category>
</xsl:template>
我的 ASPX
<asp:DropDownList ID="ddl1" runat="server" DataSourceID="XmlDataSource1"
DataTextField="Category" DataValueField="Category" />
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_Data/Xml/SettingCategory.xml"
TransformFile="~/Schema/AppCategoryXSLT.xslt"></asp:XmlDataSource>
我的源代码视图
<select name="ddl1" id="ddl1">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
I have the following XML:
<?xml version="1.0" encoding="utf-8" ?>
<ApplicationSettingCategories>
<Category>Cat1</Category>
<Category>Cat2</Category>
<Category>Cat3</Category>
<Category>Cat4</Category>
<Category>Cat5</Category>
<Category>Cat6</Category>
</ApplicationSettingCategories>
I am trying to bind this Xml to a Dropdownlist in ASP.net using an XmlDataSource and Xslt. This is my first time doing this. The Dropdownlist shows the correct number of blank items, leading me to believe the iteration is working but the Values and Text are blank.
Any help in identifying my error would be appreciated.
Thanks
My XLST
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="ApplicationSettingCategories">
<Categories>
<xsl:apply-templates select="Category"/>
</Categories>
</xsl:template>
<xsl:template match="Category">
<Category>
<xsl:attribute name="Category">
<xsl:value-of select="Category"/>
</xsl:attribute>
</Category>
</xsl:template>
My ASPX
<asp:DropDownList ID="ddl1" runat="server" DataSourceID="XmlDataSource1"
DataTextField="Category" DataValueField="Category" />
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/App_Data/Xml/SettingCategory.xml"
TransformFile="~/Schema/AppCategoryXSLT.xslt"></asp:XmlDataSource>
My Source View
<select name="ddl1" id="ddl1">
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
<option value=""></option>
</select>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这一行是您的问题:
此时(在
Category
模板内),上下文节点是当前类别。xsl:value-of
上的选择器正在查找上下文节点的子元素,也称为Category
。 只需更改该行即可获取上下文节点的文本值:This line is your problem:
At this point (inside the
Category
template), the context node is the current category. The selector you have on yourxsl:value-of
is looking for a child element of the context node also calledCategory
. Just change that line to get the context node's text value instead: