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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T21:25:56+00:00 2026-06-15T21:25:56+00:00

EDIT: for those that come to this in the future, this was a poorly

  • 0

EDIT: for those that come to this in the future, this was a poorly written question. It was not what I was after.
This Question may also be of use to you.

So, I have been trying to brush up on my XSLT the past few days. I am very unfamiliar with it, spending most of my past using XQuery to transform my XML. I am stuck on a rather simple problem, but looking around I have not found a clear solution. Simply, I want to split some elements into two depending on its children.

For example, if my XML looks like the following:

<?xml version="1.0" encoding="UTF-8"?>    
<root>
      <p>
         Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip 
         tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
         bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger 
         bacon filet mignon pork chop tail.
         <note.ref id="0001"><super>1</super></note.ref>
         <note id="0001">
           <p>
             You may need to consult a latin butcher. Good Luck.
           </p>
         </note>   
       Pork loin ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail
      hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine   
      beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham 
      hock pork hamburger fatback.
    </p>
    </root>

after I run my xsl I am left with something like the following:

<html>
<body>
   <p>
         Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip 
         tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
         bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger 
         bacon filet mignon pork chop tail.
         <span class="noteRef" id="0001"><sup>1</sup></span>
         <div id="note-0001"> 
           <p>
               You may need to consult a latin butcher. Good Luck.
           </p>
         </div>
           Pork loin ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail
           hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine   
           beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham 
           hock pork hamburger fatback.
   </p>
</body>
</html>

The problem with this is obviously an HTML <p> cannot have a <div> as a child, let a lone another <p> as a grandchild. This is just invalid. A browser, such as chromium, may render the first paragraph ending when it hits the <div>, wrapping, appropriately, the note in its own <p>, but leaving the text after the note orpahened. So that any CSS applied to the <p> will fail to be applied.

How would I split one <p> element into two depending on the elements descendants?

Desired output

  <html>
    <body>
       <p>
             Bacon ipsum dolor sit amet bacon chuck pastrami swine pork rump, shoulder beef ribs doner tri-tip 
             tongue. Tri-tip ground round short ribs capicola meatloaf shank drumstick short loin pastrami t-
             bone. Sirloin turducken short ribs t-bone andouille strip steak pork loin corned beef hamburger 
             bacon filet mignon pork chop tail.
             <span class="noteRef" id="0001"><sup>1</sup></span><
</p>
             <div id="note-0001"> 
               <p>
                   You may need to consult a latin butcher. Good Luck.
               </p>
             </div>
<p>
               Pork loin ribeye bacon pastrami drumstick sirloin, shoulder pig jowl. Salami brisket rump ham, tail
               hamburger strip steak pig ham hock short ribs jerky shank beef spare ribs. Capicola short ribs swine   
               beef meatball jowl pork belly. Doner leberkas short ribs, flank chuck pancetta bresaola bacon ham 
               hock pork hamburger fatback.
       </p>
    </body>
    </html>

I have abstracted my question slightly, so the following XSL of what I have tried could be slightly off.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
    xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xd="http://www.oxygenxml.com/ns/doc/xsl"
    exclude-result-prefixes="xs xd" version="2.0">


<xsl:template match="/">

        <html>
          <body>
             <xsl:apply-templates/>
          </body>
        </html>
</xsl:template>

    <xsl:template match="p">
        <p>
            <xsl:apply-templates/>
        </p>
    </xsl:template


    <xsl:template match="note.ref">
        <span class="noteRef" id="{@id}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>

    <xsl:template match="super">
        <sup>
            <xsl:apply-templates/>
        </sup>
    </xsl:template>

    <xsl:template match="note">
          <div id="note-{@id}">
            <xsl:apply-templates/>
        </div>
    </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-15T21:25:57+00:00Added an answer on June 15, 2026 at 9:25 pm

    Assuming an XSLT 2.0 processor I think using for-each-group can help:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      exclude-result-prefixes="xs"
      version="2.0">
    
    <xsl:output method="html" indent="yes" version="5.0"/>
    
    <xsl:template match="/">
      <html>
        <body>
          <xsl:apply-templates/>
        </body>
      </html>
    </xsl:template>
    
    <xsl:template match="p[not((.//p, .//div))]">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="p[.//p, .//div]">
      <xsl:for-each-group select="node()" group-adjacent="boolean((self::text(), self::note.ref))">
        <xsl:choose>
          <xsl:when test="current-grouping-key()">
            <p>
              <xsl:apply-templates select="current()/@*, current-group()"/>
            </p>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:template>
    
    <xsl:template match="note.ref">
        <span class="noteRef" id="{@id}">
            <xsl:apply-templates/>
        </span>
    </xsl:template>
    
    <xsl:template match="super">
        <sup>
            <xsl:apply-templates/>
        </sup>
    </xsl:template>
    
    <xsl:template match="note">
          <div id="note-{@id}">
            <xsl:apply-templates/>
        </div>
    </xsl:template>
    
    </xsl:stylesheet>
    

    The patterns p[not((.//p, .//div))] and p[.//p, .//div] and the group-adjacent expression boolean((self::text(), self::note.ref)) might need to be extended to cover other types of nodes you expect in the input and that need the same processing.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

EDIT: for those who come here with a similar problem, now i know this
Sometimes I come across this problem where you have a set of functions that
This might be considered too non-specific a question, but I'm sure others have come
My current solution will suck sometimes EDIT For those who don't understand,see this example:
EDIT 07/14 As Bill Burgess mentionned in a comment of his answer, this question
I'm not sure if this is the best place for this question, if not
Edit for those who say to use tab control I would love to use
I have come across the need to reorder a variadic list of parameters that
let us assume that I have a reusable business layer that further makes use
We have complex template classes that have some methods which will not work with

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.