Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [XML][XSL] Co drugie pole pokolorowane.
Forum PHP.pl > Forum > XML, AJAX > XML
Reiven
Cześć wam,
Mam taki problem z XSL, otóż chcę zrobić tak żeby lista numerowana wyświetlała wpisy tak, że wpisy mają być na niebiesko, a co drugi np na zielono...
Coś tego typu:

Astrid Lindgren

1. Bracia Lwie Serce
2. Braciszek i Karlsoon z dachu
3. Dzieci z Bullerbyn
4. Dzieci z z ulicy Awanturników
5. Dzień dziecka w Bullerbyn

Mój XSL wygląda tak:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  3.  
  4. <xsl:output encoding="UTF-8" media-type="text/xml"/>
  5.  
  6. <xsl:template match="/">
  7. <html>
  8. <head>
  9. <title><xsl:value-of select="@title"/></title>
  10. <meta http-equiv="Content-Language" content="pl"/>
  11. <meta http-equiv="Content-Type" content="text/html; CHARSET=UTF-8"/>
  12.  
  13. </head>
  14. <body>
  15. <xsl:apply-templates select="//author"/>
  16. </body>
  17. </html>
  18. </xsl:template>
  19. <xsl:template match="author">
  20. <xsl:variable name="aid" select='@id'/>
  21. <xsl:variable name='book_count' select="count(//book[@author=$aid])"/>
  22.  
  23.  
  24. <xsl:variable name='font_color'>
  25. <xsl:choose>
  26. <xsl:when test='$book_count>1'>gold</xsl:when>
  27. <xsl:when test='$book_count=1'>green</xsl:when>
  28. <xsl:otherwise>black</xsl:otherwise>
  29. </xsl:choose>
  30. </xsl:variable>
  31.  
  32.  
  33.  
  34.  
  35. <h1>
  36. <font><xsl:attribute name='color'><xsl:value-of select='$font_color'/></xsl:attribute>
  37. <xsl:apply-templates select='gname'/> <xsl:apply-templates select='sname'/>
  38. </font>
  39. </h1>
  40.  
  41.  
  42. <ol>
  43. <xsl:if test="$book_count">
  44. <font> <xsl:attribute name="color">red</xsl:attribute>
  45. <xsl:apply-templates select="//book[@author=$aid]"/>
  46. </font>
  47. </xsl:if>
  48.  
  49.  
  50.  
  51. </ol>
  52.  
  53. </xsl:template>
  54.  
  55. <xsl:template match='gname'>
  56. <xsl:value-of select='text()'/>
  57. </xsl:template>
  58.  
  59. <xsl:template match='book'>
  60. <li>
  61. <xsl:value-of select='title'/>
  62. </li>
  63.  
  64. </xsl:template>
  65.  
  66. </xsl:stylesheet>


a część XML tak:

  1. <baza>
  2. <author id="1">
  3. <gname>Astrid</gname>
  4. <sname>Lindgren</sname>
  5. </author>
  6. <book id="1" author="1">
  7. <title>Bracia Lwie Serce</title>
  8. </book>
  9. <book id="2" author="1">
  10. <title>Braciszek i Karlsoon z dachu</title>
  11. </book>
  12. <book id="3" author="1">
  13. <title>Dzieci z Bullerbyn</title>
  14. </book>
  15. <book id="4" author="1">
  16. <title>Dzieci z z ulicy Awanturników</title>
  17. </book>
  18. <book id="5" author="1">
  19. <title>Dzień dziecka w Bullerbyn</title>
  20. </book>
  21. </baza>


Jak widać mam też zrobione kolorowanie według ilości posiadanych książek, lecz chciałbym też dla odróżnienia żeby każdy wpis był np niebieski, a co drugi zielony.. Walcze z tym od dwóch dni i nie mam żadnych koncepcji...
cfk
z wezła book spróbuj użyć modulo dla @id.
i na to wszystko if winksmiley.jpg

<xsl:if test=book@id mod 2 = '1' ">
kolor zielony
</xsl:if>
<xsl:if test=book@id mod 2 != '1' ">
kolor niebieski
</xsl:if>

ale czy to zadziała to nie jestem pewien na 100%
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.