【发布时间】:2013-12-17 13:42:27
【问题描述】:
我有几个 XML 文档,每个文档都是一本书的一个章节。所有这些文件都合并到一个 DOMDocument 中,其中将 <book> 添加为根元素。
DOMDocument 得到以下结构:
<book>
<chapter title="This is the first chapter">
<section title="This is the first section">
<paragraph title="This is the first paragraph">This is the paragraph content</paragraph>
</section>
</chapter>
<chapter title="This is the second chapter">
<section title="This is the first section">
<paragraph title="This is the first paragraph">This is the paragraph content</paragraph>
</section>
</chapter>
</book>
我成功地制作了一个目录,我可以在其中选择带有查询字符串链接的章节(即 default.php?chapter=1)。当我尝试打开一章时,参数“chapter”被设置为查询字符串。
如何才能成功显示与查询字符串相等的章节?我从参数设置位置时遇到问题。我收到“未定义的变量”错误。但是当我在 value-of 中使用它时没有错误。
<xsl:param name="chapter" />
<xsl:template match="//chapter[$chapter]">
<html>
<head>
<title><xsl:value-of select="$chapter"/> - <xsl:value-of select="@title"/></title>
</head>
<body>
<h1>
Chapter <xsl:value-of select="$chapter"/> <xsl:value-of select="@title"/>
</h1>
<xsl:apply-templates />
</body>
</html>
</xsl:template>
所以,我想要的是,当用户访问“default.php?chapter=1”时,他会得到唯一的第 1 章:
<html>
<head>
<title>Chapter 1 - This is chapter 1</title>
</head>
<body>
<h1>Chapter 1 - This is chapter 1</h1>
blablablablablablabla
</body>
</html>
【问题讨论】:
-
你不能在 中使用变量。我不明白你试图达到什么目的。您可以发布您期望的目标 XML 吗?
-
我用我想要的结果更新了我的问题。我只想根据查询字符串显示 1 章。