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

  • Home
  • SEARCH
  • 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 9080769
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T20:09:01+00:00 2026-06-16T20:09:01+00:00

Is it possible to convert this <boxed-text> <para role=Box legend>Box 2 Caption</para> <para role=Box

  • 0

Is it possible to convert this

<boxed-text>
<para role="Box legend">Box 2 Caption</para>
<para role="Box head">Text Text Text</para>
<para role="Box text">Text Text Text.<sup>1</sup></para>
<para role="Box subhead A">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
<para role="Box subhead A">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
<para role="Box subhead B">Text Text Text</para>
<para role="Box text">Text Text Text.</para>
</boxed-text>

into something like this?

<boxed-text>
<caption><para>Box 2 Caption</para></caption>
<para>Text Text Text</para>
<para>Text Text Text.<sup>1</sup></para>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
</sec>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
<sec>
<title>Text Text Text</title>
<para>Text Text Text.</para>
</sec>
</sec>
</boxed-text>

However, subheads may not appear, thus,

<boxed-text>
<para role="Box legend">Box 2 Caption</para>
<para role="Box head">Text Text Text</para>
<para role="Box text">Text Text Text.<sup>1</sup></para>
<para role="Box text">Text Text Text.</para>
<para role="Box text">Text Text Text.</para>
<para role="Box text">Text Text Text.</para>
</boxed-text>

should produce

<boxed-text>
<caption><para>Box 2 Caption</para></caption>
<para>Text Text Text</para>
<para>Text Text Text.<sup>1</sup></para>
<para>Text Text Text.</para>
<para>Text Text Text.</para>
<para>Text Text Text.</para>
</boxed-text>

I am having a hard time getting this done with xsl:for-each-group. An answer will be much appreciated. Thanks in advance!

  • 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-16T20:09:03+00:00Added an answer on June 16, 2026 at 8:09 pm

    Here is my edited suggestion, incorporating a slight change to make sure we process all para elements when there is nothing to group:

    <xsl:stylesheet
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
      xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:mf="http://example.com/mf"
      exclude-result-prefixes="xs mf"
      version="2.0">
    
    <xsl:param name="group-role" select="'Box subhead '"/>
    
    <xsl:output indent="yes"/>
    
    <xsl:function name="mf:group" as="element()*">
      <xsl:param name="paras" as="element(para)*"/>
      <xsl:param name="head" as="xs:string"/>
      <xsl:for-each-group select="$paras" group-starting-with="para[@role = concat($group-role, $head)]">
        <xsl:choose>
          <xsl:when test="self::para[@role = concat($group-role, $head)]">
            <sec>
              <xsl:apply-templates select="."/>
              <xsl:sequence select="mf:group(current-group() except ., codepoints-to-string(string-to-codepoints($head)[1] + 1))"/>
            </sec>
          </xsl:when>
          <xsl:otherwise>
            <xsl:apply-templates select="current-group()"/>
          </xsl:otherwise>
        </xsl:choose>
      </xsl:for-each-group>
    </xsl:function>
    
    <xsl:template match="@* | node()">
      <xsl:copy>
        <xsl:apply-templates select="@* , node()"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="boxed-text">
      <xsl:copy>
        <xsl:variable name="first-sh" select="para[@role = concat($group-role, 'A')][1]"/>
        <xsl:apply-templates select="if ($first-sh) then $first-sh/preceding-sibling::para else para"/>
        <xsl:sequence select="mf:group(($first-sh, $first-sh/following-sibling::para), 'A')"/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="para[matches(@role, concat($group-role, '[A-Z]'))]">
      <title>
        <xsl:apply-templates/>
      </title>
    </xsl:template>
    
    <xsl:template match="para[@role = 'Box legend']">
      <caption>
        <para>
          <xsl:apply-templates/>
        </para>
      </caption>
    </xsl:template>
    
    <xsl:template match="para[@role = ('Box head', 'Box text')]">
      <para>
        <xsl:apply-templates/>
      </para>
    </xsl:template>
    
    </xsl:stylesheet>
    

    When applied to the input

    <boxed-text>
      <para role="Box legend">Box 2 Caption</para>
      <para role="Box head">Text Text Text</para>
      <para role="Box text">Text Text Text.<sup>1</sup></para>
      <para role="Box subhead A">Text Text Text</para>
      <para role="Box text">Text Text Text.</para>
      <para role="Box subhead A">Text Text Text</para>
      <para role="Box text">Text Text Text.</para>
      <para role="Box subhead B">Text Text Text</para>
      <para role="Box text">Text Text Text.</para>
    </boxed-text>
    

    with Saxon 9.4 I get the result

    <boxed-text>
       <caption>
          <para>Box 2 Caption</para>
       </caption>
       <para>Text Text Text</para>
       <para>Text Text Text.<sup>1</sup>
       </para>
       <sec>
          <title>Text Text Text</title>
          <para>Text Text Text.</para>
       </sec>
       <sec>
          <title>Text Text Text</title>
          <para>Text Text Text.</para>
          <sec>
             <title>Text Text Text</title>
             <para>Text Text Text.</para>
          </sec>
       </sec>
    </boxed-text>
    

    When applied to the input

    <boxed-text>
    <para role="Box legend">Box 2 Caption</para>
    <para role="Box head">Text Text Text</para>
    <para role="Box text">Text Text Text.<sup>1</sup></para>
    <para role="Box text">Text Text Text.</para>
    <para role="Box text">Text Text Text.</para>
    <para role="Box text">Text Text Text.</para>
    </boxed-text>
    

    I get the result

    <boxed-text>
       <caption>
          <para>Box 2 Caption</para>
       </caption>
       <para>Text Text Text</para>
       <para>Text Text Text.<sup>1</sup>
       </para>
       <para>Text Text Text.</para>
       <para>Text Text Text.</para>
       <para>Text Text Text.</para>
    </boxed-text>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to convert this if statement into a one line statement ?
Is it possible to convert this code into one LINQ query. I could have
I'm wondering if is possible to convert this HQL query into a criteria api
Is it possible to convert this function, list comprehension combination into a single list
Possible Duplicate: Convert multidimensional array into single array I have this array: Array (
Is it possible to convert this java code snippet to php? public void testBalanceReturnsToZeroOnVending()
Is it possible to convert HTML + CSS into HTML for a system that
Is it possible to convert facebook data (ex. user_pic) into a DisplayObject so I
can somebody please explain is it possible to convert this snippet of the code
Possible Duplicate: Convert this string to datetime I have to convert my string value

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.