Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7511125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T23:27:07+00:00 2026-05-29T23:27:07+00:00

Iv’e got an xml file containing data about games, which are an .exe download;

  • 0

Iv’e got an xml file containing data about games, which are an .exe download; the game name; the image location and the alternate text to use. Which is sorted into catagorys. (See Below)

<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="games.xsl"?>
<all>
 <general>
  <catagory_name>
   <catg_1>Action</catg_1>
   <catg_2>Other</catg_2>
  </catagory_name>
 </general>
 <games>
  <catagory name='Action'>
   <game>
    <name>1945<!--name--></name>
    <url><!--URL to game download--></url>
    <image><!--image location--></image>
    <alt>1945 Icon<!--alt text for image--></alt>
   </game>
  </catagory>
  <catagory name='Other'>
   <game>
    <name>Platform Game<!--name--></name>
    <url><!--URL to game download--></url>
    <image><!--image location--></image>
    <alt>Platform Game Icon<!--alt text for image--></alt>
   </game>
  </catagory>
 </games>
</all>

I’ve then got this xslt file to convert the xml to xhtml…

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:template match="/all">
  <html>
   <body>
    <h1 style="text-align:center;">Games</h1>
    <center>
     <table>
      <xsl:if test="//general/catagory_name/catg_1">
       <xsl:variable name="catg">
        <xsl:value-of select="//general/catagory_name/catg_1"/>
       </xsl:variable>
       <xsl:if test="//games/catagory[@name=$catg]/game[position() = (1)]/url">
        <tr>
         <td colspan="4" rowspan="1">
          <h2>
           <xsl:copy-of select="$catg"/>
          </h2>
         </td>
        </tr>
        <xsl:for-each select="//games/catagory[@name=$catg]/game[position() mod 4 = 1]">
         <xsl:variable name="pos" select="position() * 4"/>
         <tr>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
         </tr>
         <tr>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 3)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -3)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 2)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -2)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos - 1)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos -1)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg]/game[position() = ($pos)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg]/game[position() = ($pos)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg]/game[position() = ($pos)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
         </tr>
        </xsl:for-each>
       </xsl:if>
      </xsl:if>
      <xsl:if test="//general/catagory_name/catg_2">
       <xsl:variable name="catg2">
        <xsl:value-of select="//general/catagory_name/catg_2"/>
       </xsl:variable>
       <xsl:if test="//games/catagory[@name=$catg2]/game[position() = (1)]/url">
        <tr>
         <td colspan="4" rowspan="1">
          <h2>
           <xsl:copy-of select="$catg2"/>
          </h2>
         </td>
        </tr>
        <xsl:for-each select="//games/catagory[@name=$catg2]/game[position() mod 4 = 1]">
         <xsl:variable name="pos" select="position() * 4"/>
         <tr>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
             </xsl:attribute>
             <img>
              <xsl:attribute name="src">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/image)"/>
              </xsl:attribute>
              <xsl:attribute name="alt">
               <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/alt)"/>
              </xsl:attribute>
             </img>
            </a>
           </td>
          </xsl:if>
         </tr>
         <tr>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -3)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -2)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -1)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
          <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
           <td>
            <a>
             <xsl:attribute name="href">
              <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
             </xsl:attribute>
             <p>
              <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos)]/name"/>
             </p>
            </a>
           </td>
          </xsl:if>
         </tr>
        </xsl:for-each>
       </xsl:if>
      </xsl:if>
     </table>
    </center>
   </body>
  </html>
 </xsl:template>
</xsl:stylesheet>

This code works absolutely fine however it is already quite complicated. First it checks to see if a particular category exists then if that category has any games in it. Then it displays it but only if the data exists. So you’ll be pleased that that’s all fine. My problem is that every time i want to add a new category i have to go into the xslt file and add the code below with the variable $catg being called $catg1, $catg2 and so on for all the catagorys i need because you can’t change the value of an existing variable.

 <xsl:if test="//general/catagory_name/catg_2">
  <xsl:variable name="catg2">
   <xsl:value-of select="//general/catagory_name/catg_2"/>
  </xsl:variable>
  <xsl:if test="//games/catagory[@name=$catg2]/game[position() = (1)]/url">
   <tr>
    <td colspan="4" rowspan="1">
     <h2>
      <xsl:copy-of select="$catg2"/>
     </h2>
    </td>
   </tr>
   <xsl:for-each select="//games/catagory[@name=$catg2]/game[position() mod 4 = 1]">
    <xsl:variable name="pos" select="position() * 4"/>
    <tr>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
        </xsl:attribute>
        <img>
         <xsl:attribute name="src">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/image)"/>
         </xsl:attribute>
         <xsl:attribute name="alt">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/alt)"/>
         </xsl:attribute>
        </img>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
        </xsl:attribute>
        <img>
         <xsl:attribute name="src">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/image)"/>
         </xsl:attribute>
         <xsl:attribute name="alt">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/alt)"/>
         </xsl:attribute>
        </img>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
        </xsl:attribute>
        <img>
         <xsl:attribute name="src">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/image)"/>
         </xsl:attribute>
         <xsl:attribute name="alt">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/alt)"/>
         </xsl:attribute>
        </img>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
        </xsl:attribute>
        <img>
         <xsl:attribute name="src">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/image)"/>
         </xsl:attribute>
         <xsl:attribute name="alt">
          <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/alt)"/>
         </xsl:attribute>
        </img>
       </a>
      </td>
     </xsl:if>
    </tr>
    <tr>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 3)]/url)"/>
        </xsl:attribute>
        <p>
         <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -3)]/name"/>
        </p>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 2)]/url)"/>
        </xsl:attribute>
        <p>
         <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -2)]/name"/>
        </p>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos - 1)]/url)"/>
        </xsl:attribute>
        <p>
         <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos -1)]/name"/>
        </p>
       </a>
      </td>
     </xsl:if>
     <xsl:if test="//games/catagory[@name=$catg2]/game[position() = ($pos)]/url">
      <td>
       <a>
        <xsl:attribute name="href">
         <xsl:value-of select="normalize-space(//games/catagory[@name=$catg2]/game[position() = ($pos)]/url)"/>
        </xsl:attribute>
        <p>
         <xsl:value-of select="//games/catagory[@name=$catg2]/game[position() = ($pos)]/name"/>
        </p>
       </a>
      </td>
     </xsl:if>
    </tr>
   </xsl:for-each>
  </xsl:if>
 </xsl:if>

What i want to do is to be able to use a for each so only have to have one of the code (above) however complicated that never needs altering for a new catagory. So my question is how would i do this? Ive tried with variables ect but ou’d still need to edit the code each time you wanted a new catagory because you can’t change the value of an existing variable.

The only solution i can think of is to replace the $catg with the node value it represents ie general/catagory_name/catg and loop it every time but i haven’t had any success.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T23:27:09+00:00Added an answer on May 29, 2026 at 11:27 pm

    Use OL for numbering them from 1.

    Select each category, irrespectively of duplicated values.

    Find out if this is the first occurrence of category, by id of the first game, and id of first-of-all game within the same category.

    If this is the first occurrence of category, just apply-templates for each game with ancestor as the given category. Use li for each category, and use classes and CSS to achieve table-like arrangement.

    Sorts are added to achieve sorted output…

    <xsl:template name="categoryList">
        <ol>
        <xsl:for-each select="//game/ancestor::category">
            <xsl:sort select="@name" />
            <xsl:variable name="name" select="@name"/>
            <xsl:variable name="gameid1" select="generate-id(game[1])"/>
            <xsl:variable name="gameidFirst" select="generate-id(//game[ancestor::category[@name=$name]][1])"/>
            <xsl:if test="$gameid1=$gameidFirst">
                <li><xsl:value-of select="@name"/>
                    <xsl:apply-templates select="//game[ancestor::category[@name=$name]]">
                        <xsl:sort select="name/text()"/>
                    </xsl:apply-templates>
                </li>
            </xsl:if>
        </xsl:for-each>
        </ol>
    </xsl:template>
    
    <xsl:template match="game">
                    <div>
                        Name:<span class="name"><xsl:value-of select="name"/></span>,
                        URL:<span class="url"><xsl:value-of select="url"/></span>,
                        Image:<span class="image"><xsl:value-of select="image"/></span>,
                    </div>
    </xsl:template>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm parsing an XML file, the creators of it stuck in a bunch social
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.