Eliminating duplication of node data in XSLT 1.0 - xslt

Thanks in Advance & Sorry as it might take a while for you to understand my logic (i'm not a pro in using XSLT).
Could anyone help in eliminating duplication of tag "PaySlip_Val" and group all "ListOfPaySlipData" childs under one parent "ListOfPaySlipMonth".
Below is XML hierarchy,
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<P>
<FstName>F1</FstName>
<LstName>L1</LstName>
<Type>
<Payslip>y</Payslip>
<Details>
<Year>2016</Year>
<Month>Jan</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>yes</Payslip>
<Details>
<Year>2016</Year>
<Month>Mar</Month>
<Amount>$$$</Amount>
</Details>
<Details>
<Year>2016</Year>
<Month>Apr</Month>
<Amount>$$$</Amount>
</Details>
</Type>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>May</Month>
<Amount>$$$</Amount>
</Details>
</Type>
</P>
<P>
<FstName>F2</FstName>
<LstName>L2</LstName>
<Type>
<Payslip>n</Payslip>
<Details>
<Year>2016</Year>
<Month>Feb</Month>
<Leaves>4</Leaves>
</Details>
</Type>
</P>
</Data>
I have applied below XSLT,
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format">
<xsl:variable name="smallcase" select="'abcdefghijklmnopqrstuvwxyz'" />
<xsl:variable name="uppercase" select="'ABCDEFGHIJKLMNOPQRSTUVWXYZ'" />
<xsl:template match="/">
<xsl:call-template name="Pay_Slip" />
</xsl:template>
<xsl:template name="Pay_Slip">
<xsl:element name="ListOfData">
<xsl:for-each select="Data/P">
<xsl:element name="First_Name">
<xsl:value-of select="FstName" />
</xsl:element>
<xsl:element name="Last_Name">
<xsl:value-of select="LstName" />
</xsl:element>
<xsl:element name="PaySlip">
<xsl:for-each select="Type">
<xsl:variable name="Type" select="Payslip" />
<xsl:if test="$Type ='y' or $Type ='yes'">
<xsl:element name="PaySlip_Val">
<xsl:value-of select="$Type" />
</xsl:element>
<xsl:element name="ListOfPaySlipMonth">
<xsl:for-each select="Details">
<xsl:element name="ListOfPaySlipData">
<xsl:element name="Month">
<xsl:value-of select="Month" />
</xsl:element>
<xsl:element name="Salary">
<xsl:value-of select="Amount" />
</xsl:element>
<xsl:element name="Year">
<xsl:value-of select="Year" />
</xsl:element>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:for-each>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Result is :
<?xml version="1.0" encoding="UTF-8"?>
<ListOfData>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
<PaySlip_Val>yes</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
<First_Name>F2</First_Name>
<Last_Name>L2</Last_Name>
<PaySlip/>
</ListOfData>
Desired Result :
<ListOfData>
<ListOfP>
<First_Name>F1</First_Name>
<Last_Name>L1</Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<ListOfPaySlipData>
<Month>Jan</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Feb</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Mar</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
<ListOfPaySlipData>
<Month>Apr</Month>
<Salary>$$$</Salary>
<Year>2016</Year>
</ListOfPaySlipData>
</ListOfPaySlipMonth>
</PaySlip>
</ListOfP>
</ListOfData>

AFAICT, this returns the expected result:
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="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Data">
<ListOfData>
<xsl:apply-templates/>
</ListOfData>
</xsl:template>
<xsl:template match="P">
<First_Name><xsl:value-of select="FstName"/></First_Name>
<Last_Name><xsl:value-of select="LstName"/></Last_Name>
<xsl:variable name="payslips" select="Type[Payslip='yes' or Payslip='y']" />
<PaySlip>
<xsl:if test="$payslips">
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<xsl:apply-templates select="Type[Payslip='yes' or Payslip='y']/Details"/>
</ListOfPaySlipMonth>
</xsl:if>
</PaySlip>
</xsl:template>
<xsl:template match="Details">
<ListOfPaySlipData>
<xsl:copy-of select="Month"/>
<Salary><xsl:value-of select="Amount"/></Salary>
<xsl:copy-of select="Year"/>
</ListOfPaySlipData>
</xsl:template>
</xsl:stylesheet>
Edit:
I have updated the desired result. The condition is that I need to get
the First Name & Last name only if the "Payslip" tag has y or yes. In
other cases, it should not fetch the data (F2 & L2 as per example).
Try it this way, then:
<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="*"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="/Data">
<ListOfData>
<xsl:apply-templates select="P[Type/Payslip='yes' or Type/Payslip='y']"/>
</ListOfData>
</xsl:template>
<xsl:template match="P">
<First_Name><xsl:value-of select="FstName"/></First_Name>
<Last_Name><xsl:value-of select="LstName"/></Last_Name>
<PaySlip>
<PaySlip_Val>y</PaySlip_Val>
<ListOfPaySlipMonth>
<xsl:apply-templates select="Type[Payslip='yes' or Payslip='y']/Details"/>
</ListOfPaySlipMonth>
</PaySlip>
</xsl:template>
<xsl:template match="Details">
<ListOfPaySlipData>
<xsl:copy-of select="Month"/>
<Salary><xsl:value-of select="Amount"/></Salary>
<xsl:copy-of select="Year"/>
</ListOfPaySlipData>
</xsl:template>
</xsl:stylesheet>

Related

Append new xml node under existing xml nodes using xslt

I am trying to append a new XML node under existing XML node but i wasn't able to achieve the desired result.
Please find my below XML,
<Root>
<Row>
<A1>0</A1>
<A2>1</A2>
<Preferred_First_Name>aaaa</Preferred_First_Name>
<Preferred_Last_Name>yyyy</Preferred_Last_Name>
<location>xxxx</location>
<ID>12345</ID>
</Row>
</Root>
I want to modify the above XML in such a way that Preferred_First_Name, Preferred_Last_Name and location node need to be under a new XML tag "Data".
The desired output should be like below,
<Root>
<Row>
<A1>0</A1>
<A2>1</A2>
<Data>
<Preferred_First_Name>aaaa</Preferred_First_Name>
<Preferred_Last_Name>yyyy</Preferred_Last_Name>
<location Descriptor="xxxx">
<ID type="ID">xxxx</ID>
<ID type="LocationID">xxxx</ID>
</location>
</Data>
<ID>12345</ID>
</Row>
</Root>
Can someone please help?
you can use below
<?xml version="1.0" encoding="utf-16"?>
<xsl:stylesheet xmlns:xalan="http://xml.apache.org/xalan" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" version="1.0">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes" />
<xsl:template match="Root">
<xsl:element name="Root">
<xsl:call-template name="Row" />
</xsl:element>
</xsl:template>
<xsl:template name="Row">
<xsl:element name="Row">
<xsl:copy-of select="Row/A1"/>
<xsl:copy-of select="Row/A2"/>
<xsl:element name="Data">
<xsl:copy-of select="Row/Preferred_First_Name"/>
<xsl:copy-of select="Row/Preferred_Last_Name"/>
<xsl:element name="location">
<xsl:attribute name="Descriptor">
<xsl:value-of select="Row/location"/>
</xsl:attribute>
<xsl:element name="ID">
<xsl:attribute name="type">
<xsl:value-of select="'ID'"/>
</xsl:attribute>
<xsl:value-of select="Row/location"/>
</xsl:element>
<xsl:element name="ID">
<xsl:attribute name="type">
<xsl:value-of select="'LocationID'"/>
</xsl:attribute>
<xsl:value-of select="Row/location"/>
</xsl:element>
</xsl:element>
<xsl:copy-of select="Row/ID"/>
</xsl:element>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Please let me know this helps you
If it is okay to put <ID> after <Data> mentioned by Tim C, then the optimized solution can be:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<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>
<xsl:template match="Row">
<Row>
<xsl:apply-templates select="child::node()[not(self::Preferred_First_Name or self::Preferred_Last_Name
or self::location)]" />
<Data>
<xsl:apply-templates select="child::node()[self::Preferred_First_Name or self::Preferred_Last_Name
or self::location]"/>
</Data>
</Row>
</xsl:template>
</xsl:stylesheet>

How to rename root and children element XSLT

How do I rename root 'channel' and children 'item' to 'Records' and 'Record' respectively?
Input
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<item>
<title>A</title>
<link>/news/view/25857/A.html</link>
<guid isPermaLink="true">/news/view/25857/A.html</guid>
<comments>/news/25857/A.html</comments>
<pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
<description><![CDATA[]]></description>
<category>headline,hacker,bank,cybercrime,data loss,fraud</category>
</item>
</channel>
</rss>
XSLT
<?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" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item/comments">
<taglink>
<xsl:text></xsl:text>
<xsl:value-of select="text()" />
</taglink>
</xsl:template>
<!--delimits values if separated by comma-->
<xsl:template match="item/category[contains(.,',')]">
<category>
<xsl:variable name="elementName" select="name(..)"/>
<xsl:call-template name="splitIntoElements">
<xsl:with-param name="baseName" select="name(..)" />
<xsl:with-param name="txt" select="." />
</xsl:call-template>
</category>
</xsl:template>
<xsl:template name="splitIntoElements">
<xsl:param name="baseName" />
<xsl:param name="txt" />
<xsl:param name="delimiter" select="','" />
<xsl:param name="index" select="1" />
<xsl:variable name="first" select="substring-before($txt, $delimiter)" />
<xsl:variable name="remaining" select="substring-after($txt, $delimiter)" />
<xsl:element name="value">
<xsl:choose>
<xsl:when test="$first">
<xsl:value-of select="$first" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$txt" />
</xsl:otherwise>
</xsl:choose>
</xsl:element>
<xsl:if test="$remaining">
<xsl:call-template name="splitIntoElements">
<xsl:with-param name="baseName" select="$baseName" />
<xsl:with-param name="txt" select="$remaining" />
<xsl:with-param name="index" select="$index" />
<xsl:with-param name="delimiter" select="$delimiter" />
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<item>
<title>A</title>
<link>/news/view/25857/A.html</link>
<guid isPermaLink="true">/news/view/25857/A.html</guid>
<taglink>/news/25857/A.html</taglink>
<pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
<description/>
<category>
<value>headline</value>
<value>hacker</value>
<value>bank</value>
<value>cybercrime</value>
<value>data loss</value>
<value>fraud</value>
</category>
</item>
</channel>
</rss>
Expected Output - There are many 'item' within 'channel' so there is expected be have many 'records' within 'record'
<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<Records>
<Record>
<title>A</title>
<link>/news/view/25857/A.html</link>
<guid isPermaLink="true">/news/view/25857/A.html</guid>
<taglink>/news/25857/A.html</taglink>
<pubDate>Sat, 03 Oct 2015 00:42:42 GMT</pubDate>
<description/>
<category>
<value>headline</value>
<value>hacker</value>
<value>bank</value>
<value>cybercrime</value>
<value>data loss</value>
<value>fraud</value>
</category>
</Record>
</Records>
</rss>
You can simply add a couple of templates that match element to be renamed, and spit out the new element name, for example :
<xsl:template match="channel">
<Records>
<xsl:apply-templates/>
</Records>
</xsl:template>
<xsl:template match="item">
<Record>
<xsl:apply-templates/>
</Record>
</xsl:template>

XSLT Saxon - Getting Latest node value

I am trying to find the last element in my input xml, which looks like
<ROOT>
<Items>
<Item>
<FieldTag>AMOUNT</FieldTag>
</Item>
<Item>
<FieldTag>CAT_TYPE</FieldTag>
</Item>
<Item>
<FieldTag>NUMBER</FieldTag>
</Item>
</Items>
<getResponse xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<MAIN>
<ArrayItem>
<NUMBER>123456789</NUMBER>
<CTRY>GB</CTRY>
<NAME>TEST NAME</NAME>
<RS>
<ArrayOfRSItem>
<DATE_1>2014-12-12T10:14:02-05:00</DATE_1>
<DATE_2>2014-12-12T10:13:53-05:00</DATE_2>
<AMOUNT>11111</AMOUNT>
</ArrayOfRSItem>
<ArrayOfRSItem>
<DATE_1>2014-12-13T17:16:19-05:00</DATE_1>
<DATE_2>2014-12-13T16:33:07-05:00</DATE_2>
<AMOUNT>22222</AMOUNT>
</ArrayOfRSItem>
<ArrayOfRSItem>
<DATE_1>2014-12-12T10:14:02-05:00</DATE_1>
<DATE_2>2014-12-12T10:13:53-05:00</DATE_2>
<CAT_TYPE>10000</CAT_TYPE>
</ArrayOfRSItem>
<ArrayOfRSItem>
<DATE_1>2014-12-13T17:16:19-05:00</DATE_1>
<DATE_2>2014-12-13T16:33:07-05:00</DATE_2>
<CAT_TYPE>20000</CAT_TYPE>
</ArrayOfRSItem>
</RS>
</ArrayItem>
</MAIN>
</getResponse>
</ROOT>
and I use following XSLT file for transformation
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="2.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:urn="urn:iso:std:iso:20022:tech:xsd:pain.001.001.02"
xmlns:func="http://www.test.com/"
exclude-result-prefixes="urn func" xmlns:saxon="http://saxon.sf.net/"
extension-element-prefixes="saxon">
<xsl:output method ="xml" indent="yes" omit-xml-declaration="yes" />
<xsl:template match="/">
<xsl:if test="count(/ROOT/getResponse/MAIN/ArrayItem) > 0" >
<Data>
<xsl:for-each select="/ROOT/getResponse/MAIN/ArrayItem" >
<xsl:element name="NodeItem">
<xsl:variable name="TempNo" select="NUMBER"/>
<xsl:element name="Number">
<xsl:value-of select="NUMBER"/>
</xsl:element>
<xsl:element name="Country">
<xsl:value-of select="CTRY"/>
</xsl:element>
<xsl:element name="Name">
<xsl:value-of select="NAME"/>
</xsl:element>
<xsl:element name="DataChanges">
<xsl:for-each select="./RS/ArrayOfRSItem/*" >
<xsl:if test="name()!='DATE_1' and name()!='DATE_2' ">
<xsl:variable name="ChangedNodeName" select="name()"></xsl:variable>
<xsl:for-each select="/ROOT/Items/Item">
<xsl:variable name="DisplayNodeName" select="FieldTag"></xsl:variable>
<xsl:if test="$DisplayNodeName = $ChangedNodeName">
<xsl:element name="FieldItem">
<xsl:attribute name="Tag">
<xsl:value-of select="FieldTag"/>
</xsl:attribute>
<xsl:variable name="NodeFullText" select="concat('../../getResponse/MAIN/ArrayItem[NUMBER=',$TempNo,']/RS/ArrayOfRSItem/',$DisplayNodeName)"/>
<xsl:attribute name="Value">
<xsl:value-of select="saxon:evaluate($NodeFullText)"/>
</xsl:attribute>
</xsl:element>
</xsl:if>
</xsl:for-each>
</xsl:if>
</xsl:for-each>
</xsl:element>
</xsl:element>
</xsl:for-each>
</Data>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
to get the desired output in the form of
<Data>
<NodeItem>
<Number>123456789</Number>
<Country>GB</Country>
<Name>TEST NAME</Name>
<DataChanges>
<FieldItem Tag="AMOUNT" Value="22222"/>
<FieldItem Tag="CAT_TYPE" Value="20000"/>
</DataChanges>
</NodeItem>
</Data>
But I am getting the output xml as
<Data>
<NodeItem>
<Number>123456789</Number>
<Country>GB</Country>
<Name>TEST NAME</Name>
<DataChanges>
<FieldItem Tag="AMOUNT" Value="11111 22222"/>
<FieldItem Tag="AMOUNT" Value="11111 22222"/>
<FieldItem Tag="CAT_TYPE" Value="10000 20000"/>
<FieldItem Tag="CAT_TYPE" Value="10000 20000"/>
</DataChanges>
</NodeItem>
</Data>
I need to get the latest AMOUNT entry and CAT_TYPE. Any help or guidance would be much appreciated. Thanks in advance.
Couldn't this be (much) simpler?
XSLT 1.0 (or 2.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="/">
<Data>
<xsl:apply-templates select="ROOT/getResponse/MAIN/ArrayItem"/>
</Data>
</xsl:template>
<xsl:template match="ArrayItem">
<NodeItem>
<Number><xsl:value-of select="NUMBER"/></Number>
<Country><xsl:value-of select="CTRY"/></Country>
<Name><xsl:value-of select="NAME"/></Name>
<DataChanges>
<xsl:apply-templates select="RS/ArrayOfRSItem/AMOUNT">
<xsl:sort select="../DATE_1" data-type="text" order="descending"/>
</xsl:apply-templates>
<xsl:apply-templates select="RS/ArrayOfRSItem/CAT_TYPE">
<xsl:sort select="../DATE_1" data-type="text" order="descending"/>
</xsl:apply-templates>
</DataChanges>
</NodeItem>
</xsl:template>
<xsl:template match="AMOUNT | CAT_TYPE">
<xsl:if test="position() = 1">
<FieldItem Tag="{local-name()}" Value="{.}"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Result, when applied to your example input:
<?xml version="1.0" encoding="utf-8"?>
<Data>
<NodeItem>
<Number>123456789</Number>
<Country>GB</Country>
<Name>TEST NAME</Name>
<DataChanges>
<FieldItem Tag="AMOUNT" Value="22222"/>
<FieldItem Tag="CAT_TYPE" Value="20000"/>
</DataChanges>
</NodeItem>
</Data>

Duplicate node() for every token in grandchild and replace element text of the grandchild by token in XSLT 1.0?

My (simplified) input XML looks like this:
<?xml version="1.0" encoding="utf-8"?>
<root>
<recordList>
<record>
<id>16</id>
<MaterialGroup>
<material>
<term>metal, glass</term>
</material>
<material.notes />
<material.part>body</material.part>
</MaterialGroup>
<MaterialGroup>
<material>
<term>wood</term>
</material>
<material.notes>fragile</material.notes>
<material.part>lid</material.part>
</MaterialGroup>
</record>
<record>
...
</record>
</recordList>
</root>
Note that term may contain a comma-separated list of multiple materials (metal, glass).
Desired output:
I want to split the material/term and need to duplicate the grandparent Material with all attributes and nodes for that.
<?xml version="1.0" encoding="utf-8"?>
...
<MaterialGroup>
<material>
<term>metal</term>
</material>
<material.notes />
<material.part>body</material.part>
</MaterialGroup>
<MaterialGroup>
<material>
<term>glass</term>
</material>
<material.notes />
<material.part>body</material.part>
</MaterialGroup>
<MaterialGroup>
<material>
<term>wood</term>
</material>
<material.notes>fragile</material.notes>
<material.part>lid</material.part>
</MaterialGroup>
</record>
...
The first MaterialGroup is copied for every token in the delimited grandchild element material/term, and the term text is set to the token text. material.parts and material.notes can be copied unchanged.
My stylesheet:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:variable name="separator" select="','"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="material/term" mode="s">
<xsl:param name="split_term"/>
<xsl:value-of select="$split_term"/>
</xsl:template>
<xsl:template match="MaterialGroup" name="tokenize">
<xsl:param name="text" select="material/term"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="material/term" mode="s">
<xsl:with-param name="split_term">
<xsl:value-of select="normalize-space($text)"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates/>
<xsl:apply-templates select="material/term" mode="s">
<xsl:with-param name="split_term">
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</xsl:with-param>
</xsl:apply-templates>
</xsl:copy>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Actual output:
<?xml version="1.0" encoding="utf-8"?>
<root>
<recordList>
<record>
<id>16</id>
<MaterialGroup>
<material>
<term>metal, glass</term>
</material>
<material.notes />
<material.part>body</material.part>
metal
</MaterialGroup>
<MaterialGroup>
<material>
<term>metal, glass</term>
</material>
<material.notes />
<material.part>body</material.part>
glass
</MaterialGroup>
<MaterialGroup>
<material>
<term>wood</term>
</material>
<material.notes>fragile</material.notes>
<material.part>lid</material.part>
wood
</MaterialGroup>
</record>
<record>
...
</record>
</recordList>
</root>
The tokens (metal, glass) occur as text elements as MaterialGroup children, below material.parts. The text element where it should actually appear (material/term) is unchanged.
I looked at couple solutions to similar problems, but no success:
https://stackoverflow.com/a/5480198/2044940
https://stackoverflow.com/a/10430719/2044940
http://codesequoia.wordpress.com/2012/02/15/xslt-example-add-a-new-node-to-elements/
...
Any ideas?
Edit: Solution by Martin, without modes as suggested by michael:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="separator" select="', '"/>
<xsl:template match="#* | node()">
<xsl:param name="term"/>
<xsl:copy>
<xsl:apply-templates select="#* | node()">
<xsl:with-param name="term" select="$term"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="material/term">
<xsl:param name="term"/>
<xsl:copy>
<xsl:value-of select="$term"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MaterialGroup" name="tokenize">
<xsl:param name="text" select="material/term"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:copy>
<xsl:apply-templates>
<xsl:with-param name="term" select="$text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates>
<xsl:with-param name="term" select="substring-before($text, $separator)"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
I think you need to pass your term around:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl"
>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="separator" select="', '"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#* | node()" mode="s">
<xsl:param name="term"/>
<xsl:copy>
<xsl:apply-templates select="#* | node()" mode="s">
<xsl:with-param name="term" select="$term"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="material/term" mode="s">
<xsl:param name="term"/>
<xsl:copy>
<xsl:value-of select="$term"/>
</xsl:copy>
</xsl:template>
<xsl:template match="MaterialGroup" name="tokenize">
<xsl:param name="text" select="material/term"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<xsl:copy>
<xsl:apply-templates mode="s">
<xsl:with-param name="term" select="$text"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:when>
<xsl:otherwise>
<xsl:copy>
<xsl:apply-templates mode="s">
<xsl:with-param name="term" select="substring-before($text, $separator)"/>
</xsl:apply-templates>
</xsl:copy>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
That way, with your input, I get
<root>
<recordList>
<record>
<id>16</id>
<MaterialGroup>
<material>
<term>metal</term>
</material>
<material.notes/>
<material.part>body</material.part>
</MaterialGroup>
<MaterialGroup>
<material>
<term>glass</term>
</material>
<material.notes/>
<material.part>body</material.part>
</MaterialGroup>
<MaterialGroup>
<material>
<term>wood</term>
</material>
<material.notes>fragile</material.notes>
<material.part>lid</material.part>
</MaterialGroup>
</record>
<record>
...
</record>
</recordList>
</root>

xslt: how to normalize numerical element

I would like to add an element to existing XML file with normalized value of existing element in the XML.
Any help would be extremely appreciated.
regards
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<c>32</c>
<d>34</d>
</Results>
</top>
In the sample input xml above, I would like to add "b_nom" elements to each of the results where the value is (b)/(minimum of 'b' grouped with a). The expected output is as below
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<b_nom>1.66</b_nom>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>no</a>
<b>8</b>
<b_nom>1.33</b_nom>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>no</a>
<b>6</b>
<b_nom>1</b_nom>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<b_nom>1</b_nom>
<c>32</c>
<d>34</d>
</Results>
</top>
I think you want
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="k1" match="Results" use="a"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:call-template name="identity"/>
<xsl:variable name="min">
<xsl:for-each select="key('k1', ../a)">
<xsl:sort select="b" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="b"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<b_nom>
<xsl:value-of select="format-number(. div $min, '0.##')"/>
</b_nom>
</xsl:template>
</xsl:stylesheet>
Here is a changed version of the stylesheet that takes a couple of values identifying a group into account:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:data="http:://example.com/data"
exclude-result-prefixes="data">
<data:data xmlns="">
<group>
<key>no</key>
<values>
<value>no</value>
<value>n</value>
<value>0</value>
</values>
</group>
<group>
<key>yes</key>
<values>
<value>yes</value>
<value>y</value>
<value>1</value>
</values>
</group>
</data:data>
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:variable name="main-input" select="/"/>
<xsl:variable name="groups" select="document('')/xsl:stylesheet/data:data/group"/>
<xsl:template match="#* | node()" name="identity">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="b">
<xsl:call-template name="identity"/>
<xsl:variable name="this" select="."/>
<xsl:variable name="min">
<xsl:for-each select="$main-input//Results[a = $groups/values[value = $this/../a]/value]">
<xsl:sort select="b" data-type="number"/>
<xsl:if test="position() = 1">
<xsl:value-of select="b"/>
</xsl:if>
</xsl:for-each>
</xsl:variable>
<b_nom>
<xsl:value-of select="format-number(. div $min, '0.##')"/>
</b_nom>
</xsl:template>
</xsl:stylesheet>
That transforms the input
<?xml version="1.0" encoding="UTF-8"?>
<top>
<Results>
<a>no</a>
<b>10</b>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>n</a>
<b>8</b>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>0</a>
<b>6</b>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<c>32</c>
<d>34</d>
</Results>
</top>
into the output
<top>
<Results>
<a>no</a>
<b>10</b>
<b_nom>1.67</b_nom>
<c>12</c>
<d>9</d>
</Results>
<Results>
<a>n</a>
<b>8</b>
<b_nom>1.33</b_nom>
<c>50</c>
<d>12</d>
</Results>
<Results>
<a>0</a>
<b>6</b>
<b_nom>1</b_nom>
<c>55</c>
<d>56</d>
</Results>
<Results>
<a>yes</a>
<b>23</b>
<b_nom>1</b_nom>
<c>32</c>
<d>34</d>
</Results>
</top>