Nelly
Nelly

Reputation: 309

xslt: retrieve data from an xml file comparing dates

I am trying to get from an xml file that contains data about some movies, all the movies that are playing this week, but it prints me out all the movies.. I am new to xml and xslt so, any help would be appreciated! My xml looks like:

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="movies.xsl"?>
<?xml-stylesheet type="text/css" href="movies.css"?>
<movies>
   <movie>
      <title> Title1 </title>
      <actor> Actor1 </actor>
      <genre> Genre1 </genre>
      <dateOfPlaying>20160403</dateOfPlaying>
      <duration> Duration1 </duration>
   </movie>
   <movie>
      <title> Title2 </title>
      <actor> Actor2 </actor>
      <genre> Genre2 </genre>
      <dateOfPlaying>20160420</dateOfPlaying>
      <duration> Duration2 </duration>
   </movie>
   <movie>
      <title> Title2 </title>
      <actor> Actor2 </actor>
      <genre> Genre2 </genre>
      <dateOfPlaying>20160406</dateOfPlaying>
      <duration>Duration3</duration>
   </movie>
</movies>  

and the xsl file:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <html>
        <body>
            <h2> Movies report </h2>
                <table>
                    <xsl:for-each select="movies/movie">
                        <xsl:if test="number(dateOfPlaying) > 20160401 and number(dateOfPlaying) < 20160408">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="actor"/>
                            </td>
                            <td>
                                <xsl:value-of select="genre"/>
                            </td>
                            <td>
                                <xsl:value-of select="dateOfPlaying"/>
                            </td>
                            <td>
                                <xsl:value-of select="duration"/>
                            </td>
                        <tr>
                        </xsl:if>
                    </xsl:for-each>
                </table>
            </body>
        </html>
    </xsl:template>
</xsl:stylesheet>

Upvotes: 0

Views: 65

Answers (1)

SomeDude
SomeDude

Reputation: 14228

You need this :

<xsl:if test="dateOfPlaying &gt; 20160401 and dateOfPlaying &lt; 20160408">

I also see you have a wrong <tr> before </xsl:if> correct it to </tr>

Below is xsl:

<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
    <html>
        <body>
            <h2> Movies report </h2>
                <table>
                    <xsl:for-each select="movies/movie">
                        <xsl:if test="dateOfPlaying &gt; 20160401 and dateOfPlaying &lt; 20160408">
                        <tr>
                            <td>
                                <xsl:value-of select="title"/>
                            </td>
                            <td>
                                <xsl:value-of select="actor"/>
                            </td>
                            <td>
                                <xsl:value-of select="genre"/>
                            </td>
                            <td>
                                <xsl:value-of select="dateOfPlaying"/>
                            </td>
                            <td>
                                <xsl:value-of select="duration"/>
                            </td>
                        </tr>
                        </xsl:if>
                    </xsl:for-each>
                </table>
            </body>
    </html>
</xsl:template>
</xsl:stylesheet>

The output looks like :

Movies report

Title1 Actor1 Genre1 20160403 Duration1
Title2 Actor2 Genre2 20160406 20160430

Upvotes: 1

Related Questions