XSLT: Convert Name/Value pair and transform an XML - xslt-1.0

I need to convert a name value pair into XML. I'm able to generate an XML, but the element name should be grouped and it should not be duplicated. Please see below. The FieldValue element contains 2 OrderItem values in the Detail node. If the FieldValue with OrderItem repeats, then the result should be grouped into one OrderItem node. Please help.
Source XML:
<SC>
<Header>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderHeader</FieldValue>
</Record>
<Record>
<FieldName>Order</FieldName>
<FieldValue>1234</FieldValue>
</Record>
</Header>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>1</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>10</FieldValue>
</Record>
</Detail>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>2</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>20</FieldValue>
</Record>
</Detail>
</SC>
Target XML:
<Order>
<OrderItem>
<Item>
<Item>1</Item>
<Qty>10</Qty>
</Item>
<Item>
<Item>2</Item>
<Qty>20</Qty>
</Item>
</OrderItem>
</Order>
XSLT:
<xsl:template match="#*|node()">
<Order>
<xsl:for-each select="Detail">
<Item>
<xsl:apply-templates select="Record[position()>1]"/>
</Item>
</xsl:for-each>
</Order>
</xsl:template>
<xsl:template match="Record">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>

The grouping can be done as follows:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="SC">
<Order>
<xsl:for-each-group select="Detail" group-by="Record[1]/FieldValue">
<xsl:element name="{current-grouping-key()}">
<xsl:apply-templates select="current-group()"/>
</xsl:element>
</xsl:for-each-group>
</Order>
</xsl:template>
<xsl:template match="Detail">
<Item>
<xsl:apply-templates select="Record[position() gt 1]"/>
</Item>
</xsl:template>
<xsl:template match="Record">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

It appears that you are trying to define two XSLT templates, when one should be sufficient. You want to match on the root and then that you want to iterate over each SC/Detail.
Then, you want to take the FieldValue of the sibling of the FieldName node that is 'Item' (for item value) and 'Qty' (for quantity value), but only those listed under 'Record'.
Note: You have specified a doubly-nested <Item> in your transformed output and this solution reflects that requirement.
This XSLT should do what you are requesting:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<xsl:for-each select="SC/Detail">
<Order>
<OrderItem>
<Item>
<Item>
<xsl:value-of select="Record[FieldName[text()='Item']]/FieldValue" />
</Item>
<Qty>
<xsl:value-of select="Record[FieldName[text()='Qty']]/FieldValue" />
</Qty>
</Item>
</OrderItem>
</Order>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

Related

How to loop through all same name parent and child nodes

I have an XML file where I have same name nodes: Parent and child, for example:
<Root>
<Field name="ID">
<description>Test 1</description>
</Field>
<Field name="Period">
<description>Test 2</description>
<Field name="Name">
<description>Test 3</description>
</Field>
<Field name="Name2">
<description>Test 4</description>
<Field name="address">
<description>Test 5</description>
</Field>
<Field name="partyID">
<description>Test 6</description>
<Field name="E-ID">
<Field name="address">
<description>Test 7</description>
</Field>
</Field>
</Field>
</Field>
</Field>
</Root>
The problem is, I am not sure how deep we can have child elements with the same name:
I used template-match on top parent node:
<xsl:template match="Field">
<xsl:value-of select="description"/>
<xsl:for-each select="Field">
<xsl:value-of select="description"/>
</xsl:for-each>
</xsl:template>
This code is not providing me all the child node values. I am looking for a code which can loop through all same name nodes and provide value of description element.
I cannot add multiple for-each because as I said, this is unknown how many times we will have Field node inside another Field node.
Please help me to solve this problem.
You could use recursion to process all Field elements from the root of the tree to the leaves - for example (you did not post the expected result, so I made something up):
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:strip-space elements="*"/>
<xsl:template match="/Root">
<output>
<xsl:apply-templates select="Field"/>
</output>
</xsl:template>
<xsl:template match="Field">
<desc>
<xsl:value-of select="description"/>
</desc>
<xsl:apply-templates select="Field"/>
</xsl:template>
</xsl:stylesheet>
Alternatively, you could just select all Field elements regardless of their position in the tree hierarchy:
<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:template match="/Root">
<output>
<xsl:for-each select="//Field">
<desc>
<xsl:value-of select="description"/>
</desc>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>

Remove both duplicate records using XSLT

I am trying to remove both the duplicate records in an XML
I already can remove the second occurrence but I need to remove both records in this case.
This is the XSLT mapping that I have
<?xml version="1.0" encoding="UTF-8"?>
<xsl:transform version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="item">
<xsl:copy-of select="." />
</xsl:template>
<xsl:template match="/ZTABLE/Record">
<ZTABLE>
<Record>
<xsl:apply-templates select="item[not(ID=preceding-sibling::item/ID)]" />
</Record>
</ZTABLE>
</xsl:template>
</xsl:transform>
The input XML is:
<ZTABLE>
<Record>
<item>
<ID>400400</ID>
</item>
<item>
<ID>100100</ID>
</item>
<item>
<ID>200200</ID>
</item>
<item>
<ID>300300</ID>
</item>
<item>
<ID>400400</ID>
</item>
</Record>
</ZTABLE>
The expected output is
<ZTABLE>
<Record>
<item>
<ID>100100</ID>
</item>
<item>
<ID>200200</ID>
</item>
<item>
<ID>300300</ID>
</Record>
</ZTABLE>
Try it this way:
<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:key name="item" match="item" use="ID" />
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[count(key('item', ID)) > 1]"/>
</xsl:stylesheet>
For explanation, read about Muenchian grouping.

How do I handle files of different lengths / more 'lines'

My problem is, is that my input will have varying lengths / lines. My current code (thanks to michael.hor257k on my previous question) works with my test file, but I'm sure it won't work with files that have more items.
For example:
An input file with 100 records
or
an input file with 2 records.
I am also not sure how to ignore the last record as it is junk created in the original csv file that's converted to the input file.
Input:
<csv-xml>
<record line="1">
<csv-field-1>1</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>7654321</csv-field-3>
<csv-field-4>1</csv-field-4>
<csv-field-5>08/08/19</csv-field-5>
<csv-field-6>08/08/19</csv-field-6>
</record>
<record line="2">
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>12345678</csv-field-3>
<csv-field-4>3</csv-field-4>
</record>
<record line="3">
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>22345679</csv-field-3>
<csv-field-4>7</csv-field-4>
</record>
<record line="4">
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>32345680</csv-field-3>
<csv-field-4>6</csv-field-4>
</record>
<record line="5">
<csv-field-1>2</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3>42345681</csv-field-3>
<csv-field-4>2</csv-field-4>
</record>
<record line="6">
<csv-field-1>3</csv-field-1>
<csv-field-2>12345</csv-field-2>
<csv-field-3></csv-field-3>
</record>
</csv-xml>
Code:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
<xsl:output method="xml" indent="yes"/>
<xsl:template match="/">
<orders>
<order>
<accountNo>
<xsl:value-of select="csv-xml/record[#line>='1']/csv-field-3"/>
</accountNo>
<orderDate>
<xsl:value-of select="csv-xml/record[#line>='1']/csv-field-5"/>
</orderDate>
<orderItems>
<orderItem>
<productCode>
<xsl:value-of select="csv-xml/record[#line>='2']/csv-field-3"/>
</productCode>
<Quantity>
<xsl:value-of select="csv-xml/record[#line>='2']/csv-field-4"/>
</Quantity>
</orderItem>
<orderItem>
<productCode>
<xsl:value-of select="csv-xml/record[#line>='3']/csv-field-3"/>
</productCode>
<Quantity>
<xsl:value-of select="csv-xml/record[#line>='3']/csv-field-4"/>
</Quantity>
</orderItem>
<orderItem>
<productCode>
<xsl:value-of select="csv-xml/record[#line>='4']/csv-field-3"/>
</productCode>
<Quantity>
<xsl:value-of select="csv-xml/record[#line>='4']/csv-field-4"/>
</Quantity>
</orderItem>
<orderItem>
<productCode>
<xsl:value-of select="csv-xml/record[#line>='5']/csv-field-3"/>
</productCode>
<Quantity>
<xsl:value-of select="csv-xml/record[#line>='5']/csv-field-4"/>
</Quantity>
</orderItem>
</orderItems>
</order>
</orders>
</xsl:template>
</xsl:stylesheet>
The output I have:
<?xml version="1.0"?>
<orders>
<order>
<accountNo>7654321</accountNo>
<orderDate>08/08/19</orderDate>
<orderItems>
<orderItem>
<productCode>12345678</productCode>
<Quantity>3</Quantity>
</orderItem>
<orderItem>
<productCode>22345679</productCode>
<Quantity>7</Quantity>
</orderItem>
<orderItem>
<productCode>32345680</productCode>
<Quantity>6</Quantity>
</orderItem>
<orderItem>
<productCode>42345681</productCode>
<Quantity>2</Quantity>
</orderItem>
</orderItems>
</order>
</orders>
The output I want:
<?xml version="1.0"?>
<orders><order accountNo="7654321" orderDate="08/08/19">
<orderItems>
<orderItem productCode="12345678" quantity="3"/>
<orderItem productCode="22345679" quantity="7"/>
<orderItem productCode="32345680" quantity="6"/>
<orderItem productCode="42345681" quantity="2"/>
</orderItems>
</order>
</orders>
How about simply:
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="/csv-xml">
<xsl:variable name="header" select="record[1]" />
<orders>
<order accountNo="{$header/csv-field-3}" orderDate="{$header/csv-field-5}">
<orderItems>
<xsl:for-each select="record[position() != 1 and position() != last()]">
<orderItem productCode="{csv-field-3}" quantity="{csv-field-4}"/>
</xsl:for-each>
</orderItems>
</order>
</orders>
</xsl:template>
</xsl:stylesheet>
P.S. Read about Attribute Value Templates.

XSLT 1.0 grouping on one or multiple levels

Today's challenge was grouping in XSLT 1.0. Found out there are something called keys and the Muenchian grouping.
Input XML:
<Items>
<Item>
<ID>1</ID>
<Name>A</Name>
<Country>Sweden</Country>
<Region>Småland</Region>
</Item>
<Item>
<ID>2</ID>
<Name>B</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
<Item>
<ID>3</ID>
<Name>C</Name>
<Country>USA</Country>
<Region>Alaska</Region>
</Item>
<Item>
<ID>4</ID>
<Name>D</Name>
<Country>USA</Country>
<Region>Texas</Region>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
<Country>Sweden</Country>
<Region>Norrland</Region>
</Item>
</Items>
I need to make thins XML into a better structure, and from this sample XML I't like to get items structured by country and region. Below is wanted result where country and region gets sorted as well:
<Items>
<Country Name="Sweden">
<Region Name="Norrland">
<Item>
<ID>2</ID>
<Name>B</Name>
</Item>
<Item>
<ID>5</ID>
<Name>E</Name>
</Item>
</Region>
<Region Name="Småland">
<Item>
<ID>1</ID>
<Name>A</Name>
</Item>
</Region>
</Country>
<Country Name="USA">
<Region Name="Alaska">
<Item>
<ID>3</ID>
<Name>C</Name>
</Item>
</Region>
<Region Name="Texas">
<Item>
<ID>4</ID>
<Name>D</Name>
</Item>
</Region>
</Country>
</Items>
EDIT:
I also want to make sure regions end up in their own country, even if there are duplicates. I edited the answer accordingly.
Also, I'd like to hint about xsltfiddle.liberty-development.net as an easy way of doing trial-and-error XSLT development...
Inspired by this article, I found a neat solution to this problem:
I have included comments for using it for single or double grouping, see comments in the code. Notice how I use first key (index) as input to the secon for-each loop:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="country" match="Item" use="Country" />
<xsl:key name="region" match="Item" use="concat(Region, '|', Country)" />
<xsl:template match="/Items">
<Items>
<xsl:for-each select="Item[generate-id(.) = generate-id(key('country', Country))]">
<xsl:sort select="Country" />
<xsl:variable name="_country" select="Country" />
<xsl:element name="Country">
<xsl:attribute name="Name"><xsl:value-of select="$_country" /></xsl:attribute>
<!-- single level grouping -->
<!--<xsl:apply-templates select="key('country', Country)" />-->
<!-- double grouping -->
<!-- START -->
<xsl:for-each select="key('country', Country)[generate-id(.) = generate-id(key('region', concat(Region, '|', Country)))]">
<xsl:sort select="Region" />
<xsl:variable name="_region" select="Region" />
<xsl:element name="Region">
<xsl:attribute name="Name"><xsl:value-of select="$_region" /></xsl:attribute>
<xsl:apply-templates select="key('region', concat(Region, '|', Country))" />
</xsl:element>
</xsl:for-each>
<!-- END -->
</xsl:element>
</xsl:for-each>
</Items>
</xsl:template>
<xsl:template match="Item">
<xsl:element name="Item">
<xsl:element name="ID"><xsl:value-of select="ID" /></xsl:element>
<xsl:element name="Name"><xsl:value-of select="Name" /></xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>

XSLT: Need to convert name value pair content into XML

I'm new to XSLT. I have a requirement to convert an XML containing name value pair into target XML.
I need to generate a target XML where each FieldName is an element name and it value is FieldValue. Please find below the output which I need.
Thanks in advance for the help.
Source XML:
<SC>
<Header>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderHeader</FieldValue>
</Record>
<Record>
<FieldName>Order</FieldName>
<FieldValue>1234</FieldValue>
</Record>
</Header>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>1</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>10</FieldValue>
</Record>
</Detail>
<Detail>
<Record>
<FieldName>Schema</FieldName>
<FieldValue>OrderItem</FieldValue>
</Record>
<Record>
<FieldName>Item</FieldName>
<FieldValue>2</FieldValue>
</Record>
<Record>
<FieldName>Qty</FieldName>
<FieldValue>20</FieldValue>
</Record>
</Detail>
</SC>
Target XML to be generated:
<SC>
<OrderHeader>
<Order>1234</Order>
</OrderHeader>
<OrderItem>
<Item>1</Item>
<Qty>10</Qty>
</OrderItem>
<OrderItem>
<Item>2</Item>
<Qty>20</Qty>
</OrderItem>
</SC>
XSLT which I tried: I'm not getting the desired output
<xsl:template match="Header">
<xsl:apply-templates select="Record"/>
</xsl:template>
<xsl:template match="Record">
<xsl:if test="FieldName = 'Structure'">
<xsl:element name="{FieldValue}">
<xsl:value-of select="./text()"/>
</xsl:element>
</xsl:if>
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
Here's another option that is similar to Martin's.
<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="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Header|Detail">
<xsl:element name="{Record[1]/FieldValue}">
<xsl:apply-templates select="Record[position()>1]"/>
</xsl:element>
</xsl:template>
<xsl:template match="Record">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
It seems you want to process the first Record child as a container element and the following siblings are to be transformed as in your description:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
exclude-result-prefixes="xs"
version="2.0">
<xsl:output indent="yes"/>
<xsl:template match="SC">
<xsl:copy>
<xsl:apply-templates select="*/Record[1]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SC/*/Record[1]">
<xsl:element name="{FieldValue}">
<xsl:apply-templates select="following-sibling::Record"/>
</xsl:element>
</xsl:template>
<xsl:template match="SC/*/Record[position() gt 1]">
<xsl:element name="{FieldName}">
<xsl:value-of select="FieldValue"/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>