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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T22:21:59+00:00 2026-06-04T22:21:59+00:00

OK upfront, I’m a newb on PHP and Java, however trying to refresh my

  • 0

OK upfront, I’m a newb on PHP and Java, however trying to refresh my coding after journeying into management for the past ten years.

I have a table in the form:

heading1_sub1_element1 = data1
heading1_sub1_element2 = data2
heading1_sub1_element3 = data3
heading1_sub2_element1 = data4
heading1_sub2_element2 = data5
heading1_sub2_element3 = data6

Using the awesome example at Tony Marsden’s site I have been able to get the table to extract the data into the form:

<table>
    <heading1_sub1_element1>data1</heading1_sub1_element1>
    <heading1_sub1_element2>data2</heading1_sub1_element2>
    <heading1_sub1_element3>data3</heading1_sub1_element3>
    <heading1_sub2_element1>data4</heading1_sub2_element1>
    <heading1_sub2_element2>data5</heading1_sub2_element2>
    <heading1_sub2_element3>data6</heading1_sub2_element3>
</table>

However what I would like to get to is:

<heading1>
    <sub1>
        <element1>Data1</element1>
        <element2>Data2</element2>
        <element3>Data3</element3>
    </sub1>
    <sub2>
        <element1>Data4</element1>
        <element2>Data5</element2>
        <element3>Data6</element3>
    </sub2>
</heading1>

Does anyone have any idea on how to get the data into that format? will I need to use XSLT, or can PHP do this directly?

My only reason for doing this is that the XML looks a whole load better.

Thanks in advance, and any assistance will be greatfully recieved.

  • 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-04T22:22:01+00:00Added an answer on June 4, 2026 at 10:22 pm

    Here is a generic solution that correctly processes any set of lines having the specified format — even if there are different number of underscores on each line and the first “name” isn’t the same on all lines:

    <xsl:stylesheet version="2.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
     xmlns:xs="http://www.w3.org/2001/XMLSchema"
     xmlns:my="my:my" exclude-result-prefixes="my xs">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
    
     <xsl:variable name="vLines" select="tokenize(/*, '\r?\n')[.]"/>
    
     <xsl:variable name="vPass1">
      <t>
       <xsl:apply-templates mode="pass1"/>
      </t>   
     </xsl:variable>
    
    
     <xsl:template match="/*" mode="pass1">
      <xsl:for-each select="$vLines">
       <xsl:sequence select="my:makeTree(normalize-space(.))"/>
      </xsl:for-each>
     </xsl:template>
    
     <xsl:template match="/">
      <xsl:apply-templates select="$vPass1" mode="pass2"/>
     </xsl:template>
    
     <xsl:function name="my:makeTree">
      <xsl:param name="pLine"/>
    
      <xsl:variable name="vName" select="substring-before($pLine, '_')"/>
    
      <xsl:choose>
        <xsl:when test="$vName">
          <xsl:element name="{$vName}">
            <xsl:sequence select="my:makeTree(substring-after($pLine, '_'))"/>
          </xsl:element>
        </xsl:when>
        <xsl:otherwise>
         <xsl:element name=
           "{normalize-space(substring-before($pLine, '='))}">
           <xsl:sequence select="substring-after($pLine, '=')"/>
         </xsl:element>
        </xsl:otherwise>
      </xsl:choose>
     </xsl:function>
    
     <xsl:function name="my:group">
      <xsl:param name="pNodes" as="node()*"/>
    
      <xsl:for-each-group select="$pNodes[self::*]" group-by="name()">
        <xsl:element name="{name()}">
          <xsl:for-each select="current-group()">
             <xsl:sequence select="my:group(node())"/>
          </xsl:for-each>
        </xsl:element>
      </xsl:for-each-group>
      <xsl:copy-of select="$pNodes[not(self::*)]"/>
     </xsl:function>
    
      <xsl:template match="*[not(my:path(.) = preceding::*/my:path(.))]" mode="pass2">
      <xsl:copy>
       <xsl:apply-templates select="//*[my:path(.) = my:path((current()))]/node()"
            mode="pass2"/>
      </xsl:copy>
     </xsl:template>
    
     <xsl:template match="*" mode="pass2"/>
    
     <xsl:template match="/*" mode="pass2" priority="3">
       <xsl:apply-templates mode="pass2"/>
     </xsl:template>
    
     <xsl:function name="my:path" as="xs:string">
      <xsl:param name="pElement" as="element()"/>
    
      <xsl:sequence select=
       "string-join($pElement/ancestor-or-self::*/name(.), '/')"/>
     </xsl:function>
    
    </xsl:stylesheet>
    

    when this transformation is applied on the following XML document (the given lines, wrapped into a top element to make this a well-formed XML document):

    <t>
        heading1_sub1_element1 = data1
        heading1_sub1_element2 = data2
        heading1_sub1_element3 = data3
        heading1_sub2_element1 = data4
        heading1_sub2_element2 = data5
        heading1_sub2_element3 = data6
    </t>
    

    the wanted, correct result is produced:

    <heading1>
       <sub1>
          <element1> data1</element1>
          <element2> data2</element2>
          <element3> data3</element3>
       </sub1>
       <sub2>
          <element1> data4</element1>
          <element2> data5</element2>
          <element3> data6</element3>
       </sub2>
    </heading1>
    

    When applying the same transformation to this, much more complicated XML document:

    <t>
        heading1_sub1_element1 = data1
        heading1_sub1_element2 = data2
        heading1_sub1_element3 = data3
        heading1_sub2_element1 = data4
        heading1_sub2_element2 = data5
        heading1_sub2_element3 = data6
        heading2_sub1_sub2_sub3 = data7
        heading2_sub1_sub2_sub3_sub4 = data8
        heading2_sub1_sub2 = data9
        heading2_sub1 = data10
        heading2_sub1_sub2_sub3 = data11
    </t>
    

    we again get the correct, wanted result:

    <heading1>
       <sub1>
          <element1> data1</element1>
          <element2> data2</element2>
          <element3> data3</element3>
       </sub1>
       <sub2>
          <element1> data4</element1>
          <element2> data5</element2>
          <element3> data6</element3>
       </sub2>
    </heading1>
    <heading2>
       <sub1>
          <sub2>
             <sub3>
                data7
                <sub4> data8</sub4>
                data11
             </sub3>
             data9
          </sub2>
          data10
      </sub1>
    </heading2>
    

    Explanation:

    This is a two-pass processing:

    1. In pass1 we convert the input to an temporary tree that (in the case of the first XML document above) looks like:

    …..

    <t>
       <heading1>
          <sub1>
             <element1> data1</element1>
          </sub1>
       </heading1>
       <heading1>
          <sub1>
             <element2> data2</element2>
          </sub1>
       </heading1>
       <heading1>
          <sub1>
             <element3> data3</element3>
          </sub1>
       </heading1>
       <heading1>
          <sub2>
             <element1> data4</element1>
          </sub2>
       </heading1>
       <heading1>
          <sub2>
             <element2> data5</element2>
          </sub2>
       </heading1>
       <heading1>
          <sub2>
             <element3> data6</element3>
          </sub2>
       </heading1>
    </t>
    

    .2. In the second pass we perform a specific kind of grouping so that we produce the wanted result.

    Note: In this solution we access the input strings as the only text node child of the only element in an XML document. This infact isn’t necessary and I have done so only for convenience. We can read the strings from an external text file using the standard XSLT 2.0 function unparsed-text().

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

Sidebar

Related Questions

Intro Let me apologise upfront for the long question. It is as short as
I apologize upfront for my lack of jquery knowledge. In this website I am
I am a novice programmer and apologize upfront for the complicated question. I am
I have a strong use case for pre-allocating all the memory I need upfront
Is there a simple, upfront method to have FF and IE treat hidden JW
How do you folks retrieve all objects in code upfront? I figure you can
On a dynamic web page I don't know the number of matching elements upfront,
I am trying to compare 2 strings but i just realized that one has
OK, I'll admit upfront this is a mega kludge and that I could definately
I am trying to render a view + layout based on a condition. The

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.