简体   繁体   English

使用Xslt转换Xml

[英]Transform Xml using Xslt

I have the following 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>

I am trying to bind this Xml to a Dropdownlist in ASP.net using an XmlDataSource and Xslt. 我正在尝试使用XmlDataSource和Xslt将此Xml绑定到ASP.net中的Dropdownlist。 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 我的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 我的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>

This line is your problem: 这行是你的问题:

<xsl:value-of select="Category"/>

At this point (inside the Category template), the context node is the current category. 此时(在“ Category模板内部),上下文节点是当前类别。 The selector you have on your xsl:value-of is looking for a child element of the context node also called Category . 您在xsl:value-of上拥有的选择器正在寻找上下文节点(也称为Category子元素 Just change that line to get the context node's text value instead: 只需更改该行即可获取上下文节点的文本值:

<xsl:value-of select="text()"/>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM