2016-05-11 81 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

我试图删除@字符,但结果似乎仍然相同。 –