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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T14:02:23+00:00 2026-06-13T14:02:23+00:00

I’ve got an XML file which is something like : <doc> <element> <word> <text>one

  • 0

I’ve got an XML file which is something like :

<doc>
  <element>
    <word>
      <text>one word</text>
    </word>
    <word>
      <text>two words</text>
    </word>
  </element>
  <element>
    <other>
      <text>the sky</text>
    </other>
    <word>
      <text>NN NN</text>
    </word>
  </element>
</doc>

And I’d like to have just one tag and the content of the lines when there are two on after the other, like that :

<element>
    <word>
      <text>one word</text>
       <text>two words</text>
    </word>
</element>

The problem is that I’m generating an <xsl:element> in order to get the <word> tags. And this <xsl:element> is already in a <xsl:for-each> loop that puts the <word> tags in the right order (filtering on an attribute).

My previous XML doc is like that :

<doc>
  <element class="word">
    <text>one word</text>
  </element>
  <element class="word">
    <text>NN NN</text>
  </element>
  <element class="word">
    <text>two words</text>
  </element>
  <element class="other">
    <text>the sky</text>
  </element>
</doc>

Any help would be great, thank you 🙂

— More details —

This is the result I want to have :

<doc>
   <element>
      <word>
         <text>one word</text>
         <text>two words</text>
      </word>
   </element>
   <element>
      <other>
         <text>the sky</text>
      </other>
      <word>
         <text>NN NN</text>
      </word>
  </element>
</doc>

And I can’t specify the tags word or other because I don’t have a closed list of tags. So I’m using xsl:variable :

<xsl:for-each select="element">
    <xsl:variable name="name">
      <xsl:value-of select="@class"/>
    </xsl:variable>
    <xsl:element name="{$name}">
        <text>
          <xsl:value-of select="."/>
        </text>
    </xsl:element>
</xsl:for-each>
  • 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-13T14:02:25+00:00Added an answer on June 13, 2026 at 2:02 pm

    Try this:

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
      <xsl:template match="element/*">
        <xsl:if test="not(preceding-sibling::*[name() = name(current())])">
          <xsl:copy>
            <xsl:apply-templates select="* | following-sibling::*[name()=name(current())]/*" />
          </xsl:copy>
        </xsl:if>
      </xsl:template>
    
      <!-- Identity template -->
      <xsl:template match="@* | node()">
        <xsl:copy>
          <xsl:apply-templates select="@* | node()"/>
        </xsl:copy>
      </xsl:template>
    </xsl:stylesheet>
    

    It simply looks for any child nodes of element that do not have a previous sibling with the same name (i.e., the first of each one), and then applies templates to it’s own child nodes and the child nodes of any following siblings with the same name.

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

Sidebar

Related Questions

I would like to run a str_replace or preg_replace which looks for certain words
For some reason, after submitting a string like this Jack’s Spindle from a text
I've got a string that has curly quotes in it. I'd like to replace
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:
I have an autohotkey script which looks up a word in a bilingual dictionary
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
I'm trying to select an H1 element which is the second-child in its group

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.