2016-05-11 33 views
0

我在這裏做錯了什麼。這是顯示問題的樣本XSL文件:使用XML從XML文件中提取條目列表

<?xml version="1.0" encoding="utf-8"?> 
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> 
    <xsl:output method="html" indent="yes" version="4.01" 
    doctype-system="http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" 
    doctype-public="//W3C//DTD XHTML 1.0 Transitional//EN"/> 
    <xsl:template match="/"> 
    <html xmlns="http://www.w3.org/1999/xhtml"> 
     <head> 
     <meta content="text/html; charset=utf-8" http-equiv="Content-Type" /> 
     <!--<link rel="stylesheet" type="text/css" href="Workbook-off.css"/>--> 
     <title>Custom Report</title> 
     </head> 
     <body> 
     <xsl:variable name="AssignHistory" select="document('AssignHistory.xml')"/> 

     <xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1']"> 
      <xsl:apply-templates select="Item"/> 
     </xsl:for-each> 
     </body> 
    </html> 
    </xsl:template> 
    <xsl:template match="Item"> 
    <xsl:variable name="datestr" select="name(../..)" /> 
    <p> 
     <xsl:value-of select="substring($datestr, 8, 2)"/> 
     <xsl:text>/</xsl:text> 
     <xsl:value-of select="substring($datestr, 6, 2)"/> 
     <xsl:text>/</xsl:text> 
     <xsl:value-of select="substring($datestr, 2, 4)"/> 
     <br/> 
     <xsl:value-of select="Theme"/> 
     <br/> 
     <xsl:value-of select="Method"/> 
     <br/> 
    </p> 
    </xsl:template> 
</xsl:stylesheet> 

這對XML數據:

<?xml version="1.0" encoding="UTF-8"?> 
<AssignmentHistory Version="1610"> 
    <W20160104> 
     <Items ItemCount="5"> 
      <Item> 
       <Name>Name1</Name> 
       <Theme>"True Worship Requires Hard Work"</Theme> 
       <Method>Talk</Method> 
      </Item> 
      <Item> 
       <Name>Name2</Name> 
       <Theme>Digging for Spiritual Gems</Theme> 
       <Method>Questions and Answers</Method> 
      </Item> 
      <Item> 
       <Name>Name3</Name> 
       <Theme>Prepare This Month’s Presentations</Theme> 
       <Method>Discussion with Video(s)</Method> 
      </Item> 
      <Item> 
       <Name>Name4</Name> 
       <Theme>"Our Privilege to Build and Maintain Places of True Worship"</Theme> 
       <Method>Discussion with Interview(s)</Method> 
      </Item> 
      <Item> 
       <Name>Name9</Name> 
       <Theme>"Our Privilege to Build and Maintain Places of True Worship Part 2"</Theme> 
       <Method>Discussion with Interview(s)</Method> 
      </Item> 
     </Items> 
    </W20160104> 
    <W20160111> 
     <Items ItemCount="5"> 
      <Item> 
       <Name>Name5</Name> 
       <Theme>"Jehovah Values Genuine Repentance"</Theme> 
       <Method>Talk</Method> 
      </Item> 
      <Item> 
       <Name>Name1</Name> 
       <Theme>Digging for Spiritual Gems</Theme> 
       <Method>Questions and Answers</Method> 
      </Item> 
      <Item> 
       <Name/> 
       <Theme>Prepare This Month’s Presentations</Theme> 
       <Method>Discussion with Video(s)</Method> 
      </Item> 
      <Item> 
       <Name>Name7</Name> 
       <Theme>Repentance Makes a Difference</Theme> 
       <Method>Talk</Method> 
      </Item> 
      <Item> 
       <Name>Name8</Name> 
       <Theme>Forgive Freely</Theme> 
       <Method>Discussion with Video(s)</Method> 
      </Item> 
     </Items> 
    </W20160111> 
</AssignmentHistory> 

最終,我將它構建在一個表中去。但是目前我正期待它拿出兩個參賽作品。沒有顯示。

我的xpath似乎是錯誤的或什麼的。

+0

好,「沒有東西顯示」聽起來好像你在瀏覽器中測試XSLT,你可能想通過啓動安裝一個可以從命令行使用的XSLT處理器,或者將XSLT處理器作爲插件連接到您喜歡的文本編輯器,以便您可以查看和發佈由XSLT創建的XHTML,而不僅僅依賴於瀏覽器。 –

+0

@MartinHonnen我從來沒有使用過「XSLT處理器」。我在Visual Studio中編寫XSL文件並執行它(在我的情況下,在CHtmlView瀏覽器控件中)。 –

+0

我認爲Visual Studio的商業版本帶有一個XSLT調試器,所以它可能是嘗試使用它的一個很好的起點。 –

回答

2

這裏的問題是:

<xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"> 

使您的Item上下文。從這方面:

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

選擇什麼,因爲Item本身不是一個孩子。

最簡單的解決辦法是更換:

<xsl:for-each select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"> 
    <xsl:apply-templates select="Item"/> 
</xsl:for-each> 

有:

<xsl:apply-templates select="$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']"/> 
+0

謝謝。就是這樣。那,而不是使用@字符。 –

1

我猜$AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1']應該是$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']

+0

我試圖刪除@字符,但結果似乎仍然相同。 –