Incorporate div classes based on conditions in XSLT version 1.0

Utilizing my database software, I can export my albums collection to HTML using an XSL template. After modifying the XSL template to support Bootstrap 4, I attempted to incorporate an alphabetical filter. Although I managed to create a JavaScript for filtering, it lacks animation when applied.

To introduce animations like in this example here, I need to assign appropriate classes in a div within each album based on the initial letter of the artist's name.

Since I export from a database into HTML, providing an XML input is challenging. However, if I were to export my data in XML format, a snippet of the result would look like this:

    <musiclist>
<music>
  // Sample XML content here
</music>
</musiclist>
</musicinfo>

Below is the section of XSL code I've modified: For each album, I aim to retrieve all artists and assign the correct class to the div based on their initials. The resulting class names are concatenated with Bootstrap layouts.

<xsl:template match="musiclist">
    // XSL logic here
</xsl:template>

The current HTML output for an album should have a structure similar to this:

<div class="row equal" id="album-table">
    // Sample HTML content here
</div>

However, despite proper concatenation, the correct classes are not being added to the div elements by XSL. Any tips on why this might be happening?

Once I resolve this issue and add the correct classes to the divs, I plan to enhance the layout with responsiveness and animations as demonstrated here.

Your guidance on this matter would be greatly appreciated. Thank you!

Answer №1

It seems like you have XML input data that is transformed using XSLT. Your "database software" likely returns the data in XML format for this purpose.

If you're having trouble finding the XML input, you can utilize the XSLT identity transform, which will generate output identical to the input.

To address your question, let's assume your XML structure resembles this...

<musiclist>
    <music>
        <id>8660</id>
        <title>Afrikan Basement - Unreleased Extended Versions - Disc 1</title>
        <thumbfilepath>?</thumbfilepath>
        <coverfront>?</coverfront>
        <artists>
            <artist><displayname>Bolla</displayname></artist>
        </artists>
    </music>
    <music>
        <id>8881</id>
        <title>A Journey To Rotterdam</title>
        <thumbfilepath>?</thumbfilepath>
        <coverfront>?</coverfront>
        <artists>
            <artist><displayname>Jepht&#233; Guillaume</displayname></artist>
            <artist><displayname>Diephuis</displayname></artist>
        </artists>
    </music>
    <music>
        <id>376</id>
        <title>La Home Box - Disc 3</title>
        <thumbfilepath>?</thumbfilepath>
        <coverfront>?</coverfront>
        <artists>
            <artist><displayname>Laurent Garnier</displayname></artist>
            <artist><displayname>Traumer</displayname></artist>
            <artist><displayname>Bamboun</displayname></artist>
        </artists>
    </music>
</musiclist>

The initial issue with your XSLT code lies in how you define variables.

<xsl:variable name="ArtistName">
  <xsl:value-of select="substring(artists/artist/displayname, 1, 1)"/>
</xsl:variable>

The correct approach would be simplifying it to something like this...

<xsl:variable name="ArtistName" select="substring(., 1, 1)"/>

Additionally, the syntax like below is invalid as dynamic variable names aren't supported.

<xsl:variable name="first-letter{$count}">

For a more efficient solution, consider eliminating excessive variables and dynamically building the required attributes within the template.

<xsl:template match="musiclist">
    <xsl:for-each select="music">
        <div align="center">
            <xsl:attribute name="class">
                <xsl:text>col-xs-12 col-sm-6 col-md-2 filter</xsl:text>
                <xsl:for-each select="artists/artist/displayname">
                    <xsl:value-of select="concat(' letter', substring(., 1, 1))" />
                </xsl:for-each>
            </xsl:attribute>
            <xsl:apply-templates select="."/>
        </div>
    </xsl:for-each> 
</xsl:template>

Your XSLT could benefit from such enhancements to improve functionality and clarity.

You might want to try out this revised XSLT code to address these concerns.

[Revised XSLT Code Goes Here]

Feel free to test it at this link.

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

Is there a way to eliminate the white line on my bootstrap navbar and customize the text colors?

I've been experimenting with Bootstrap and want to create a small personal page with a navbar design like the one below: <body> <nav class="navbar navbar-default navbar-fixed-top navbar-shrink"> <div class="container"> <div c ...

React Timer App: The setInterval function is being reset after each render operation

I'm currently working on a straightforward timer application that will begin counting seconds when a button is clicked. To implement this, I am utilizing react hooks. import React, { useState } from 'react' function Timer() { const [sec ...

Similar to session_start() and $_SESSION in Node.js/Express

I'm on a quest to discover the equivalent of session_start() and $_SESSION in Node.js/Express so I can store the current user's id in the session. Most tutorials and videos recommend using express-session, but I've come across a warning: ...

The AngularJS alert success message does not display the success div immediately after the function returns a successful response

Here is the HTML code snippet for users to input their email address and student ID: <div id="statusScope" class="pos-rel" style="margin:0 auto 50px;" ng-controller="statusController"> <form class="form-inline text-center" action=""> < ...

Xpath-Utilizing a variable within an XPATH expression is necessary

Currently, I am utilizing Selenium-webdriver and facing an issue while trying to select an element based on its "text" using xpath. Since the value is subject to change, I am utilizing a variable. I am looking for a solution to resolve this problem or an a ...

The failure of CSS Layout Design

So I'm attempting to create a profile cover like the one shown below: This is what my code looks like: <div class="show_cover"> <%= image_tag @show.cover(:show_cover) %> <!-- Mouseover Options--> <% if current_user == @ho ...

Error in Angular Due to Circular Reference in JSON

I'm currently working on integrating a tree structure in AngularJS. The goal is to construct the tree by dynamically adding nodes based on user input from an Angular Select element. Here is the specific operation I'm trying to accomplish: var a ...

Issue with enabling Picture-in-Picture feature on iOS Progressive Web Apps

My goal is to enable Picture-in-Picture (PiP) on an HTML video by using the code below: await videoElement.requestPictureInPicture().catch((error) => alert(`PiP failed, ${error}`); ); While this code works perfectly in Safari, I encountered an iss ...

Assign a JavaScript variable upon clicking a polygon on Mapbox

I have a MapBox map that consists of approximately 800 polygons representing census tracts, created using TileMill. The map has been integrated into an HTML page alongside a D3.js chart. A drop-down menu on the page allows users to select one of the 800 ce ...

Refreshing $scope variables within a function

$scope.myVar = cheese; function please(){ $scope.myVar = "New Value"; } google.maps.event.addListener(marker, 'click', please); This scenario is perplexing to me. I have a variable called "cheese" in the HTML and I anticipated that clicking ...

Tips for crafting interactive Dropdown menus

Visit this link for more information Hello all, I am a beginner in HTML and Javascript and seeking guidance on how to create dynamic dropdown menus similar to the ones on the provided website. I have successfully implemented the source code, but my questi ...

Is it possible to dynamically assign class properties from an array in JavaScript/TypeScript?

Greetings for the assistance in advance. Currently, I am working with TypeScript, but I believe any JS solution will suffice. I aim to create something akin to the following: class ExcelData { 'Id 1': items[0].id, 'Quantity 1': item ...

What could be causing the script to display the object's content inaccurately?

Below is the code for the client side: import {useEffect,useState} from 'react'; import io from 'socket.io-client'; import Peer from './Peer'; export default function TestMeeting(){ let peerName; const [peerList,setPee ...

Executing the get function using javascript

Recently, I've been experimenting with using Web Api Asp.Net alongside Javascript to execute my controller. Here's an example of how I've been approaching this: JS function loadCatalog() { apiService.get("../../api/CatalogoReg ...

When hovering over the image, it enlarges and gradually becomes transparent, unveiling the hidden text beneath

I've been struggling to achieve the desired effect for quite some time now, putting in hours of effort without success. Essentially, I am aiming to create a div that showcases an image. When the mouse hovers over the image, it should zoom in and fade ...

Triggering a JavaScript function when a page is focused due to user interaction

In my project, I have a specific requirement that involves triggering a new window to open when the user clicks on an icon. In this new window, the user should be able to view and edit certain fields. Upon closing the new window and returning to the parent ...

How can I retrieve the value from req.body following an AJAX POST request on the server side with Express?

Currently, I am utilizing AJAX to send JSON data: app.use(express.json()); app.use(bodyParser.urlencoded({extended:true})) app.use(express.urlencoded({ extended: true})); const rowObject=JSON.stringify(rowData) $.ajax({ type: "POST&q ...

What is the best way to activate a progress bar upon clicking a button in an HTML document?

Is it possible to make a progress bar start moving upon clicking a button using only HTML? I am new to coding (just started today) and would appreciate some guidance on how to achieve this. This is what I currently have: <input type="image" src="Butto ...

"Interactive functionality: Toggling checkboxes with a DIV click

I am trying to set up a simple div container that displays contact information in a formatted list (<ul><li>). Clicking on the div currently takes you to the user's profile page, which is functioning correctly. However, I am facing an iss ...

Challenges with date formatting arise for Spanish speakers when the date returns as NaN or an Invalid

I have been working on an Angular app Objective: My aim is to allow users to input dates in Spanish format (DD/MM/YYYY) and display them as such, while converting them back to English format when saving the data to the Database. Issue: One problem I enco ...