2013-03-15 18 views
-5

后面的代码可以实现同样的功能,那么使用xslt和xml有什么好处?我最近开始使用ASP.NET(VB)的XML。 我的查询如下。如果通过

1)如果后面的代码可以实现同样的功能,为什么还要使用xslt? 2)你能推荐一个简单的xslt教程链接,这对于像我这样的新手来说是可以理解的。 3)我有一个XML文件,它看起来像这样

<video> 
     <name>name</name> 
     <source>source</source> 
     <category>category</category> 
     <date>date</date> 
     <description>description</description> 
     <image>image</image> 
     <tags>tags</tags> 
</video> 

我要在以下格式

<ItemTemplate > 
       <asp:Label ID="nameLabel" runat="server" Text='<%# Eval("name") %>' /> 
       <br /> 
       <asp:Image ID="ImgsLabel" runat="server" ImageUrl='<%# Eval("image") %>' /> 
       <br /> 
       Length: 
       <asp:Label ID="lengthLabel" runat="server" Text='<%# Eval("length") %>' /> 
       <br /> 
       Dateloaded: 
       <asp:Label ID="DateloadedLabel" runat="server" Text='<%# Eval("date") %>' /> 
       <br /> 
       <asp:Label ID="DescriptionLabel" runat="server" Text='<%# Eval("description") %>' Visible="False" /> 
       <asp:Label ID="sourceLabel" runat="server" Text='<%# Eval("source") %>' Visible="False" /> 
       <br /> 
       <asp:LinkButton id="SelectButton" Text="Select" CommandName="Select" runat="server" 
      />     
      </ItemTemplate> 

显示这个文件你可以明白,这可以很容易地使用后面的代码实现,但为了学习一个人一步一步地指导我如何在asp.net页面中使用xslt实现这一目标?

+0

你可能只想把代码放在后面...... – summea 2013-03-15 20:42:06

+2

为什么用xslt的时候可以用其他方法做同样的事情?这很容易问,为什么在xslt中可以做同样的事情时使用其他方式?检查你的假设。 – 2013-03-15 22:58:27

+0

我得到了它的感谢您的所有帮助。检查我的解决方案 – user2175422 2013-03-16 11:51:29

回答

0

我开始googleing XSLT例子,落在这个page. 请记住,这是我第一天用XML和XSLT试验。我通常在我的应用程序中使用SQL数据源。

所以我有一个XML文件,它基本上看起来像这样

<?xml version="1.0" encoding="utf-8" ?> 
    <!DOCTYPE video [ 
    <!ELEMENT video (name,embedsource,category,date,description,image,tags)> 
    <!ELEMENT name (#PCDATA)> 
    <!ELEMENT embedsource (#PCDATA)> 
    <!ELEMENT category (#PCDATA)> 
    <!ELEMENT date (#PCDATA)> 
    <!ELEMENT description (#PCDATA)> 
    <!ELEMENT image (#PCDATA)> 
    <!ELEMENT tags (#PCDATA)> 
    ]> <!-- This part is the Document Type Definition --> 
    <video> 
    <name>somename</name> 
    <embedsource>some source</embedsource> 
    <category>some category</category> 
    <date>16/03/2013</date> 
    <description>some description</description> 
    <image>some image link</image> 
    <tags>sometag1,,sometag2,,sometag3,,sometag4</tags> 
    </video> <!-- This part contains the actual data --> 

现在,我们有我们的数据,但我们需要显示/这个数据反馈到控制的一些方法(这可能是GridView控件或DataList控件或任何你感到舒服的东西)应用程序。我可以ofcourse使用字符串函数或其他一些代码来实现这一点,但我们将使用变换(你可以得到有关的详细信息在w3school链接变换。

<?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="/"> 
     <video> 
     <xsl:apply-templates select="//video"/> 
     </video> 
     </xsl:template> 
     <xsl:template match ="//video"> 
     <video> 
     <xsl:attribute name="name"> 
     <xsl:value-of select="name"/> 
     </xsl:attribute> 
     <xsl:attribute name="description"> 
     <xsl:value-of select="description"/> 
     </xsl:attribute> 
     <xsl:attribute name="category"> 
     <xsl:value-of select="category"/> 
     </xsl:attribute> 
     <xsl:attribute name="date"> 
     <xsl:value-of select="date"/> 
     </xsl:attribute> 
     <xsl:attribute name="embedsource"> 
     <xsl:value-of select="embedsource"/> 
     </xsl:attribute> 
     <xsl:attribute name="tags"> 
     <xsl:value-of select="tags"/> 
     </xsl:attribute> 
     </video> 
     </xsl:template> 
     </xsl:stylesheet> <!-- Notice that I am using Attribute in my xslt but in my xml 
     file data is in Element form that is because DATASETS can only recognise 
     Attributes. Attributes and Elements are interchangeable in an xml file 
     and it is entirely upto you. I can be wrong here please correct me if it is the 
     case. --> 

最后的一步是配置xml数据源。文件路径将是xml文件的路径,转换路径将是xslt文件的路径。向你的应用程序添加一个数据列表,你身边的疯狂科学家去muhahahahahhahaaaa。

使用xslt的正反面仍然是开放的,我很快就会结束这一部分。