2013-08-04 143 views
1

我正在學習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> 

請提供一些援助,爲我做錯了這裏。

+0

有什麼TransformFile的目的這種情況?它看起來像它會工作,如果你刪除該屬性,將主XPath更改爲'/ root/Customers/Customer',並將其他XPath更改爲'Id','FirstName'等。 – JLRishe

+0

謝謝,...我使用Visual Studio的轉換文件嘗試讀取xml文件時產生錯誤。它只會在使用屬性方法時讀取xml文件。 – user1724708

回答

1

測試出你的XSLT顯示輸出XML如下:

<root> 
    <Customer Id="1" FirstName="Joe" Surname="Doe"/> 
    <Customer Id="2" FirstName="Mary" Surname="Brown"/> 
    <Customer Id="3" FirstName="Paul" Surname="Smith"/> 
</root> 

如果您發現,沒有出現一個顧客在這裏元素,但在你的ASP XPath表達式:的XmlDataSource是「 /根/客戶」。

假設你是要刪除的客戶元素,首先需要改變你的XPath表達式相應

XPath="/root/Customer" 

但你也有與XPath表達式的問題在您的項目模板

<ItemTemplate> 
     <%# XPath("Customer/Id")%> 
    </ItemTemplate> 

在目前的修復中,您需要從表達式中刪除客戶,因爲您現在將位於客戶元素。而且,在您的輸出XML中,Id現在是屬性,而不是子元素。這意味着你的XPath必須是這樣的(注@符號用於屬性)

<ItemTemplate> 
     <%# XPath("@Id")%> 
    </ItemTemplate> 

試試這個ASPX

<asp:XmlDataSource ID="XmlDataSource1" runat="server" 
     DataFile="~/Sample-no-attributes.xml" 
     TransformFile="~/Sample-no-attributes.xsl" 
     XPath="/root/Customer"> 
    </asp:XmlDataSource> 

    <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
     DataSourceID="XmlDataSource1"> 
     <Columns> 
      <asp:TemplateField HeaderText="CustomerID"> 
     <ItemTemplate> 
      <%# XPath("@Id")%> 
     </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="First Name"> 
     <ItemTemplate> 
      <%# XPath("@FirstName")%> 
     </ItemTemplate> 
     </asp:TemplateField> 

     <asp:TemplateField HeaderText="Surname"> 
     <ItemTemplate> 
      <%# XPath("@Surname")%> 
     </ItemTemplate> 
     </asp:TemplateField> 
     </Columns> 
    </asp:GridView> 

需要注意的是,如果你確實想留住客戶在你的XSLT中,可能值得理解他們爲什麼失蹤。在你的第一個模板,你這樣做

<xsl:apply-templates select ="//Customers"/> 

但是你沒有一個模板在你的XSLT匹配客戶。發生這種情況時,應用XSLT的內置模板。這些將輸出元素內的任何文本節點,然後查找與其子元素匹配的模板。但它不會輸出元素本身。因此您將失去Customers元素。

要保留它,您可以添加下面的模板

<xsl:template match ="Customers"> 
    <Customers> 
     <xsl:apply-templates select ="Customer"/> 
    </Customers> 
</xsl:template> 

但是,它會更好,在這裏使用的XSLT Identity Transform,複製現有的元素。

試試這個XLST,它保留了Customers元素。注意你如何不需要明確的模板來匹配客戶。還要注意使用屬性值模板輸出每個元素的屬性,使得代碼變得更加冗長。

<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="@*|node()"> 
    <xsl:copy> 
     <xsl:apply-templates select="@*|node()"/> 
    </xsl:copy> 
    </xsl:template> 

    <xsl:template match ="//Customer"> 
    <Customer Id="{Id}" FirstName="{FirstName}" Surname="{Surname}" /> 
    </xsl:template> 
</xsl:stylesheet> 

如果你沒有使用XSLT,在asp的XPath的:XmlDataSource控件將保持爲「/根/客戶」,但的ItemTemplate中的XPath將<%# XPath("Customer/@Id")%>

+0

所有有效的觀點,但我沒有看到在這裏首先使用XSLT的要點,因爲它直接訪問XML也很容易。 – JLRishe

+0

謝謝你,這兩個解決方案/解釋工作,並非常有幫助。再次感謝 – user1724708