Blogify: Difference between revisions

From ICO wiki
Jump to navigationJump to search
Line 182: Line 182:
</xsl:stylesheet>
</xsl:stylesheet>
</source>
</source>
<br><br><br>


=XSLT Posts - Algne tulem=
=XSLT Posts - Algne tulem=

Revision as of 00:02, 21 March 2017

Meeskond

  • Hando Laasmägi
  • Tarvo Tammejuur
  • Cathy Toomast
  • Tõnn Vaher

XML fail

<?xml version="1.0" encoding="utf-8" ?>
<Main>
  <Posts>
    <Post PostId="1" UserId="1" Date="2015-01-13T07:25:15" Title="My trip to Norway">
      <Content><![CDATA[Blog post content.]]></Content>
      <Comments>
        <Comment UserId="2" Date="2015-01-14T13:55:33" Title="I am so envious!!!">
          <Content><![CDATA[I wish i had the time to do those cool things]]></Content>
        </Comment>
        <Comment UserId="3" Date="2015-02-11T19:24:22" Title="Prr... So cold out there">
          <Content><![CDATA[I don't know how you managed to stay alive there...]]></Content>
        </Comment>
      </Comments>
    </Post>
    <Post PostId="2" UserId="1" Date="2016-03-14T10:24:12" Title="My summer in Romania">
      <Content><![CDATA[Blog post content..]]></Content>
      <Comments>
        <Comment UserId="3" Date="2017-03-14T11:21:34" Title="Home of Dracula?!">
          <Content><![CDATA[So did you meat the charming beast out in the mountains?]]></Content>
        </Comment>
        <Comment UserId="2" Date="2017-03-15T12:56:11" Title="Do they use Euros there?">
          <Content><![CDATA[Do they have their own money or are they using Euros?]]></Content>
        </Comment>
        <Comment UserId="3" Date="2017-03-16T19:22:54" Title="Why u so poor?">
          <Content><![CDATA[Romania is for people who can't afford Greece?!]]></Content>
        </Comment>
      </Comments>
    </Post>
    <Post PostId="3" UserId="2" Date="2017-03-16T11:33:55" Title="Our love in Berlin">
      <Content><![CDATA[Blog post content.]]></Content>
      <Comments>
        <Comment UserId="1" Date="2017-03-19T16:23:12" Title="Such love, such lies...">
          <Content><![CDATA[I know all your secrets and all about Mark]]></Content>
        </Comment>
      </Comments>
    </Post>
  </Posts>
  <Users>
    <User UserId="1" Username="Hunter241" Firstname="Thomas" Surname="Stansfield" Email="tommy.stansfield@gmail.com">
      <Description><![CDATA[I am a tough old man standing out for my country]]></Description>
    </User>
    <User UserId="2" Username="ToughChick53" Firstname="Irene" Surname="Kennedy" Email="irene@cia.gov">
      <Description><![CDATA[Recently I have been feeling a little down because of the death of my mentor and all those attacks against me.]]></Description>
    </User>
    <User UserId="3" Username="IronMan" Firstname="Mitch" Surname="Rapp" Email="eyeinthemiddleeast@hotmail.com">
      <Description><![CDATA[I have been, where few American men have been.]]></Description>
    </User>
  </Users>
</Main>

XSLT - Posts

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="html" indent="yes"/>

  <xsl:template name="formatDate">
    <xsl:param name="dateTime" />
    <xsl:variable name="date" select="substring-before($dateTime, 'T')" />
    <xsl:variable name="year" select="substring-before($date, '-')" />
    <xsl:variable name="month" select="substring-before(substring-after($date, '-'), '-')" />
    <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />
    <xsl:value-of select="concat($day, '.', $month, '.', $year)" />
  </xsl:template>

  <xsl:template match="/">
    <html>
      <body>
        <h1>Blog posts!</h1>
        <xsl:for-each select="/Main/Posts/Post">
          <h2>
            <xsl:value-of select="@Title"/> (
            <xsl:call-template name="formatDate">
              <xsl:with-param name="dateTime" select="@Date" />
            </xsl:call-template>
            )
          </h2>
          <p>
            <xsl:value-of select="Content"/>
          </p>

          <h3>Comments</h3>
          <ol>
            <xsl:for-each select="Comments/Comment">
              <li>
                <b>
                  <xsl:value-of select="@Title"/>
                </b>
                (
                <xsl:call-template name="formatDate">
                  <xsl:with-param name="dateTime" select="@Date" />
                </xsl:call-template>
                )
                <p>
                  <xsl:value-of select="Content"/>
                </p>
              </li>
              <!--Ends comments list-->
            </xsl:for-each>
          </ol>
          <!--Ends posts list-->
        </xsl:for-each>
        <hr></hr>
        <h2>
          <u>Comments from user IronMan</u>
        </h2>
        <ol>
          <xsl:for-each select="/Main/Posts/Post/Comments/Comment">
            <xsl:if test="@UserId = 3">
              <li>
                <xsl:value-of select="@Title"/>
                (
                <xsl:call-template name="formatDate">
                  <xsl:with-param name="dateTime" select="@Date" />
                </xsl:call-template>
                )
                <br></br>
                <xsl:value-of select="Content"/>
              </li>
            </xsl:if>
          </xsl:for-each>
        </ol>

        <hr></hr>
        <h2>
          <u>Showing all posts with comments,<br></br>
        where day of week of published comment is less than 15, and sorted in descending order</u>
        </h2>
        <xsl:for-each select="/Main/Posts/Post">
          <xsl:for-each select="Comments/Comment">
            <xsl:sort select="@Date" order="descending"/>

            <xsl:variable name="date" select="substring-before(@Date, 'T')" />
            <xsl:variable name="day" select="substring-after(substring-after($date, '-'), '-')" />

            <xsl:if test="$day &lt; 15">
              <h2>
                <xsl:value-of select="@Title"/>
                (
                <xsl:call-template name="formatDate">
                  <xsl:with-param name="dateTime" select="@Date" />
                </xsl:call-template>
                )
              </h2>
              <p>
                <xsl:value-of select="Content"/>
              </p>

              <h3>Comments</h3>
              <ol>
                <li>
                  <b>
                    <xsl:value-of select="@Title"/>
                  </b>
                  (
                  <xsl:call-template name="formatDate">
                    <xsl:with-param name="dateTime" select="@Date" />
                  </xsl:call-template>
                  )
                  <p>
                    <xsl:value-of select="Content"/>
                  </p>
                </li>
              </ol>
            </xsl:if>
          </xsl:for-each>
        </xsl:for-each>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>




XSLT Posts - Algne tulem






XSLT Posts - Tingimustega tulem





XSLT - Users

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
  <xsl:output method="xml" indent="yes"/>

  <xsl:template match="/">
    <Users>
        <xsl:for-each select="/Main/Users/User">
          <xsl:if test="@Firstname='Mitch'">
            <User>
              <Username>
                <xsl:value-of select="@Username"/>
              </Username>
              <Firstname>
                <xsl:value-of select="@Firstname"/>
              </Firstname>
              <Surname>
                <xsl:value-of select="@Surname"/>
              </Surname>
              <Email>
                <xsl:value-of select="@Email"/>
              </Email>
              <Description>
                <xsl:value-of select="Description"/>
              </Description>
            </User>
          </xsl:if>  
        </xsl:for-each>
    </Users>
  </xsl:template>
</xsl:stylesheet>

XSLT - Users tulem

<?xml version="1.0" encoding="utf-8"?>
<Users>
  <User>
    <Username>IronMan</Username>
    <Firstname>Mitch</Firstname>
    <Surname>Rapp</Surname>
    <Email>eyeinthemiddleeast@hotmail.com</Email>
    <Description>I have been, where few American men have been.</Description>
  </User>
</Users>