John Go
John Go

Reputation: 93

How to insert an element into a previously created element in xslt?

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.

Upvotes: 2

Views: 1373

Answers (2)

Dimitre Novatchev
Dimitre Novatchev

Reputation: 243579

As short and easy as this:

<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:template match="/*">
  <xsl:apply-templates select="XML_FILE_HEADER"/>
 </xsl:template>

 <xsl:template match="XML_FILE_HEADER">
   <File FileName="{@file_name}">
     <xsl:apply-templates select="../*[not(self::XML_FILE_HEADER)]"/>
   </File>
 </xsl:template>

 <xsl:template match="XML_RECORD">
   <Record name="{@name}" Age="{@Age}"/>
 </xsl:template>

 <xsl:template match="XML_FILE_FOOTER">
   <Totals TotalRecords="{@total_records}"/>
 </xsl:template>
</xsl:stylesheet>

When this transformation is applied on the provided XML (corrected to be well-formed) document:

<t>
    <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"/>
</t>

the wanted, correct result is produced:

<File FileName="sample.txt">
   <Record name="John Doe" Age="21"/>
   <Record name="Jessica Sanchez" Age="23"/>
   <Totals TotalRecords="2"/>
</File>

Explanation:

  1. Proper use of templates.

  2. Proper use of xsl:apply-templates for ordering the results.

  3. Proper use of AVT (Attribute Value Templates).

  4. Avoided the use of xsl:element

  5. No use of xsl:call-template.

  6. Implemented in "push style" almost completely.

Upvotes: 2

Jaimal Chohan
Jaimal Chohan

Reputation: 8645

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>

Upvotes: 0

Related Questions