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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:20:46+00:00 2026-05-27T23:20:46+00:00

I need to transform xml data into a different format. I have done very

  • 0

I need to transform xml data into a different format. I have done very little XSL stuff and my internet search didn’t quite give me needed results. I have this sample xml:

 <MailStatusReport>
        <Output>
            <ColMetaData ColCount="5">
                <ColList>
                    <Col  Name="parcelid" Pos="1"/>
                    <Col  Name="currentlocationid" Pos="2"/>
                    <Col  Name="deliverystatus" Pos="4"/>
                    <Col  Name="requestedlocationid" Pos="3"/>
                    <Col  Name="requestor" Pos="5"/>
                </ColList>
            </ColMetaData>
            <RowList>
                <Row>
                    <ColList>
                        <Col Pos="2">Delaware</Col>
                        <Col Pos="1">001</Col>
                        <Col Pos="3">NewYork</Col>
                        <Col Pos="4">InRoute</Col>
                        <Col Pos="5">John</Col>

                    </ColList>
                </Row>
                <Row>
                    <ColList>
                        <Col Pos="1">002</Col>
                        <Col Pos="2">Sanjose</Col>
                        <Col Pos="3">Michigan</Col>
                        <Col Pos="4">Delivered</Col>
                        <Col Pos="5">Rob</Col>
                    </ColList>
                </Row>
            </RowList>
        </Output>
</MailStatusReport>

Desired output:

   <MailStatusReport>
        <Row parcelid="001" currentlocationid="Delaware" requestedlocationid="NewYork" deliverystatus="InRoute" requestor="John"/>
        <Row parcelid="002" currentlocationid="Sanjose" requestedlocationid="Michigan" deliverystatus="Delivered" requestor="Rob"/>
  </MailStatusReport> 

Few things to note:

  1. The Name attribute of ColMetaData/ColList/Col in the input xml ends up as an attribute name for each Row element of the output xml.
  2. The value of RowList/Row/ColList/Col in the input xml ends up as the attribute value in the output xml for the attribute which has the same Pos as in the ColMetaData/ColList/Col section.
  3. ColCount attribute of ColMetaData is trust worthy and could be used as a counter if needed.

The Pseudo code I could think of would be:

 For each CurrentRow in the RowList
   Begin Building OutputRow
    For each Col in ColMetaData/ColList
      attrName = ColMetaData/ColList/Col/Name
      attrPos = ColMetaData/ColList/Col/Pos
      attrVal = CurrentRow/ColList/Col[@Pos=$attrPos]
      Add $attrName=$attrVal to the outputRow

I would appreciate any help that I could get!
Thanks
Srinivas

  • 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-05-27T23:20:46+00:00Added an answer on May 27, 2026 at 11:20 pm

    This transformation:

    <xsl:stylesheet version="1.0"
     xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
     <xsl:output omit-xml-declaration="yes" indent="yes"/>
     <xsl:strip-space elements="*"/>
    
     <xsl:key name="kColNameByPos" match="Col/@Name"
      use="../@Pos"/>
    
     <xsl:template match="/*">
      <MailStatusReport>
       <xsl:apply-templates/>
      </MailStatusReport>
     </xsl:template>
    
     <xsl:template match="Row">
      <Row>
        <xsl:apply-templates select="*/*">
          <xsl:sort select="@Pos" data-type="number"/>
        </xsl:apply-templates>
      </Row>
     </xsl:template>
    
     <xsl:template match="Row/ColList/Col">
      <xsl:attribute name="{key('kColNameByPos', @Pos)}">
       <xsl:value-of select="."/>
      </xsl:attribute>
     </xsl:template>
    </xsl:stylesheet>
    

    when applied on the provided XML document:

    <MailStatusReport>
        <Output>
            <ColMetaData ColCount="5">
                <ColList>
                    <Col  Name="parcelid" Pos="1"/>
                    <Col  Name="currentlocationid" Pos="2"/>
                    <Col  Name="deliverystatus" Pos="4"/>
                    <Col  Name="requestedlocationid" Pos="3"/>
                    <Col  Name="requestor" Pos="5"/>
                </ColList>
            </ColMetaData>
            <RowList>
                <Row>
                    <ColList>
                        <Col Pos="2">Delaware</Col>
                        <Col Pos="1">001</Col>
                        <Col Pos="3">NewYork</Col>
                        <Col Pos="4">InRoute</Col>
                        <Col Pos="5">John</Col>
                    </ColList>
                </Row>
                <Row>
                    <ColList>
                        <Col Pos="1">002</Col>
                        <Col Pos="2">Sanjose</Col>
                        <Col Pos="3">Michigan</Col>
                        <Col Pos="4">Delivered</Col>
                        <Col Pos="5">Rob</Col>
                    </ColList>
                </Row>
            </RowList>
        </Output>
    </MailStatusReport>
    

    produces the wanted, correct result:

    <MailStatusReport>
       <Row parcelid="001"
            currentlocationid="Delaware"
            requestedlocationid="NewYork"
            deliverystatus="InRoute"
            requestor="John"/>
       <Row parcelid="002"
            currentlocationid="Sanjose"
            requestedlocationid="Michigan"
            deliverystatus="Delivered"
            requestor="Rob"/>
    </MailStatusReport>
    

    Explanation:

    1. Using <xsl:sort> for ordering the results of the attributes generation.

    2. Using <xsl:key> and the key() function to define a mapping between column name and column position — for more convenient calculating the name of the attribute that is being generated.

    3. Producing the name of attributes from calculated values using AVT (Attribute Value Template).

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

Sidebar

Related Questions

I have the following xslt to transform data into a JQuery-accepted format. However, because
I need to extract data from a .dbf file and transform it into xml.
I need to transform a valid XML document to the OFX v1.0.2 format .
I need some to help oon generating the XSL file for my XML Data.
I need to transform large XML files that have a nested (hierarchical) structure of
We are using XSL to convert a XML file into a pipe-delimited format. <?xml
Very new to XSL (and XML for that matter), but I need to step
I am having some problems with an XML transform and need some help. The
I need to transform an Oracle SQL statement into a Stored Procedure therefore users
I need to transform my nested sets structure (mysql) into json for this spacetree

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.