'Date operations on xsl 1.0

I need to get 3 days before and after a given date defined in a variable, and store each one of them in a new individual variable in xsl 1.0. i can't use any extensions or third party tools.

Looking trough the answers in the forums, i found this: Expanding datetime ranges in XSLT 1.0 for a similar problem, but i dont fully understand if and how it would aply to my code.

Mi date variable is in standard dateTime format, like this:

<xsl:variable name="Date" select="2014-05-13T00:00:00"/>

And i would need to output an html similar to this:

<table>
  <tr>
    <td>
   2014-05-10
    <td>
  </tr>
  <!---some rows with pricing information -->
</table>
<table>
  <tr>
    <td>
   2014-05-11
    <td>
  </tr>
  <!---some rows with pricing information -->
</table>
<table>
  <tr>
    <td>
   2014-05-12
    <td>
  </tr>
  <!---some rows with pricing information -->
</table>
<!-- etc -->

In the rows with pricing information I will have to use each individual date to perform other operations, so each day must be stored in a variable for further use.

Is there a way to accomplish this, using just xslt 1.0?

Thanks in advance.



Solution 1:[1]

SAXON 6.5.5 supports the EXSLT extensions, but the date:add-duration() from dates and times module — which would solve your problem elegantly — is not implemented.

However, you can directly use Java objects from within XSLT with Saxon:

You can also use a short-cut technique of binding external Java classes, by making the class name part of the namespace URI.

With the short-cut technique, the URI for the namespace identifies the class where the external function will be found. The namespace URI must either be "java:" followed by the fully-qualified class name (for example xmlns:date="java:java.util.Date") ...

Quoting from this post, the method for adding days to dates in Java is

String dt = "2008-01-01";  // Start date
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
c.setTime(sdf.parse(dt));
c.add(Calendar.DATE, 1);  // number of days to add
dt = sdf.format(c.getTime());  // dt is now the new date

the XSLT version could look something like this (untested):

<xsl:stylesheet
  version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:simple-date-format:="java:java.text.SimpleDateFormat"
  xmlns:calendar="java:java.util.Calendar"
>

  <xsl:template name="date-add-days">
    <xsl:param name="input" />
    <xsl:param name="days" />

    <xsl:if test="
      function-available('simple-date-format:new') 
      and function-available('calendar:get-instance')
    ">
      <xsl:variable name="sdf"  select="simple-date-format:new('yyyy-MM-dd')" />
      <xsl:variable name="cal"  select="calendar:get-instance()" />

      <xsl:variable name="time" select="simple-date-format:parse($sdf, $input)" />
      <xsl:variable name="tmp1" select="calendar:set-time($cal, $time)" />
      <xsl:variable name="tmp2" select="calendar:add($cal, calendar:DATE(), number($days))" />
      <xsl:variable name="res" select="calendar:get-time($cal)" />

      <xsl:value-of select="simple-date-format:format($sdf, $res)" />
    </xsl:if>
  </xsl:template>

</xsl:stylesheet>

With regard to interacting with Java classes and objects from XPath:

I'm not sure of the method name format. The Saxon 6.5.5 documentation seems to imply dashed format (toString() becomes to-string()), so I've been using this here. Maybe calendar:set-time() must actually called calendar:setTime(), try it out & fix my answer.

Solution 2:[2]

The issue seems to be in your nginx configuration, I would replicate this for your /ws endpoint. Seems to be missing the $proxy_add_x_forwarded_for

server {
        listen 80;
        server_name goddit.pro; // change this
        location / { // You can leave it at / or set it for another location, i.e. /ws
                proxy_set_header X-Real-IP $remote_addr;
                proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
                proxy_set_header Host $http_host;
                proxy_set_header X-NginX-Proxy true;

                proxy_pass http://localhost:8080/;
                proxy_redirect off;

                proxy_http_version 1.1;
                proxy_set_header Upgrade $http_upgrade;
                proxy_set_header Connection "upgrade";
        }
}

The issue could also be in your javaScript, here's how you connect and upgrade.

window.onload = function () {
  // Somewhere in your HTML document
  var conn;
  // Then you need to connect
  function connectWs(id) {
    if (window["WebSocket"]) {
      conn = new WebSocket("wss://" + document.location.host + "/id/" + id);
      conn.onopen = function (evt) {
        var item = document.createElement("li");
        item.innerHTML = "<em>Connected to " + id + "</em>";
        log.appendChild(item);
      };
      conn.onclose = function (evt) {
        var item = document.createElement("li");
        item.innerHTML = "<em>Connection to " + id + " closed</em>";
        log.appendChild(item);
      };
      conn.onmessage = function (evt) {
        var message = JSON.parse(evt.data);
        // console.log(message);
        printMessage(message);
      };
    } else {
      var item = document.createElement("li");
      item.innerHTML = "<b>Your browser does not support WebSockets.</b>";
      log.appendChild(item);
    }
  }
  connectWs(id);
}

After you shared the error logs from the React console:

Close Code 1006 is a special code that means the connection was closed abnormally (locally) by the browser implementation.

If your browser client reports close code 1006, then you should be looking at the websocket.onerror(evt) event for details.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Community
Solution 2