Updated April 17, 2023
Introduction to XSLT mapping
XSLT Mapping converts the source structure into the specific target structure. It maps one or two source input files to any output. It’s a tool with the possible advantage of using Interfaces. It allows to easily create XSLT templates to transform an input XML schema into an output XML schema. XSLT transforms the source data, which can be exchanged between different business management.
Syntax
The Syntax for the XSLT mapping is given as
<?xml version="1.0" encoding="UTF-8" ?>
<?oracle-xsl-mapper>
<mapSources>
<source type="XML">
<schema location="../xxxx.xml"/>
<rootElement name="aaaaa" namespace="http://www.educba.org"/>
</source>
</mapSources>
<mapTargets>
How mapping work in XSLT?
The term mapping refers dynamic transformation of one application to another application that represents different schema schedules. XSLT serves as a mediator for mapping sources to target in a flexible manner. A very simple example to narrate the definition of XSLT mapping. We normally receive a particular invoice for the product from the user in one format, but we transform this format to the system’s structure. To put the message in context, let’s see the next sections.
In this article, first, I have used XCS Data Mapper, in which XSLT is generated using this mapper by simply dragging the Source elements. The tool which we use perhaps generates a mapping, which is an added advantage to XSLT. The structure has a source format, logical structure, and other relationships between a source and the destination document. Below are the steps we normally follow to work with the syntax of XSL to link between the nodes. The sample is shown below.
The Next Step is to select the format with the reader Description. Adding to it, we include XSLT, which is fully editable, and all the editing process is reflected in the mapping tab.
The second Mapper we use is Stylus Studio XSLT Mapper that does visual model and Source Code edit. It takes input from any of the sources like Instance Documents, DTDs, Web services, or any other Databases. Other beautify is it supports Multiple Data Sources.
Even XSLT mapping could be implemented with java. let’s see now:
The steps include:
- Implement a class with static methods for the respective documents. Inside that, import the message as a tracking Object.
- Give a method call in the program.
Let’s take a Source file as
<vehicle>
<vehicle_name> Roy cccc </vehicle name>
<vehicle_add> USA </vehicle_add>
</vehicle>
And now the target instance would be
<vehicle>
<name> Roy cccc USA </name>
</vehicle>
Using concat() method we can merge the two strings for the target file. And the mapping declares the value param for the input.
public static String concat(String name, String add, Map inputparam) { // give information trace = (AbstractTrace)inputparam.get( StreamTransformationConstants.MAPPING_TRACE );
trace.addInfo("concat(): \nvehicle-name = " + name + "\n vehicle_add = " + add );
// produce concat result
return name + ' ' + add ; }
//...
}
In the next section, we shall see how to map files without repeating XML elements and saved them in the same location to access. We have different mapping tools like Altova, Oracle data Manager for B2B, Denodo XSLT mapper, and Stylus Studio. Here I have executed the implementation in XSLT mapper Open Source.
Examples of XSLT mapping
Different examples are mentioned below:
Example #1
XML code as the source input
<?xml version="1.0" encoding="UTF-8"?>
<Western>
<Scandivian>
<name>Denmark</name>
<code>DEN</code>
</Scandivian>
<Scandivian>
<name>Norway</name>
<code>Nry</code>
</Scandivian>
<Scandivian>
<name>Sweden</name>
<code>Swd</code>
</Scandivian>
<Scandivian>
<name>Switzerland</name>
<code>Swiss</code>
</Scandivian>
</Western>
The XSL stylesheet is given as
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common" extension-element-prefixes="exsl">
<xsl:template match="/">
<xsl:variable name="westList">
;DEN=Denmark;ALB=Albania;DZA=Algeria;AND=Andorra;AGO=Angola;ATG=Antigua and Barbuda;ARG=Argentina;ARM=Armenia;AUS=Australia;AUT=Austria;AZE=Azerbaijan;BHS=Bahamas, The;BHR=Bahrain;BGD=Bangladesh;BRB=Barbados;BLR=Belarus;BEL=Belgium;BLZ=Belize;BEN=Benin;BTN=Bhutan;BOL=Bolivia;BIH=Bosnia and Herzegovina;BWA=Botswana;BRA=Brazil;BRN=Brunei;BGR=Bulgaria;BFA=Burkina Faso;BDI=Burundi;KHM=Cambodia;CMR=Cameroon;CAN=Canada;CPV=Cape Verde;CAF=Central African Republic;TCD=Chad;CHL=Chile;CHN=China, People's Republic of;COL=Colombia;COM=Comoros;COD=Congo, Democratic Republic of the (Congo – Kinshasa);COG=Congo, Republic of the (Congo – Brazzaville);CRI=Costa Rica;DEU=Germany;GHA=Ghana;GRC=Greece;GRD=Grenada;GTM=Guatemala;GIN=Guinea;GNB=Guinea-Bissau;GUY=Guyana;HTI=Haiti;HND=Honduras;HUN=Hungary;ISL=Iceland;IND=India;IDN=Indonesia;IRN=Iran;IRQ=Iraq;IRL=Ireland;ISR=Israel;ITA=Italy;JAM=Jamaica;JPN=Japan;JOR=Jordan;KAZ=Kazakhstan;KEN=Kenya;KIR=Kiribati;PRK=Korea, Democratic People's Republic of (North Korea);KOR=Korea, Republic of (South Korea);KWT=Kuwait;KGZ=Kyrgyzstan;LAO=Laos;LVA=Latvia;LBN=Lebanon;LSO=Lesotho;LBR=Liberia;LBY=Libya;LIE=Liechtenstein;LTU=Lithuania;LUX=Luxembourg;MKD=Macedonia;MDG=Madagascar;MWI=Malawi;MYS=Malaysia;MDV=Maldives;MLI=Mali;MLT=Malta;MHL=Marshall Islands;MRT=Mauritania;MUS=Mauritius;MEX=Mexico;FSM=Micronesia;MDA=Moldova;MCO=Monaco;MNG=Mongolia;MNE=Montenegro;MAR=Morocco;MOZ=Mozambique;MMR=Myanmar (Burma);NAM=Namibia;NRU=Nauru;NPL=Nepal;NLD=Netherlands;NZL=New Zealand;NIC=Nicaragua;NER=Niger;NGA=Nigeria;NOR=Norway;OMN=Oman;PAK=Pakistan;PLW=Palau;PAN=Panama;PNG=Papua New Guinea;PRY=Paraguay;PER=Peru;PHL=Philippines;POL=Poland;PRT=Portugal;QAT=Qatar;ROU=Romania;RUS=Russia;RWA=Rwanda;KNA=Saint Kitts and Nevis;LCA=Saint Lucia;VCT=Saint Vincent and the Grenadines;WSM=Samoa;SMR=San Marino;STP=Sao Tome and Principe;SAU=Saudi Arabia;SEN=Senegal;SRB=Serbia;SYC=Seychelles;SLE=Sierra Leone;SGP=Singapore;SVK=Slovakia;SVN=Slovenia;SLB=Solomon Islands;SOM=Somalia;ZAF=South Africa;ESP=Spain;LKA=Sri Lanka;SDN=Sudan;SUR=Suriname;SWZ=Swaziland;SWE=Sweden;CHE=Switzerland;SYR=Syria;TJK=Tajikistan;TZA=Tanzania;THA=Thailand;TLS=Timor-Leste (East Timor);TGO=Togo;TON=Tonga;TTO=Trinidad and Tobago;TUN=Tunisia;TUR=Turkey;TKM=Turkmenistan;TUV=Tuvalu;UGA=Uganda;UKR=Ukraine;ARE=United Arab Emirates;GBR=United Kingdom;USA=United States;URY=Uruguay;UZB=Uzbekistan;VUT=Vanuatu;VAT=Vatican City;VEN=Venezuela;VNM=Vietnam;YEM=Yemen;ZMB=Zambia;ZWE=Zimbabwe;GEO=Abkhazia;TWN=China, Republic of (Taiwan);AZE=Nagorno-Karabakh;CYP=Northern Cyprus;MDA=Pridnestrovie (Transnistria);SOM=Somaliland;GEO=South Ossetia;
</xsl:variable>
<xsl:element name="Western">
<xsl:for-each select="Western/Scandivian">
<xsl:element name="Scandivian">
<xsl:variable name="scandCode" select="."/>
<xsl:attribute name="code">
<xsl:value-of select="$scandCode"/>
</xsl:attribute>
<xsl:attribute name="name">
<xsl:value-of select="substring-before(substring-after($westList, concat(';',$scandCode,'=')),';')"/>
</xsl:attribute>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Explanation
The above XSL file includes a key-value string in the template to get the concerned value. Here is a country code value that does the external mapping.
Output:
Example #2
XML
<?xml version="1.0" encoding="UTF-8"?>
<MEMP>
<admin>
<A_Code>5425</A_Code>
<PatientName>Hilfer</PatientName>
<GuardName>Motis</GuardName>
<entry_Dt>05/12/06</entry_Dt>
<Dept>DDDD</Dept>
<ICU>N34</ICU>
</admin>
<admin>
<A_Code>5456</A_Code>
<PatientName>Gten</PatientName>
<GuardName>Thmas</GuardName>
<entry_Dt>06/1/08</entry_Dt>
<Dept>Sssss</Dept>
<ICU>N67</ICU>
</admin>
<admin>
<A_Code>5545</A_Code>
<PatientName>Seatfilg</PatientName>
<GuardName>david</GuardName>
<entry_Dt>08/11/15</entry_Dt>
<Dept>PPPP</Dept>
<ICU>N45</ICU>
</admin>
<admin>
<A_Code>8974</A_Code>
<PatientName>Glacy</PatientName>
<GuardName>Ancid</GuardName>
<entry_Dt>08/5/07</entry_Dt>
<Dept>AAAA</Dept>
<ICU>P677</ICU>
</admin>
</MEMP>
XSL
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
<xsl:template match="/">
<ETARGET>
<xsl:for-each select="//admin">
<admin>
<xsl:variable name="pname" select="//admin/PatientName"/>
<xsl:variable name="gname" select="//admin/GuardName"/>
<A_Code><xsl:value-of select="//admin/A_Code"/></A_Code>
<entry_Dt><xsl:value-of select="//admin/entry_Dt"/></entry_Dt>
<Designation><xsl:value-of select="//admin/ICU"/></Designation>
<Dept><xsl:value-of select="//admin/Dept"/></Dept>
</admin>
</xsl:for-each>
</ETARGET>
</xsl:template>
</xsl:transform>
Explanation
The above code is the simple scenario of XSLT mapping, which converts an XML input source to HTML Output, as shown below.
Output:
Example #3
XML
<?xml version="1.0" encoding="UTF-8"?>
<Packets>
<Frame-17>005</Frame-17>
<Frame-18>006</Frame-18>
<Frame-19>018</Frame-19>
<Frame-20>028</Frame-20>
</Packets>
XSL
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:template match="/Packets">
<Newcase>
<xsl:for-each select="*">
<f1>
<xsl:value-of select="." />
</f1>
</xsl:for-each>
</Newcase>
</xsl:template>
</xsl:stylesheet>
Explanation
The above code maps each element in the source to the element in the target code. To achieve this functionality, we have used a select statement which maps multiple elements to a single element Newcase in our case in target result using XSLT.
Output:
Advantages
- It drops the namespaces or any prefix in the document but keeps the structure.
- The Mapping tools used here provide good debugging of XSLT code to understand the structure.
- The only solution for request/response messages. Even it helps in updating the target tables of a relational database.
Conclusion
Therefore, to the end, the mapping that handles the structure in B2B is quite complex; in such a case, we use integrations of the best cases. Mapping, in general, is a tricky process though we use different tools; when we suppose to map 100 fields, it sucks. Though many of the commercial tools are available, I probably use Altova for a simple mapping task.
Recommended Articles
This is a guide to XSLT mapping. Here we discuss How mapping work in XSLT and its Advantages, along with the examples and outputs. You may also have a look at the following articles to learn more –