2017-02-06 8 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

ここでは、単一のループで動作する例があります。

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

こと注:最初のテンプレートはlibraryと一致する

  • したがって、for-eachの(相対)XPathは、内部の要素です。すなわち、 thriller/bookです。
  • ループの前に、テーブルを "開始"し、その後にそれを "閉じる"必要があります。 あなたのソリューションでそれを忘れたのかもしれません。
  • ソースに含まれている最後の2つのループに空のコンテンツ があるため、何もしません。
+0

内部の子要素、次にを持つ別の要素lyk があるとします。もし私がいくつかの子要素だけを印刷したいのであれば、を使用して、すべての子要素を表示しないようにします。これは正しいです ? –

+0

for-each select = "スリラー/ブック"ループの中で、現在の要素は 'book'です。 したがって、現在の 'bookのの1つの子要素を出力する必要がある場合は、' select = "something" '(' 'thriller/book'を除いて)を使用してください。 このような子要素が複数ある場合は、for eachと のいずれかを使用できます。 'string-join'関数です。 –

関連する問題