How to Insert an XML tag after a particular tag using XSLT?

KM6895

Hi – I would your help to transform following input XML using XSLT

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
	<Summary>
		<Employee_ID>12345</Employee_ID>
		<Name>Mel Gibson</Name>
	</Summary>
	<Personal>
		<First_Name>Mel</First_Name>
		<Middle_Name>Samuel</Middle_Name>
		<Gender>Male</Gender>
	</Personal>
	<Status>
		<Active>Yes</Active>
		<Base_Location>Boston</Base_Location>
	</Status>
	<Summary_Information>
		<Location>California</Location>
		<Last_Formatted_Name>Samuel Gibson</Last_Formatted_Name>
	</Summary_Information>
</Employee>

Requirements for transforming above XML is:

If Last_Name doesn’t exist in input XML, a node Last_Name should be created right after First_Name node and should hold the value of Last_Formatted_Name

Output should look like below. Please note Last_Name node is created right after First_Name and Last_Formatted_Name node is removed.

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
	<Summary>
		<Employee_ID>12345</Employee_ID>
		<Name>Mel Gibson</Name>
	</Summary>
	<Personal>
		<First_Name>Mel</First_Name>
		<Last_Name>Samuel Gibson</Last_Name>
		<Middle_Name>Samuel</Middle_Name>
		<Gender>Male</Gender>
	</Personal>
	<Status>
		<Active>Yes</Active>
		<Base_Location>Boston</Base_Location>
	</Status>
	<Summary_Information>
		<Location>California</Location>
	</Summary_Information>
</Employee>

I have written following XSLT (with the help of another stackoverflow post). However, Last_Name is being inserted as last tag under ‘Personal’ where as ‘Last_Name’ should be inserted right after ‘First_Name’ tag.

<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:strip-space elements="*" />

    <!-- identity transform -->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!-- modifies the value of <Last_Name> if it exists -->
    <xsl:template match="Last_Name">
        <xsl:copy>
            <xsl:value-of select="../../Summary_Information/Last_Formatted_Name" />
        </xsl:copy>
    </xsl:template>

    <!-- if <Last_Name> does not exist, creates a last name with the appropriate value -->
    <xsl:template match="Personal[not(Last_Name)]">
        <xsl:copy>
            <xsl:apply-templates />
            <Last_Name>
                <xsl:value-of select="../Summary_Information/Last_Formatted_Name" />
            </Last_Name>
        </xsl:copy>
    </xsl:template>

    <!-- removes the node -->
    <xsl:template match="Last_Formatted_Name" />
</xsl:stylesheet>

With above XSLT, I’m getting following result. How should I change my XSLT to insert Last_Name just after First_Name tag.

<?xml version="1.0" encoding="UTF-8"?>
<Employee>
	<Summary>
		<Employee_ID>12345</Employee_ID>
		<Name>Mel Gibson</Name>
	</Summary>
	<Personal>
		<First_Name>Mel</First_Name>
		<Middle_Name>Samuel</Middle_Name>
		<Gender>Male</Gender>
		<Last_Name>Samuel Gibson</Last_Name>
	</Personal>
	<Status>
		<Active>Yes</Active>
		<Base_Location>Boston</Base_Location>
	</Status>
	<Summary_Information>
		<Location>California</Location>
	</Summary_Information>
</Employee>

Daniel Haley

You can apply-templates to First_Name, create the new Last_Name, and then apply-templates to everything else.

Example...

<xsl:template match="Personal[not(Last_Name)]">
  <xsl:copy>
    <xsl:apply-templates select="First_Name"/>
    <Last_Name>
      <xsl:value-of select="../Summary_Information/Last_Formatted_Name" />
    </Last_Name>
    <xsl:apply-templates select="*[not(self::First_Name)]"/>
  </xsl:copy>
</xsl:template>

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Insert new hierarchical tag in XML using XSLT

How to properly remove an XML tag using XSLT

How to get and convert attributes after tag <product to xml | XSLT

How to extract single particular tag value in xml using python and modify

How to add two xml tag values using a for:each in XSLT?

How to output an IMG tag from XML using XSLT

How to dynamically extract a part from XML tag name using XSLT?

How to check if particular tag name equals a value and return the entire data inside the tag from XML using Python

xerces: how to take xml node with a particular tag?

Changing XML element tag using XSLT

Get XML tag value using XSLT

Split XML file based on a tag using XSLT

Get a value of an XML tag using XSLT

Delete a tag from XML file using XSLT if another tag is empty

XSLT:How to Maintain Sequential Order of tag in XML

How to read namespaced tag "<a:a>" from XML in XSLT?

how to replace a particular div tag with a span tag using jsoup

How to insert/replace XML tag in XmlDocument?

SQL - How to insert unique id in XML tag

How to insert data in a XML sub tag

Modifying value of a particular tag in xml

How to limit the Scope of element extraction between the start and end tag of a particular xml element using XPath in Python?

Concat a particular XML tag element content on multiple occurance using xsl

How to modify the content in the xml file except inside a particular tag

How to get particular tag from an xml document in python

How to get all nested tags and text in an xml, inside a particular tag?

How to add the name, if tag is not available in the XML, while converting into the CSV using XSLT?

how to select a complex xml tag containing key value pairs that have a specific string using xslt

How to find an element in xml file and place that inside another tag using XSLT?