How to use XSL 1.0 to identify the highest value among nodes

Looking to Retrieve the Maximum Value from Nodes

Below is the XML provided:

<xml>
     <NewDataSet>
        <priorityResult>
            <DXCount>32</DXCount>
            <LHHight>12</LHHight>
            <LHMedium>1</LHMedium>
            <RiskPriority>6</RiskPriority>
        </priorityResult>
    </NewDataSet>
   </xml>

The goal is to calculate a value on an XSLT page using this logic:

 var priority= parseInt(row["RiskPriority"]);
 var newPriority= 0;

if ("DXCount" > 0 )
newPriority= 4;
else if ("LHHight" > 0)
newPriority= 3;
else if ("LHMedium" > 0)
newPriority= 2;

return Math.max(priority, newPriority)

How can I achieve this max value using XSLT?

Here's my attempt at displaying the Node values:

<?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">
<xsl:output method="html" version="4.0" encoding="UTF-8" indent="yes"/>
 <xsl:template match="/">    
<xsl:for-each select="//priorityResult[SessionID != '']">
            <p>
              Priority :
              <xsl:choose>
                  <xsl:when test="DXCount > 0">
                    4
                  </xsl:when>
                  <xsl:when test="LHHight > 0">
                    3
                  </xsl:when>
                  <xsl:when test="LHMedium > 0">
                    2
                  </xsl:when>
                  <xsl:otherwise>
                    <xsl:value-of select ="RiskPriority"/>
                  </xsl:otherwise>
                </xsl:choose>
            </p>
          </xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Answer №1

XSLT 1.0 does not come with a built-in max() function. If I follow your reasoning correctly, you can work around this limitation in XSLT 1.0 by creating your own implementation:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="html"/>

<xsl:template match="/xml">    
    <xsl:for-each select="NewDataSet/priorityResult">
        <xsl:variable name="newPriority">
            <xsl:choose>
                <xsl:when test="DXCount > 0">4</xsl:when>
                <xsl:when test="LHHigh > 0">3</xsl:when>
                <xsl:when test="LHMedium > 0">2</xsl:when>
            </xsl:choose>
        </xsl:variable>
        <p>
            <xsl:text>Priority :</xsl:text>
            <xsl:choose>
                <xsl:when test="RiskPriority > $newPriority">
                    <xsl:value-of select ="RiskPriority"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:value-of select ="$newPriority"/>
                </xsl:otherwise>
            </xsl:choose>
        </p>
    </xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Exploring the flow of try, catch, and finally blocks with return statements in C#

There is very little uncertainty regarding the workflow of try, catch, and finally with return statements... This particular function is utilized to fetch employee leave information for supervisor viewing. It operates efficiently, returning data if found ...

Cloud Firestore query error in Firebase Cloud Function despite data being present in Cloud Firestore

I'm facing an issue with my cloud function. The function is designed to query data from a Cloud Firestore collection, which exists. However, when I call the function from my iOS app, it always goes to the else statement and prints "NOT IN COLLECTION." ...

Having difficulty extracting data from a mongoDB object on the frontend for a Node server using JavaScript and EJS

I'm currently facing an issue while trying to display a mongoDB object and what I intend to achieve is not happening as expected. Here is the EJS code snippet: <% for(let date in data) { %> <% let parsedDate = Date.parse(date); %&g ...

Ways to verify if a user is authenticated in Firebase and using Express/Node.js

How can I determine if a user is authenticated to access a page that requires authentication? I attempted to check with (firebase.auth().currentUser !== null) but encountered an error: TypeError: firebase.auth is not a function This is my current configu ...

A double click is required to enable the connected mouse press/release action

I have a section of code that is giving me some trouble: $('.toggle_box').each(function (){ var timeout, longtouch; $(this).bind('mousedown', function() { timeout = setTimeout(function() { longtouch = tru ...

What is the best way to retrieve all classes that have a hyphen/dash in them from a DOM element and then store them in an array using JavaScript?

How can I retrieve all classes that contain a hyphen/dash applied to any DOM element and store them in an array using JavaScript? My current approach: const getClasses = []; const classesContain = []; document.querySelectorAll('*').forEach( ...

The issue of AngularJS memory leaks

We're facing a memory leak issue in our AngularJS application when switching between different sections. Pinpointing the root cause has been a challenge for us. Our app includes a main controller with a sub controller nested within it. The sub contro ...

Seamlessly linking TypeScript projects on both client and server side

My root project includes both the server and client side apps structured as follows: -- server -- node_modules -- index.ts -- package.json -- ... -- client -- node_modules -- index.ts -- package.json -- html/ -- css/ -- ... I'm s ...

Tips for correctly sending the response code from a Node.js API

I have a straightforward node-based API that is responsible for parsing JSON data, saving it into a Postgres database, and sending the correct response code (e.g., HTTP 201). Here is an excerpt of my code: router.route('/customer') .post(fu ...

Bootstrap: The search icon button within the input box breaks away from the input box on resolutions other than desktop

I am attempting to place a Search Icon Button inside the Search Input Box while using Bootstrap. Although everything appears correctly at Desktop resolution, at non-desktop resolutions when the menu items collapse into a dropdown, the button and input box ...

Selecting an option from dropdown1 to retrieve a specific value from a JSON file

I currently have 2 dropdown lists to fill with data from a JSON file: $scope.categories = [ {name:"Blouse", sizes:["36","38","40","42","44","46","48","50"]}, {name:"Shirt", sizes:["36","38","40","42","44","46","48","50"]}, {name:"Pants", size ...

ReactJS Chatkit has not been initialized

I made some progress on a tutorial for creating an Instant Messenger application using React and Chatkit. The tutorial can be found in the link below: https://www.youtube.com/watch?v=6vcIW0CO07k However, I hit a roadblock around the 19-minute mark. In t ...

JavaScript's asynchronous callbacks

As a PHP developer delving into the world of NodeJS, I find myself struggling to fully grasp the concept of asynchrony in JavaScript/Node. Consider this example with ExpressJS: router.get('/:id', function (req, res, next) { var id = req.par ...

Creating custom functions within views using Sencha Touch 2

I'm having trouble creating my own function in Sencha Touch 2 and I keep receiving an error: Uncaught ReferenceError: function22 is not defined The issue seems to be coming from my Position.js file located in the View directory. Ext.define(' ...

Implementing Oauth in Angular and Node

I am facing challenges with implementing oauth in Angular. When I directly enter the link into the browser http://localhost:8080/auth/twitter I am able to successfully connect using oauth and receive a response. However, when I try to achieve the same in ...

Setting up AutoMapper to employ local time for all DateTime properties

Let's consider two classes with identical properties: public class MyDto { public int Id { get; set; } public DateTime CreatedOn { get; set; } } public class MyViewModel { public int Id { get; set; } public DateTime CreatedOn { get; s ...

Discovering Product Information Using ASP.NET MVC4 and Barcode Labels

I'm currently working on a web application that involves generating labels with barcodes. We now need to be able to read these labels during the packing stage. My understanding is that Barcode scanners function as keyboard input, so I've created ...

Encrypting href links in Nodemailer with Handlebars for forwarding purposes

I am working on a project that involves utilizing NodeMailer + Handlebars for sending and tracking emails. I am interested in changing every href link to the project URL before redirecting to the destination link. For example: Original email: <a href ...

Incorrect Tooltip DisplayWhat could be causing the issue with

I am facing an issue when trying to add a tooltip to a glyphicon within a tile. It doesn't seem to work correctly when it should. However, placing the tooltip outside of the tile works fine. I'm quite perplexed and would greatly appreciate any as ...

"What could be causing the issue of the key enter button not functioning when

I'm currently facing an issue with my login form. When the input field is focused and Enter is pressed without filling out the fields, errors are prompted as expected. However, even after entering both fields correctly and pressing Enter, nothing happ ...