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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T14:43:05+00:00 2026-06-10T14:43:05+00:00

I have several records from the DB for a corresponding record in a file.

  • 0

I have several records from the DB for a corresponding record in a file.

Example
Record no. XML

  1. <XML_FILE_HEADER file_name="sample.txt" />
  2. <XML_RECORD record_number="1" name="John Doe" Age="21"/>
  3. <XML_RECORD record_number="2" name""Jessica Sanchez" Age="23"/>
  4. <XML_FILE_FOOTER total_records="2"/>

Now for each record I have an xslt template that would create the output file in xml.

For record no 1:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
<xsl:output method="xml"/>
<xsl:template match="XML_FILE_HEADER">
    <xsl:element name="File">
    <xsl:attribute name="FileName"><xsl:value-of select="@file_name"/></xsl:attribute>
    </xsl:element>
  </xsl:element>
</xsl:template>
</xsl:stylesheet>

For records 2 and 3:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
     <xsl:output omit-xml-declaration="yes"/>
     <xsl:template match="XML_RECORD">
       <xsl:element name="Record">
        <xsl:attribute name="Name"><xsl:value-of select="@name"/></xsl:attribute>
        <xsl:element name="Details">
        <xsl:attribute name="Age"><xsl:value-of select="@Age"/></xsl:attribute>
        </xsl:element>
      </xsl:element>
     </xsl:template>
</xsl:stylesheet>

For record 4:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
     <xsl:output omit-xml-declaration="yes"/>
     <xsl:template match="XML_FILE_FOOTER">
       <xsl:element name="Totals">
        <xsl:attribute name="Total Records"><xsl:value-of select="@total_records"/></xsl:attribute>
      </xsl:element>
     </xsl:template>
</xsl:stylesheet>

The problem with is is I would have an output of this after appending each record using the templates above:

<?xml version="1.0" encoding="UTF-8"?>
<File FileName="sample.txt"></File>
<Record Name="John Doe" Age="21"></Record>
<Record Name="Jessica Sanchez" Age="22"></Record>
<Totals Total Records="2"></Totals>

How would I be able to insert the Record and Totals elements under File? so that it would have an output like this:

<?xml version="1.0" encoding="UTF-8"?>
<File FileName="sample.txt">
<Record Name="John Doe" Age="21"></Record>
<Record Name="Jessica Sanchez" Age="22"></Record>
<Totals Total Records="2"></Totals>
</File>

Any help would be very much appreciated. Thanks.

  • 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-10T14:43:06+00:00Added an answer on June 10, 2026 at 2:43 pm

    What you want is the <xsl:call-template name="templatename" /> element. This allows you to call a template from inside another template.

    Something like

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:xalan="http://xml.apache.org/xslt">
        <xsl:output method="xml"/>
        <xsl:template match="/XML_FILE/XML_FILE_HEADER">
          <xsl:element name="File">
            <xsl:attribute name="FileName">
              <xsl:value-of select="@file_name"/>
            </xsl:attribute>
            <xsl:for-each select="/XML_FILE/XML_RECORD">
              <xsl:call-template name="RecordTemplate" />
            </xsl:for-each>        
            <xsl:call-template name="TotalTemplate" />
          </xsl:element>
        </xsl:template>
    
        <xsl:template name="RecordTemplate">
          <xsl:element name="Record">
            <xsl:attribute name="Name"><xsl:value-of select="@name"/></xsl:attribute>
            <xsl:attribute name="Age"><xsl:value-of select="@Age"/></xsl:attribute>
          </xsl:element>
        </xsl:template>
    
        <xsl:template match="/XML_FILE/XML_FILE_FOOTER" name="TotalTemplate">
          <xsl:element name="Totals">
              <xsl:attribute name="Total Records"><xsl:value-of select="@total_records"/>
          </xsl:element>
        </xsl:template>
    </xsl:stylesheet>
    

    of course your input you have to be XML valid (i.e. have a single root node) like so

    <XML_FILE>
      <XML_FILE_HEADER file_name="sample.txt" />
      <XML_RECORD record_number="1" name="John Doe" Age="21"/>
      <XML_RECORD record_number="2" name""Jessica Sanchez" Age="23"/>
      <XML_FILE_FOOTER total_records="2"/>
    </XML_FILE>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a large disk file (around 8 GB) containing several million records that
I have several fairly large XML files that represent data exported from a system
I have a query that is supposed to pull records from several tables using
I have a sitution where I need to import records from a flat file
I have rake tasks importing thousands of records from several different sources and formats
I have a mysql table that have several records linked to one particular tag.
I have this query that, due to the number of records, is taking several
I have a view responsible for recording audio. How can i record several audios
I have several xml files with different node structure. I want to extract xml
I have several xml files that are formated this way: <ROOT> <OBJECT> <identity> <id>123</id>

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.