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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:03:36+00:00 2026-06-18T02:03:36+00:00

Given an input like the following: <p>Some information about the proceeding source listing:</p> <code

  • 0

Given an input like the following:

<p>Some information about the proceeding source listing:</p>
<code language="CSharp"><![CDATA[ ... ]]></code>
<code language="AnotherLanguage"><![CDATA[ ... ]]></code>

<p>This is a different example which perhaps applies to just one language:</p>
<code language="CSharp"><![CDATA[ ... ]]></code>

<p>Another example:</p>
<code language="CSharp"><![CDATA[ ... ]]></code>
<code language="AnotherLanguage"><![CDATA[ ... ]]></code>
<code language="YetAnotherLanguage"><![CDATA[ ... ]]></code>

Using XSLT 1.0, how can I group the adjacent <code> element as follows:

<p>Some information about the proceeding source listing:</p>
<div class="source-selector">
    <ul class="tabs">
        <li class="tab" data-language="CSharp">CSharp</li>
        <li class="tab" data-language="AnotherLanguage">AnotherLanguage</li>
    </ul>
    <div data-language="CSharp">
        <pre>...</pre>
    </div>
    <div data-language="AnotherLanguage">
        <pre>...</pre>
    </div>
</div>

<p>This is a different example which perhaps applies to just one language:</p>
<div class="source-selector">
    <ul class="tabs">
        <li class="tab" data-language="CSharp">CSharp</li>
    </ul>
    <div data-language="CSharp">
        <pre>...</pre>
    </div>
</div>

<p>Another example:</p>
<div class="source-selector">
    <ul class="tabs">
        <li class="tab" data-language="CSharp">CSharp</li>
        <li class="tab" data-language="AnotherLanguage">AnotherLanguage</li>
        <li class="tab" data-language="YetAnotherLanguage">YetAnotherLanguage</li>
    </ul>
    <div data-language="CSharp">
        <pre>...</pre>
    </div>
    <div data-language="AnotherLanguage">
        <pre>...</pre>
    </div>
    <div data-language="YetAnotherLanguage">
        <pre>...</pre>
    </div>
</div>

This is what I have at the moment which does not work properly because all source code is grouped into one single selector after all other content (like paragraphs in this example).

<!-- Display paragraphs first -->
<xsl:apply-templates select="*[not(name() = 'code')]"/>

<!-- Display consecutive source code within selector -->
<div class="source-selector">
    <ul class="tabs">
    <xsl:for-each select="code">
        <li class="tab" data-language="{@language}"><include item="{@language}Label"/></li>
    </xsl:for-each>
    </ul>
<xsl:for-each select="code">
    <div data-language="{@language}">
        <pre><xsl:copy-of select="node()"/></pre>
    </div>
</xsl:for-each>
</div>
  • 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-18T02:03:37+00:00Added an answer on June 18, 2026 at 2:03 am

    You can accomplish this in XSLT 1.0 by having templates hop to elements’ next neighbors one at a time, like this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:output method="xml" indent="yes"/>
    
      <xsl:template match="/*">
        <div>
          <xsl:apply-templates select="*[not(self::code)]" />
        </div>
      </xsl:template>
    
      <xsl:template match="/*/*[not(self::code)]">
        <xsl:copy-of select="."/>
        <!-- Select the next neighbor element, but only if it is a <code> -->
        <xsl:variable name="firstCode" select="following-sibling::*[1][self::code]" />
        <div class="source-selector">
          <!-- Skip the part with the <ul> if there are no <code> neighbors-->
          <xsl:if test="$firstCode">
            <ul>
              <xsl:apply-templates select="$firstCode" mode="list" />
            </ul>
            <xsl:apply-templates select="$firstCode" mode="samples" />
          </xsl:if>
        </div>
      </xsl:template>
    
      <xsl:template match="code" mode="list">
        <li class="tab" data-language="{@language}">
          <xsl:value-of select="@language"/>
        </li>
        <!-- Apply this template to the next neighbor, if it is a <code> -->
        <xsl:apply-templates select="following-sibling::*[1][self::code]" mode="list" />
      </xsl:template>
    
      <xsl:template match="code" mode="samples">
        <div data-language="{@language}">
          <pre>
            <xsl:value-of select="string(.)"/>
          </pre>
        </div>
        <!-- Apply this template to the next neighbor, if it is a <code> -->
        <xsl:apply-templates 
                           select="following-sibling::*[1][self::code]" mode="samples" />
      </xsl:template>
    
    </xsl:stylesheet>
    

    When run on your sample input, this produces:

    <div>
      <p>Some information about the proceeding source listing:</p>
      <div class="source-selector">
        <ul>
          <li class="tab" data-language="CSharp">CSharp</li>
          <li class="tab" data-language="AnotherLanguage">AnotherLanguage</li>
        </ul>
        <div data-language="CSharp">
          <pre> ... </pre>
        </div>
        <div data-language="AnotherLanguage">
          <pre> ... </pre>
        </div>
      </div>
      <p>This is a different example which perhaps applies to just one language:</p>
      <div class="source-selector">
        <ul>
          <li class="tab" data-language="CSharp">CSharp</li>
        </ul>
        <div data-language="CSharp">
          <pre> ... </pre>
        </div>
      </div>
      <p>Another example:</p>
      <div class="source-selector">
        <ul>
          <li class="tab" data-language="CSharp">CSharp</li>
          <li class="tab" data-language="AnotherLanguage">AnotherLanguage</li>
          <li class="tab" data-language="YetAnotherLanguage">YetAnotherLanguage</li>
        </ul>
        <div data-language="CSharp">
          <pre> ... </pre>
        </div>
        <div data-language="AnotherLanguage">
          <pre> ... </pre>
        </div>
        <div data-language="YetAnotherLanguage">
          <pre> ... </pre>
        </div>
      </div>
    </div>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Can someone direct me to some information about how to do the following: I
The following piece of code was given to us from our instructor so we
I'm trying to create some divs, with user input values like amount and size.
I have been given a task of inputting some student data, like option 1:
I've written some code to implement an algorithm that takes as input a vector
Given the following input against 60% toleration STACKOVERflow is a quesTions and ANSwers weBSITE
Given a table of input data I would like to have a set o
I want to give integer input to Ruby like this: 12 343 12312 12312
Is it possible to read alphanumeric characters only from the given input line ignoring
I need to retrieve a regex pattern matched strings from the given input. Lets

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.