我做错了什么。这是一个显示问题的示例XSL文件:
<?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好像有问题。
这里的问题是:
<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']"/>
我想$AssignHistory/AssignmentHistory/*/Items/Item[@Name='Name1']
应该是$AssignHistory/AssignmentHistory/*/Items/Item[Name='Name1']
。