2013-12-17 23 views
0

我有几个XML文档,其中每个文档都是书中的一个章节。所有这些文件都合并到一个DOMDocument中,其中<book>作为根元素添加。使用参数值作为位置

DOM文档获取的结构如下:(?即如default.php章= 1)

<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> 

我成功地实现了表的内容,我可以用查询字符串链接选择章节。当我尝试打开一章时,参数“章节”被设置为查询字符串。

如何成功显示与查询字符串相等的章节?我从param设置位置出现问题。我收到一个“未定义的变量”错误。但是,当我在价值中使用它时没有错误。

<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> 

所以,我想的是,当用户访问他得到的唯一1章这样的“如default.php章= 1?”:

<html> 
<head> 
<title>Chapter 1 - This is chapter 1</title> 
</head> 
<body> 
<h1>Chapter 1 - This is chapter 1</h1> 
blablablablablablabla 
</body> 
</html> 
+0

你不能在使用变量的。我不明白你想达到什么目的。你有没有发布你期望的目标XML? – Peter

+0

我用我想要的结果更新了我的问题。我只想根据查询字符串显示1个章节。 – janlindso

回答

0

如果应用此XSLT您源XML:

<xsl:param name="chosenChapter" select="'1'"/> 

<xsl:template match="/"> 
    <list> 
     <xsl:apply-templates select="book/chapter[position() = $chosenChapter]"/> 
    </list> 
</xsl:template> 


<xsl:template match="chapter"> 
    <xsl:param name="chapter" select="@title"/> 


    <html> 
     <head> 
      <title><xsl:value-of select="$chosenChapter"/> - <xsl:value-of select="$chapter"/></title> 
     </head> 
     <body>    
      <h1> 
       Chapter <xsl:value-of select="$chosenChapter"/> <xsl:value-of select="$chapter"/> 
      </h1> 
      <text> 
       <xsl:apply-templates /> 
      </text> 
     </body> 
    </html> 
</xsl:template> 

你得到这样的输出:

<?xml version="1.0" encoding="UTF-8"?> 
<list> 
    <html> 
      <head> 
        <title>1 - This is the first chapter</title> 
      </head> 
      <body> 
        <h1> Chapter 1This is the first chapter</h1> 
        <text> This is the paragraph content </text> 
      </body> 
    </html> 
</list> 

我编辑我的问题。我希望这有帮助。在<xsl:template match=""中,您不能添加变量,但可以在<xsl:apply-templates select=""中。在我的例子中,chosenChapter被硬编码为“1”。

最好的问候, 彼得

+0

我用我想要的结果更新了我的问题。我只想根据输入的查询字符串显示1个章节。所以如果“default.php?chapter = 1”,它只会显示第一章。 – janlindso

+0

你好,我编辑了我的答案 - 我希望现在能帮上忙。最好的问候,彼得 – Peter