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 9101845
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T01:14:06+00:00 2026-06-17T01:14:06+00:00

I would like to match the first occurance of a specific node value and

  • 0

I would like to match the first occurance of a specific node value and i am stumped.

I have tried <xsl:when test="root/content/contentType = root/content/contentType[.='generic'][1]"> but it matches every occurance, as does <xsl:when test="root/content/contentType[.='generic'][1]">

I would like to end up with the output HTML below. The first item has a header but every item has a class of the same value.

Here is my XML.

Any ideas are very much appreciated.

XML:

<root>
    <content>
        <contentType>ingredients</contentType>
        <listItems>
            <item>Item 1</item>
            <item>Item 2</item>
        </listItems>
    </content>
    <content>
        <contentType>generic</contentType>
        <listItems>
            <item>Item 1</item>
            <item>Item 2</item>
        </listItems>
    </content>
    <content>
        <contentType>generic</contentType>
        <listItems>
            <item>Item 1</item>
            <item>Item 2</item>
        </listItems>
    </content>
    <content>
        <contentType>ingredients</contentType>
        <listItems>
            <item>Item 1</item>
            <item>Item 2</item>
        </listItems>
    </content>
    <content>
        <contentType>directions</contentType>
        <listItems>
            <item>Item 1</item>
            <item>Item 2</item>
        </listItems>
    </content>
</root>

Desired Output:

<div class="ingredients">
    <h2>Ingredients</h2>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</div>
<div class="generic">
    <h2>Generic</h2>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</div>
<div class="generic">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</div>
<div class="ingredients">
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</div>
<div class="directions">
    <h2>Directions</h2>
    <ul>
        <li>Item 1</li>
        <li>Item 2</li>
    </ul>
</div>

EDIT:

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    version="1.0">
    <xsl:template match="/">

        <xsl:for-each select="root/content">
            <div>
                <xsl:attribute name="class"><xsl:value-of select="./contentType"/></xsl:attribute>

                <xsl:choose>
                    <xsl:when test="./contentType[.='ingredients'][1]">
                        <h2><xsl:value-of select="./contentType"/></h2>
                    </xsl:when>
                    <xsl:when test="./contentType[.='generic'][1]">
                        <h2><xsl:value-of select="./contentType"/></h2>
                    </xsl:when>
                    <xsl:when test="./contentType[.='directions'][1]">
                        <h2><xsl:value-of select="./contentType"/></h2>
                    </xsl:when>
                </xsl:choose>
                <ul>
                    <xsl:for-each select="listItems/item">
                        <li><xsl:value-of select="."/></li>
                    </xsl:for-each>
                </ul>
            </div>
        </xsl:for-each>       

    </xsl:template>
</xsl:stylesheet>
  • 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-06-17T01:14:08+00:00Added an answer on June 17, 2026 at 1:14 am

    How about this:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
        <xsl:template match="/">
    
            <xsl:for-each select="root/content">
                <div class="{contentType}">
                    <xsl:if test="not(preceding::content[contentType = current()/contentType])">
                            <h2><xsl:value-of select="contentType"/></h2>
                    </xsl:if>
                    <ul>
                        <xsl:for-each select="listItems/item">
                            <li><xsl:value-of select="."/></li>
                        </xsl:for-each>
                    </ul>
                </div>
            </xsl:for-each>       
    
        </xsl:template>
    </xsl:stylesheet>
    

    This might be a bit cleaner:

    <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
        version="1.0">
        <xsl:template match="content">
                <div class="{contentType}">
                    <xsl:if test="not(preceding::content[contentType = current()/contentType])">
                            <h2><xsl:value-of select="contentType"/></h2>
                    </xsl:if>
                    <ul>
                       <xsl:apply-templates select="listItems" />
                    </ul>
                </div>
        </xsl:template>
        <xsl:template match="listItem/item">
              <li><xsl:value-of select="."/></li>
        </xsl:template>
    </xsl:stylesheet>
    

    One discrepancy here is that your desired output has the h2 values capitalized, while they will not be in this case. Were you aware of that?

    I think the good practice would be to have the classes and titles as separate values in the source XML, but if you really want to use the same value in both places, you can capitalize the first letter by doing this:

    <h2><xsl:value-of select="concat(translate(substring(contentType, 1, 1), 
       'abcdefghijklmnopqrstuvwxyz', 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'), 
       substring(contentType, 2))"/></h2>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following query, and would like to list only the first match.
I have a route like following, ideally I would like it to match: domain.com/layout/1-slug-is-the-name-of-the-page
I would like 'about' to route to 'abouts/1' I tried this: match 'about' =>
i'm new to regular expressions and would like to match the first and last
I have a Java pattern I would like to match. I want to take
In my Django application, I have a URL I would like to match which
I have a string: var doi_id= 10.3390/metabo1010001 And I would like to match only
I have a string like first url, second url, third url and would like
I would like to match url pattern that has optional segments. I have URL-s
I would like to match a pattern in which two words must be present,

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.