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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:29:33+00:00 2026-06-10T20:29:33+00:00

I have a pipe delimited text file as shown below, which I need to

  • 0

I have a pipe delimited text file as shown below, which I need to transform into a well formed xml structure (example shown below) using xsl. The xsl below is my (latest) attempt at solving this – however I cannot seem to find a way to encapsulate the level 002 elements in level 001, i.e. maintain the parent-child relationship, when iterating through the file line by line. Could anyone help here ?

Pipe delimited file – input

001|XXX|YYY
002|AAA|BBB
002|CCC|DD
001|EEF|XXX
002|HHH|GGG

XML File – desired output

<root>
   <level001>
            <elem name="field1">001</elem>
            <elem name="field2">XXX</elem>
            <elem name="field3">YYY</elem>
            <level002>
                           <elem name="field1">002</elem>
                           <elem name="field2">AAA</elem>
                           <elem name="field3">BBB</elem>
             </level002>
             <level002>
                        <elem name="field1">002</elem>
                        <elem name="field2">CCC</elem>
                        <elem name="field3">DD</elem>
              </level002>
    </level001>
    <level001>
                 <elem name="field1">001</elem>
                 <elem name="field2">XXX</elem>
                <elem name="field3">YYY</elem>
                <level002>
                         <elem name="field1">002</elem>
                         <elem name="field2">HHH</elem>
                         <elem name="field3">GG</elem>
               </level002>
    </level001>
</root>

Current XSL

<xsl:variable name="Cols">
<col>field1,1</col>
<col>field2,2</col>
<col>field3,3</col> 
</xsl:variable>


 <xsl:template match="/" name="main">
<xsl:choose>
    <xsl:when test="unparsed-text-available($pathToCSV, $encoding)">
       <xsl:variable name="csv" select="unparsed-text($pathToCSV, $encoding)" />
       <xsl:variable name="lines" select="tokenize($csv, '\n')" as="xs:string+" />
       <root>
       <xsl:for-each select="$lines[position() &gt; 0]">
        <xsl:if test="translate(., '&#160; &#9;&#10;&#13;',  '') != ''">
            <level001>
            <xsl:variable name="line" select="." />
            <xsl:variable name="columns" select="tokenize(.,'\|')" as="xs:string+"/>    
            <xsl:choose>
                <xsl:when test="$columns[1]='001'">
                    <xsl:for-each select="$Cols/col">
                        <xsl:variable name="column" select="number(substring-after(.,','))"/>
                        <elem name="{substring-before(.,',')}">
                            <!-- trims the whitespace from the beginning and the ending of the value -->
                            <xsl:value-of select="replace(replace($columns[$column],'\s+$',''),'^\s+','')"/>
                        </elem>
                    </xsl:for-each>
                </xsl:when>
                <xsl:when test="$columns[1]='002'">
                    <level002>
                    <xsl:for-each select="$Cols/col">
                        <xsl:variable name="column" select="number(substring-after(.,','))"/>
                        <elem name="{substring-before(.,',')}">
                            <!-- trims the whitespace from the beginning and the ending of the value -->
                            <xsl:value-of select="replace(replace($columns[$column],'\s+$',''),'^\s+','')"/>
                        </elem>
                    </xsl:for-each>
                    </level002>
                </xsl:when>
            </xsl:choose>                               
            </level001>
        </xsl:if>
       </xsl:for-each>
       </root>
    </xsl:when>         
</xsl:choose>
  • 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-10T20:29:35+00:00Added an answer on June 10, 2026 at 8:29 pm

    I would first transform the flat text into a flat XML structure and then group that with for-each-group group-starting-with, as in the following code sample:

    <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="mf xs"
      version="2.0">
    
    <xsl:param name="text-url" as="xs:string" select="'test2012090401.txt'"/>
    <xsl:param name="sep" as="xs:string" select="'\|'"/>
    <xsl:param name="field" as="xs:string" select="'field'"/>
    
    <xsl:output indent="yes"/>
    
    <xsl:function name="mf:group" as="node()*">
      <xsl:param name="nodes" as="node()*"/>
      <xsl:param name="level" as="xs:integer"/>
      <xsl:for-each-group select="$nodes" group-starting-with="line[xs:integer(elem[1]) eq $level]">
        <xsl:element name="level{*[1]}">
          <xsl:copy-of select="*"/>
          <xsl:sequence select="mf:group(current-group() except ., $level + 1)"/>
        </xsl:element>
      </xsl:for-each-group>
    </xsl:function>
    
    <xsl:template name="main">
      <xsl:variable name="flat">
        <xsl:for-each select="tokenize(unparsed-text($text-url), '\r?\n')">
          <line>
            <xsl:for-each select="tokenize(., $sep)">
              <elem name="{$field}{position()}">
                <xsl:value-of select="."/>
              </elem>
            </xsl:for-each>
          </line>
        </xsl:for-each>
      </xsl:variable>
      <root>
        <xsl:sequence select="mf:group($flat/line, 1)"/>
      </root>
    </xsl:template>
    
    </xsl:stylesheet>
    

    When I apply that stylesheet with Saxon 9 using java -jar saxon9he.jar -it:main -xsl:sheet.xsl, the result I get is

    <?xml version="1.0" encoding="UTF-8"?>
    <root>
       <level001>
          <elem name="field1">001</elem>
          <elem name="field2">XXX</elem>
          <elem name="field3">YYY</elem>
          <level002>
             <elem name="field1">002</elem>
             <elem name="field2">AAA</elem>
             <elem name="field3">BBB</elem>
          </level002>
          <level002>
             <elem name="field1">002</elem>
             <elem name="field2">CCC</elem>
             <elem name="field3">DD</elem>
          </level002>
       </level001>
       <level001>
          <elem name="field1">001</elem>
          <elem name="field2">EEF</elem>
          <elem name="field3">XXX</elem>
          <level002>
             <elem name="field1">002</elem>
             <elem name="field2">HHH</elem>
             <elem name="field3">GGG</elem>
             <level/>
          </level002>
       </level001>
    </root>
    

    The stylesheet has a parameter named text-url to the plain text file you can set when running the stylesheet.

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

Sidebar

Related Questions

I have a pipe delimited feed file which has several fields. Since I only
I have the need to parse through a large pipe-delimited file to count the
I am trying to save a pipe delimited text file into an excel worksheet
I have a pipe delimited file as below 1 |Mike | 2000| 2| 2
Here's the situation, I have a text file that is pipe-delimited and one of
I have a form that will ultimately create a pipe delimited text file. The
I have txt pipe-delimited file (non-english - given below is Polish, but other langs
hi guys i have two pipe delimited files,first file contains 1000 records and second
I have a pipe to gnuplot which I use to graph a file. The
I have the following file (above) which seems to be an Unix pipe alt

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.