我正在學習XPath的過程中,並且當我在線上找到示例xml文件時,應用程序無法在將XSL文件實施到項目中後呈現任何值。任何關於我失蹤或做錯的建議非常感謝。XPATH不檢索節點II
XML文件:
<?xml version="1.0" encoding="utf-8"?>
<root>
<Customers>
<Customer>
<Id>1</Id>
<FirstName>Joe</FirstName>
<Surname>Doe</Surname>
</Customer>
</Customers>
<Customers>
<Customer>
<Id>2</Id>
<FirstName>Mary</FirstName>
<Surname>Brown</Surname>
</Customer>
</Customers>
<Customers>
<Customer>
<Id>3</Id>
<FirstName>Paul</FirstName>
<Surname>Smith</Surname>
</Customer>
</Customers>
</root>
XSL:
<?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:template match ="/">
<root>
<xsl:apply-templates select ="//Customers"/>
</root>
</xsl:template>
<xsl:template match ="//Customer">
<Customer>
<xsl:attribute name="Id">
<xsl:value-of select="Id"/>
</xsl:attribute>
<xsl:attribute name="FirstName">
<xsl:value-of select="FirstName"/>
</xsl:attribute>
<xsl:attribute name="Surname">
<xsl:value-of select="Surname"/>
</xsl:attribute>
</Customer>
</xsl:template>
</xsl:stylesheet>
ASPX:
<asp:XmlDataSource ID="XmlDataSource1" runat="server"
DataFile="~/xml/Sample-no-attributes.xml"
TransformFile="~/xml/Sample-no-attributes.xsl"
XPath="/root/Customers">
</asp:XmlDataSource>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
DataSourceID="XmlDataSource1">
<Columns>
<asp:TemplateField HeaderText="CustomerID">
<ItemTemplate>
<%# XPath("Customer/Id")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="First Name">
<ItemTemplate>
<%# XPath("Customer/FirstName")%>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Surname">
<ItemTemplate>
<%# XPath("Customer/Surname")%>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
請提供一些援助,爲我做錯了這裏。
有什麼TransformFile的目的這種情況?它看起來像它會工作,如果你刪除該屬性,將主XPath更改爲'/ root/Customers/Customer',並將其他XPath更改爲'Id','FirstName'等。 – JLRishe
謝謝,...我使用Visual Studio的轉換文件嘗試讀取xml文件時產生錯誤。它只會在使用屬性方法時讀取xml文件。 – user1724708