2013-12-17 2 views
0

각 문서가 책의 한 장인 여러 XML 문서가 있습니다. 이 모든 파일은 <book>이 루트 요소로 추가 된 하나의 DOMDocument로 병합됩니다. (장 = 1, 즉 default.php?.)위치로 매개 변수 값 사용

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

나는 성공적으로 내가 쿼리 문자열에 링크 장을 선택할 수있는 목차를 만들고있어 :

있는 DOMDocument는 다음과 같은 구조를 가져옵니다. 챕터를 열려고하면 param "chapter"가 쿼리 문자열로 설정됩니다.

쿼리 스트링과 동일한 장을 성공적으로 표시하려면 어떻게합니까? param에서 위치를 설정하는 데 문제가 발생합니다. "정의되지 않은 변수"오류가 발생합니다. 그러나 가치의 사용에 오류가 없습니다. "? default.php 장 1 ="

<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 장을 얻을 때이다 :

<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

결과로 원하는대로 내 질문을 업데이트했습니다. 난 단지 querystring을 기반으로 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

결과로 원하는대로 내 질문을 업데이트했습니다. 입력 된 쿼리 문자열을 기반으로 한 챕터 만 표시하려고합니다. 그래서 "default.php? chapter = 1"이라면 1 장만 보여줄 것입니다. – janlindso

+0

안녕하세요, 저는 제 대답을 편집했습니다 - 지금 도움이되기를 바랍니다. Peter 안부를 전합니다. – Peter