2017-02-06 1 views
0
<library> 
<thriller> 
    <book> 
    <ISBN>1000</ISBN> 
    <title>Sherlock Holmes</title> 
    <author>Bob Dylan</author> 
    <publisher>BBC</publisher> 
    <country>England</country> 
    <price>10.90</price> 
    <edition>5</edition> 
    <year>2005</year> 
    </book> 
    <book> 
    <ISBN>1001</ISBN> 
    <title>The Indian Girl</title> 
    <author>Chetan Bhagat</author> 
    <publisher>Anusha Publishers</publisher> 
    <country>India</country> 
    <price>1270</price> 
    <edition>3</edition> 
    <year>2005</year> 
    </book> 
</thriller> 
</library> 


<xsl:for-each select="library/thriller/book"> then 
    <tr bgcolor="#9acd32"> 
      <td><xsl:value-of select="ISBN"/></td> 
      <td><xsl:value-of select="title"/></td> 
    </tr> 

</xsl:for-each> 

     <xsl:for-each select="library/thriller> 
     <xsl:for-each select="book"> 
      </xsl:for-each> 
    </xsl:for-each> 

위의 예에서 저는 스릴러에서 모든 책의 내용을 각각 라이브러리에 인쇄하려고합니다. 나는 을 시도했다. 그러나 그것은 테이블로서 움직이지 않는다. 필자는 각각에 필요한 모든 테이블 태그를 제공했습니다. 그래서 나는 시도했다. \xslt에서 각각에 대해 중첩 된 분기를 사용하는 방법

  but whether there is away to do that in single loop. thanks in advance. 
+0

입력하신 내용을 알려주시겠습니까? 고맙습니다! –

답변

0

여기에서는 하나의 루프로 작동하는 예제가있다. 첫 번째 템플릿 library 일치

  • : 것을

    <?xml version="1.0" encoding="UTF-8" ?> 
    <xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0"> 
        <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" /> 
    
        <xsl:template match="library"> 
        <table> 
         <xsl:for-each select="thriller/book"> 
         <tr bgcolor="#9acd32"> 
          <td><xsl:value-of select="ISBN"/></td> 
          <td><xsl:value-of select="title"/></td> 
         </tr> 
         </xsl:for-each> 
        </table> 
        </xsl:template> 
    
        <xsl:template match="/"> 
        <hmtl> 
         <head> 
         <title>Books</title> 
         </head> 
         <body> 
         <xsl:apply-templates/> 
         </body> 
        </hmtl> 
        </xsl:template> 
    
        <xsl:template match="@*|node()"> 
        <xsl:copy><xsl:apply-templates select="@*|node()"/></xsl:copy> 
        </xsl:template> 
    </xsl:transform> 
    

    참고.

  • 그러므로 for-each의 (상대) XPath는 내부 요소에 해당합니다. 즉 thriller/book입니다.
  • 루프 전에 테이블을 "시작"하고 나중에 닫아야합니다. 아마도 해결책에서 잊어 버렸을 것입니다.
  • 소스에 포함 된 마지막 2 개의 루프에는 빈 내용 ( )이 없으므로 아무것도 수행하지 않습니다.
+0

내부 book 요소는 내부 자식 요소 , 를 가진 다른 요소 lyk 을 가정합니다. 일부 자식 요소 만 인쇄하려면 각 안에 넣을 수 있습니다. 그런 다음 을 사용하여 모든 자식 elemnt를 인쇄합니다. 이 올바른지 ? –

+0

'for-each select = "스릴러/책"루프 내에서 현재 요소는'book'입니다. 그러므로, 현재'book의 하나의 자식 요소' 을 인쇄해야한다면'select = "something"'(without 스릴러/책)을 사용하십시오. 하나 이상의 자식 요소가있는 경우 'for each' 또는 예 : 'string-join' 함수. –

관련 문제