Verticalize XML using XSLT - xslt

I am trying to implement an at first looking simple transformation but whatever I have tried has been failed.
The XML is generated from a fixed length record and have the below format.
<?xml version="1.0" encoding="UTF-8"?>
<record>
<no_of_records>30</no_of_records>
<cust_lastname_1>Smith</cust_lastname_1>
<cust_name_1>John</cust_name_1>
<cust_id_1>X45</cust_id_1>
<cust_lastname_2>George</cust_lastname_2>
<cust_name_2>Michael</cust_name_2>
<cust_id_2>X76</cust_id_2>
<cust_lastname_3>Ria</cust_lastname_3>
<cust_name_3>Chris</cust_name_3>
<cust_id_3>C87</cust_id_3>
...
</record>
The no_of_records indicates how many _X suffixed elements contains each record and because of its fix length origin has a defined maximum.
I want to transform it to a “verticalized” form resempling the below.
<record>
<customer num="1">
<lastname>Smith</lastname>
<name>John</name>
<id>X45</id>
</customer>
<customer num="2">
<lastname>George</lastname>
<name>Michael</name>
<id>X76</id>
</customer>
<customer num="3">
<lastname>Ria</lastname>
<name>Chris</name>
<id>C87</id>
...
</customer>
</record>
Any help would greatly appreciated.

In XSLT 2.0, you want something like
<xsl:for-each-group select="*" group-starting-with="*[starts-with(local-name(), 'cust_lastname']">
<customer num="{position()}">
<xsl:apply-templates select="current-group()"/>
</customer>
</xsl:for-each-group>
....
<xsl:template match="*[starts-with(local-name(), 'cust')]">
<xsl:element name="{replace(local-name(), 'cust_(.*?)_[0-9]+', '$1')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>

The solution from #Michael Kay works fine. Thank you !
XML
<?xml version="1.0" encoding="UTF-8"?>
<record>
<no_of_records>3</no_of_records>
<cust_lastname_1>Smith</cust_lastname_1>
<cust_name_1>John</cust_name_1>
<cust_id_1>X45</cust_id_1>
<cust_lastname_2>George</cust_lastname_2>
<cust_name_2>Michael</cust_name_2>
<cust_id_2>X76</cust_id_2>
<cust_lastname_3>Ria</cust_lastname_3>
<cust_name_3>Chris</cust_name_3>
<cust_id_3>C87</cust_id_3>
</record>
XSLT
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="record">
<records>
<xsl:for-each-group select="*[starts-with(local-name(), 'cust_')]"
group-starting-with="*[starts-with(local-name(), 'cust_lastname')]">
<customer num="{position()}">
<xsl:apply-templates select="current-group()"/>
</customer>
</xsl:for-each-group>
</records>
</xsl:template>
<xsl:template match="*[starts-with(local-name(), 'cust')]">
<xsl:element name="{replace(local-name(), 'cust_(.*?)_[0-9]+', '$1')}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result
<?xml version="1.0" encoding="UTF-8"?>
<records>
<customer num="1">
<lastname>Smith</lastname>
<name>John</name>
<id>X45</id>
</customer>
<customer num="2">
<lastname>George</lastname>
<name>Michael</name>
<id>X76</id>
</customer>
<customer num="3">
<lastname>Ria</lastname>
<name>Chris</name>
<id>C87</id>
</customer>
</records>

Related

Split large XML to smaller chunks by node count using XSLT

I have a requirement where we are getting a large XML file and I need to transform on small chunks
below is the XML sample with 4 records, I have to transform the XML so I am able to group them in chunks of 2.
<!-- Original XML-->
<EmpDetails>
<Records>
<EmpID>1</EmpID>
<Age>20</Age>
</Records>
<Records>
<EmpID>2</EmpID>
<Age>21</Age>
</Records>
<Records>
<EmpID>3</EmpID>
<Age>22</Age>
</Records>
<Records>
<EmpID>4</EmpID>
<Age>23</Age>
</Records>
</EmpDetails>
<!-- Expected XML-->
<EmpDetails>
<Split>
<Records>
<EmpID>1</EmpID>
<Age>20</Age>
</Records>
<Records>
<EmpID>2</EmpID>
<Age>21</Age>
</Records>
</Split>
<Split>
<Records>
<EmpID>3</EmpID>
<Age>22</Age>
</Records>
<Records>
<EmpID>4</EmpID>
<Age>23</Age>
</Records>
</Split>
</EmpDetails>
I tried few things including below without success.
<?xml version="1.0" encoding="UTF-8" ?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<EmpDetails>
<xsl:for-each select="/EmpDetails/Records">
<Split>
<Records>
<EmpID>
<xsl:value-of select="EmpID"/>
</EmpID>
<Age>
<xsl:value-of select="Age"/>
</Age>
</Records>
</Split>
</xsl:for-each>
</EmpDetails>
</xsl:template>
</xsl:stylesheet>
Thanks
Yatan
group them in chunks of 2.
This could be done simply by:
XSLT 1.0
<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="/EmpDetails">
<xsl:copy>
<xsl:for-each select="Records[position() mod 2 = 1]">
<Split>
<xsl:copy-of select=". | following-sibling::Records[1]"/>
</Split>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Added:
To divide the records into groups of 200, you can do:
...
<xsl:for-each select="Records[position() mod 200 = 1]">
<Split>
<xsl:copy-of select=". | following-sibling::Records[position() < 200]"/>
</Split>
</xsl:for-each>
...
In XSLT 2.0 you could do:
<xsl:stylesheet version="2.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="/EmpDetails">
<xsl:copy>
<xsl:for-each-group select="Records" group-adjacent="(position() - 1) idiv 200">
<Split>
<xsl:copy-of select="current-group()"/>
</Split>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
use this code:
<xsl:for-each select="Records[position() mod 2 = 0]">
instead of this
<xsl:for-each select="Records[position() mod 2 = 1]">

XSLT logic to add a sequence for the combination of elements with same dates and ID field

I am struggling to create logic for transformation.
Logic: "Seq" A sequential number used to make a unique key when the ID and Date fields are equal.
<root>
<Record>
<ID>11</ID>
<date>2020-03-11-07:00</date>
<quantity>10</quantity>
</Record>
<Record>
<ID>13</ID>
<date>2020-03-12-07:00</date>
<quantity>20</quantity>
</Record>
<Record>
<ID>15</ID>
<date>2020-03-13-07:00</date>
<quantity>40</quantity>
</Record>
<Record>
<ID>11</ID>
<date>2020-03-11-07:00</date>
<quantity>5</quantity>
</Record>
<Record>
<ID>13</ID>
<date>2020-03-17-07:00</date>
<quantity>100</quantity>
</Record>
</root>
to, Output
ID,seq,Date,quantity
11,1,2020-03-11-07:00,10
11,2,2020-03-11-07:00,5
13,1,2020-03-12-07:00,20
15,1,2020-03-13-07:00,40
13,1,2020-03-17-07:00,100
In XSLT 3 it is a simple grouping problem with a composite key:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="text" />
<xsl:template match="root">
<xsl:text>ID,seq,Date,quantity
</xsl:text>
<xsl:for-each-group select="Record" composite="yes" group-by="ID, date">
<xsl:apply-templates select="current-group()"/>
</xsl:for-each-group>
</xsl:template>
<xsl:template match="Record">
<xsl:value-of select="ID, position(), date, quantity" separator=","/>
<xsl:text>
</xsl:text>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/93dFepy
XSLT 3 can be used with Saxon 9.8 and later or AltovaXML 2017 R3 or later.

Flatten XML after copy

I have a requirement to create a copy of the xml record based on a repeating field which I am able to do so, however I need the result to be flattened
I have tried to use variables and copying them to the output
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<Hire>
<xsl:for-each select="Hire/Record">
<xsl:variable name="var_record" select="./*
[not(name()='Sweldo')]" />
<xsl:for-each select="Sweldo">
<xsl:variable name="var_SWELDO" select=".">
</xsl:variable>
<Record>
<xsl:copy-of select="$var_record" />
<xsl:copy-of select="$var_SWELDO" />
</Record>
</xsl:for-each>
</xsl:for-each>
</Hire>
</xsl:template>
</xsl:stylesheet>
The input is
<?xml version="1.0" encoding="UTF-8"?>
<Hire>
<Record>
<XRefCode>XX</XRefCode>
<EmployeeNumber>161</EmployeeNumber>
<BirthDate>1985-04-09</BirthDate>
<SocialSecurityNumber>XXXXXXX</SocialSecurityNumber>
<FirstName>XX</FirstName>
<LastName>XX</LastName>
<MiddleName>D</MiddleName>
<Sweldo>
<sahod>ONE MILLION</sahod>
</Sweldo>
<Sweldo>
<sahod>1 BILLION</sahod>
</Sweldo>
</Record>
</Hire>
The output I am getting is
<?xml version="1.0"?>
<Hire>
<Record>
<XRefCode>161</XRefCode>
<EmployeeNumber>161</EmployeeNumber>
<BirthDate>1985-04-09</BirthDate>
<SocialSecurityNumber>999-81-9462</SocialSecurityNumber>
<FirstName>XXX</FirstName>
<LastName>XXXXX</LastName>
<MiddleName>D</MiddleName>
<Sweldo>
<sahod>ONE MILLION</sahod>
</Sweldo>
</Record>
<Record>
<XRefCode>161</XRefCode>
<EmployeeNumber>161</EmployeeNumber>
<BirthDate>1985-04-09</BirthDate>
<SocialSecurityNumber>999-81-9462</SocialSecurityNumber>
<FirstName>XXX</FirstName>
<LastName>XXXX</LastName>
<MiddleName>D</MiddleName>
<Sweldo>
<sahod>1 BILLION</sahod>
</Sweldo>
</Record>
</Hire>
However I need the following format
<?xml version="1.0"?>
<Hire>
<Record>
<XRefCode>161</XRefCode>
<EmployeeNumber>161</EmployeeNumber>
<BirthDate>1985-04-09</BirthDate>
<SocialSecurityNumber>999-81-9462</SocialSecurityNumber>
<FirstName>XXXX</FirstName>
<LastName>XXXXXX</LastName>
<MiddleName>D</MiddleName>
<sahod>ONE MILLION</sahod>
</Record>
<Record>
<XRefCode>161</XRefCode>
<EmployeeNumber>161</EmployeeNumber>
<BirthDate>1985-04-09</BirthDate>
<SocialSecurityNumber>999-81-9462</SocialSecurityNumber>
<FirstName>XXXX</FirstName>
<LastName>XXXX</LastName>
<MiddleName>D</MiddleName>
<sahod>1 BILLION</sahod>
</Record>
</Hire>
Is there a way to completely remove the element?
Please check and update following code:-
<xsl:for-each select="Sweldo">
**change to**
<xsl:for-each select="Sweldo/sahod">

Transform XML - Form a proper record

I am trying to create an XSLT to transform an XML document and having trouble with identifying the record boundaries. Below is my xml
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<mheader>
<mid>1</mid>
<mname>mn</mname>
</mheader>
<cheader>
<cid>1</cid>
<cname>cn</cname>
</cheader>
<lheader>
<lid>1</lid>
<lname>ln</lname>
</lheader>
<aheader>
<aid>1</aid>
<aname>an</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay1</pname>
</pos>
<mheader>
<mid>2</mid>
<mname>mh1</mname>
</mheader>
<cheader>
<cid>2</cid>
<cname>ch1</cname>
</cheader>
<lheader>
<lid>2</lid>
<lname>lh1</lname>
</lheader>
<aheader>
<aid>2</aid>
<aname>ah1</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay3</pname>
</pos>
<pos>
<pid>3</pid>
<pname>pay4</pname>
</pos>
</catalog>
I have to transform my xml like the one below
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<record>
<mheader>
<mid>1</mid>
<mname>mn</mname>
</mheader>
<cheader>
<cid>1</cid>
<cname>cn</cname>
</cheader>
<lheader>
<lid>1</lid>
<lname>ln</lname>
</lheader>
<aheader>
<aid>1</aid>
<aname>an</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay1</pname>
</pos>
</record>
<record>
<mheader>
<mid>2</mid>
<mname>mh1</mname>
</mheader>
<cheader>
<cid>2</cid>
<cname>ch1</cname>
</cheader>
<lheader>
<lid>2</lid>
<lname>lh1</lname>
</lheader>
<aheader>
<aid>2</aid>
<aname>ah1</aname>
</aheader>
<pos>
<pid>1</pid>
<pname>pay</pname>
</pos>
<pos>
<pid>2</pid>
<pname>pay3</pname>
</pos>
<pos>
<pid>3</pid>
<pname>pay4</pname>
</pos>
</record>
</catalog>
A record ideally should start from the tag mheader and ends at the last POS tag.
This is what i have tried till now
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<record>
<xsl:apply-templates select="catalog/mheader"/>
<xsl:apply-templates select="catalog/cheader"/>
<xsl:apply-templates select="catalog/lheader"/>
<xsl:apply-templates select="catalog/aheader"/>
<xsl:apply-templates select="catalog/pos"/>
</record>
</xsl:template>
</xsl:stylesheet>
Any ideas on how to form a proper record here in this case?
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="catalog">
<xsl:copy>
<xsl:for-each-group select="*" group-starting-with="mheader">
<Record>
<xsl:copy-of select="current-group()"/>
</Record>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Check it.

Merging two diff XMLS using XSLT(1.0)

I am new to XSLT and I need help in merging two different XML documents into one.
XML1.xml
<customers>
<customer>
<Person name="Ram" Id="101"/>
<address>flat 4</address>
</customer>
<customer>
<Person name="Raghav" Id="102"/>
<address>flat 9</address>
</customer>
</customers>
XML2.xml
<Products>
<Product>
<name>Onida Tv</name>
<consumer>Ram</consumer>
</Product>
<Product>
<name>washing machine</name>
<consumer>Ram</consumer>
</Product>
<Product>
<name>Water purifier</name>
<consumer>Raghav</consumer>
</Product>
<Product>
<name>iPhone</name>
<consumer>Raghav</consumer>
</Products>
</Products>
Desired XML output:
<customers>
<customer>
<Person name="Ram" Id="101"/>
<address>flat 4</address>
<products>
<name>washing machine</name>
<name>Onida TV</name>
</products>
</customer>
<customer>
<Person name="Raghav" Id="102"/>
<address>flat 9</address>
<products>
<name>iPhone</name>
<name>Water purifier</name>
</products>
</customer>
</customers>
The second XML is to be considered external in this context. I need to append to each customer the corresponding products. How can I do that?
Try it this way:
XSLT 1.0
<?xml version="1.0" encoding="UTF-8"?>
<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:strip-space elements="*"/>
<xsl:variable name="lookup-document" select="document('XML2.xml')" />
<xsl:key name="product-by-consumer" match="Product" use="consumer" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="customer">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
<xsl:variable name="name" select="Person/#name" />
<products>
<!-- switch context to the other document in order to use key -->
<xsl:for-each select="$lookup-document">
<xsl:copy-of select="key('product-by-consumer', $name)/name"/>
</xsl:for-each>
</products>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note that this assumes that customer names are unique.