Using XSLT to generate nodes based on pipe-delimited attributes - xslt

(first off: I'm terribly sorry that you have to look at this document structure; it's hideous)
I have the following XML document:
<MENUS>
<MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
<RECIPES>
<NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
<RECIPE id="6461-200" plucode="" shortname="Chipotle Spinach" numservings="100" portion="4 ounces" isselected="0" ismainitem="0" group="On the Side" publishingdescription="Chipotle Spinach" publishingtext="" enticingdescription="" price="1.53" category="Vegetables" productionarea="Hot Production" nutrients="152|2.3|13.8|6.5|0|74|346|1.85|" nutrientsuncertain="0|0|0|0|0|0|0|0|">Chipotle Spinach,4U</RECIPE>
<RECIPE id="6586-300" plucode="" shortname="Asiago Crusted Chix" numservings="120" portion="3-3/4 ounces" isselected="0" ismainitem="0" group="Main Fare" publishingdescription="Asiago Crusted Chicken" publishingtext="" enticingdescription="" price="2.25" category="Chicken" productionarea="Hot Production" nutrients="203|19.6|7.6|13.2|56|124|387|1.37|" nutrientsuncertain="0|0|0|0|0|0|0|0|">Asiago Crusted Chicken,4U</RECIPE>
<!-- any number of <RECIPE> elements ... -->
</RECIPES>
</MENUS>
The <NUTRIENTS> element contains a pipe-delimited string; the components of this string need to somehow become elements for each <RECIPE>. Furthermore, the values of these new elements are specified by looking at the corresponding position within the pipe-delimited string found in <RECIPE>\<nutrients>.
The overall structure I'm shooting for is:
All of the attributes for a <RECIPE> element are converted into child elements.
The elements of <RECIPE>/<nutrients> map to the same position
within the <NUTRIENTS> element.
Using XSLT 1.0.
So, here would be my expected structure:
<?xml version="1.0"?>
<MENUS>
<MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
<RECIPES>
<NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
<RECIPE>
<id>6461-200</id>
<plucode/>
<shortname>Chipotle Spinach</shortname>
<numservings>100</numservings>
<portion>4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>On the Side</group>
<publishingdescription>Chipotle Spinach</publishingdescription>
<publishingtext/>
<enticingdescription/>
<price>1.53</price>
<category>Vegetables</category>
<productionarea>Hot Production</productionarea>
<nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
<ProteinProteingm>2.3</ProteinProteingm>
<FatFatgm>13.8</FatFatgm>
<CarbsTotalCarbohydrates>6.5</CarbsTotalCarbohydrates>
<CholestrolCholestrolmg>0</CholestrolCholestrolmg>
<CalciumCalciummg>74</CalciumCalciummg>
<SodiumSodiummg>346</SodiumSodiummg>
<IronIronmg>1.85</IronIronmg>
</RECIPE>
<RECIPE>
<id>6586-300</id>
<plucode/>
<shortname>Asiago Crusted Chix</shortname>
<numservings>120</numservings>
<portion>3-3/4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>Main Fare</group>
<publishingdescription>Asiago Crusted Chicken</publishingdescription>
<publishingtext/>
<enticingdescription/>
<price>2.25</price>
<category>Chicken</category>
<productionarea>Hot Production</productionarea>
<nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<CaloriesEnergykcalkcal>203</CaloriesEnergykcalkcal>
<ProteinProteingm>19.6</ProteinProteingm>
<FatFatgm>7.6</FatFatgm>
<CarbsTotalCarbohydrates>13.2</CarbsTotalCarbohydrates>
<CholestrolCholestrolmg>56</CholestrolCholestrolmg>
<CalciumCalciummg>124</CalciumCalciummg>
<SodiumSodiummg>387</SodiumSodiummg>
<IronIronmg>1.37</IronIronmg>
</RECIPE>
<!-- ... -->
</RECIPES>
</MENUS>
(notice, again, that I don't care about the field names we use for these new data points [which begin after <nutrientsuncertain>]; however, bonus points if you would like to show me how to relatively easily specify some sort of array, for lack of a better term, of field names)
Here's my current XSLT, which achieves goal #1; it's goal #2 that I'm stumped on:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Template #1 - Identity Transform -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Template #2 - Convert all of a <RECIPE> element's attributes to child elements -->
<xsl:template match="RECIPE/#*">
<xsl:element name="{name()}">
<xsl:value-of select="." />
</xsl:element>
</xsl:template>
<!-- Template #3 - Remove extraneous text from each <RECIPE> -->
<xsl:template match="RECIPE/text()" />
</xsl:stylesheet>
That's it. Thanks so much for your help!

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl"
version="1.0">
<xsl:output method="xml" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="*|#*|text()">
<xsl:copy>
<xsl:apply-templates select="*|#*|text()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RECIPE">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:variable name="nutrients-table-tmp">
<xsl:call-template name="tokenize-table">
<xsl:with-param name="text" select="../NUTRIENTS/text()"/>
<xsl:with-param name="delimiter-row" select="'|'"/>
<xsl:with-param name="delimiter-col" select="'~'"/>
</xsl:call-template>
</xsl:variable>
<xsl:variable name="nutrients-table" select="exsl:node-set($nutrients-table-tmp)/table"/>
<xsl:variable name="nutrients">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="#nutrients"/>
<xsl:with-param name="delimiter" select="'|'"/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($nutrients)/token">
<xsl:variable name="pos" select="position()"/>
<xsl:variable name="value" select="text()"/>
<xsl:variable name="row" select="$nutrients-table/row[$pos]"/>
<xsl:variable name="name" select="$row/cell[1]/text()"/>
<xsl:variable name="description" select="$row/cell[2]/text()"/>
<xsl:variable name="unit" select="$row/cell[3]/text()"/>
<xsl:element name="{$name}">
<xsl:attribute name="unit">
<xsl:value-of select="$unit"/>
</xsl:attribute>
<xsl:value-of select="$value"/>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
<xsl:template match="RECIPE/#*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="' '"/>
<xsl:choose>
<xsl:when test="contains($text,$delimiter)">
<token>
<xsl:value-of select="substring-before($text,$delimiter)"/>
</token>
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:when test="$text">
<token>
<xsl:value-of select="$text"/>
</token>
</xsl:when>
</xsl:choose>
</xsl:template>
<xsl:template name="tokenize-table">
<xsl:param name="text"/>
<xsl:param name="delimiter-row"/>
<xsl:param name="delimiter-col"/>
<xsl:variable name="rows">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="$text"/>
<xsl:with-param name="delimiter" select="$delimiter-row"/>
</xsl:call-template>
</xsl:variable>
<table>
<xsl:for-each select="exsl:node-set($rows)/token">
<xsl:variable name="items">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="text()"/>
<xsl:with-param name="delimiter" select="$delimiter-col"/>
</xsl:call-template>
</xsl:variable>
<row>
<xsl:for-each select="exsl:node-set($items)/token">
<cell>
<xsl:value-of select="text()"/>
</cell>
</xsl:for-each>
</row>
</xsl:for-each>
</table>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<MENUS>
<MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
<RECIPES>
<NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
<RECIPE>
<id>6461-200</id>
<plucode/>
<shortname>Chipotle Spinach</shortname>
<numservings>100</numservings>
<portion>4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>On the Side</group>
<publishingdescription>Chipotle Spinach</publishingdescription>
<publishingtext/>
<enticingdescription/>
<price>1.53</price>
<category>Vegetables</category>
<productionarea>Hot Production</productionarea>
<nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<Calories unit="kcal">152</Calories>
<Protein unit="gm">2.3</Protein>
<Fat unit="gm">13.8</Fat>
<Carbs unit="gm">6.5</Carbs>
<Cholestrol unit="mg">0</Cholestrol>
<Calcium unit="mg">74</Calcium>
<Sodium unit="mg">346</Sodium>
<Iron unit="mg">1.85</Iron>
</RECIPE>
<RECIPE>
<id>6586-300</id>
<plucode/>
<shortname>Asiago Crusted Chix</shortname>
<numservings>120</numservings>
<portion>3-3/4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>Main Fare</group>
<publishingdescription>Asiago Crusted Chicken</publishingdescription>
<publishingtext/>
<enticingdescription/>
<price>2.25</price>
<category>Chicken</category>
<productionarea>Hot Production</productionarea>
<nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<Calories unit="kcal">203</Calories>
<Protein unit="gm">19.6</Protein>
<Fat unit="gm">7.6</Fat>
<Carbs unit="gm">13.2</Carbs>
<Cholestrol unit="mg">56</Cholestrol>
<Calcium unit="mg">124</Calcium>
<Sodium unit="mg">387</Sodium>
<Iron unit="mg">1.37</Iron>
</RECIPE>
</RECIPES>
</MENUS>

I am not 100% sure I understand your question, but I think you should take a look at the XSL tokenize function. If you combine this in a variable with the position() function you should be able to achieve that correlated output?
To further add to this, you can combine the name() with a (replace for 2.0/translate for 1.0) to get the element name automatically) within a for-each, extracting the positions.

Refer my implementation:-
XSLT File:
<?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:stylesheet>-->
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="no" indent="yes"/>
<xsl:strip-space elements="*"/>
<!-- Template #1 - Identity Transform -->
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<!-- Template #2 - Convert all of a <RECIPE> element's attributes to child elements -->
<xsl:template match="RECIPE/#*">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
</xsl:template>
<!-- Template #3 - Remove extraneous text from each <RECIPE> -->
<xsl:template match="RECIPE/text()"/>
<!-- Identifying the last attribute -->
<xsl:template match="RECIPE/#*[position()=last()]">
<xsl:element name="{name()}">
<xsl:value-of select="."/>
</xsl:element>
<!--- Call the String Tokenize template -->
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="/MENUS/RECIPES/NUTRIENTS/text()"/>
<xsl:with-param name="strValue" select="/MENUS/RECIPES/RECIPE/#nutrients"/>
</xsl:call-template>
</xsl:template>
<!--- String Tokenize -->
<xsl:template name="tokenize">
<xsl:param name="string"/>
<xsl:param name="strValue"/>
<xsl:param name="delimiter" select="'|'"/>
<xsl:choose>
<xsl:when test="$delimiter and contains($string, $delimiter) and contains($strValue, $delimiter)">
<xsl:variable name="subbef" select="translate(substring-before($string, $delimiter), '()~ ', '')"/>
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:value-of select="$subbef"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="substring-before($strValue, $delimiter)"/>
<xsl:text disable-output-escaping="yes"></</xsl:text>
<xsl:value-of select="$subbef"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:call-template name="tokenize">
<xsl:with-param name="string" select="translate(substring-after($string, $delimiter), '()~ ', '')"/>
<xsl:with-param name="strValue" select="substring-after($strValue, $delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:if test="string($string) and string($strValue)">
<xsl:text disable-output-escaping="yes"><</xsl:text>
<xsl:value-of select="$string"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
<xsl:value-of select="$strValue"/>
<xsl:text disable-output-escaping="yes"></</xsl:text>
<xsl:value-of select="$string"/>
<xsl:text disable-output-escaping="yes">></xsl:text>
</xsl:if>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
OUTPUT:
<?xml version="1.0" encoding="UTF-8"?>
<MENUS>
<MENU id="192748" servedate="20120213" mealid="3" mealname="Lunch" menuname="Cafeteria" menuid="26" totalcount="200">Cafeteria</MENU>
<RECIPES>
<NUTRIENTS>Calories~Energy (kcal)~kcal|Protein~Protein~gm|Fat~Fat~gm|Carbs~Total Carbohydrates~gm|Cholestrol~Cholesterol~mg|Calcium~Calcium~mg|Sodium~Sodium~mg|Iron~Iron~mg|</NUTRIENTS>
<RECIPE>
<id>6461-200</id>
<plucode />
<shortname>Chipotle Spinach</shortname>
<numservings>100</numservings>
<portion>4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>On the Side</group>
<publishingdescription>Chipotle Spinach</publishingdescription>
<publishingtext />
<enticingdescription />
<price>1.53</price>
<category>Vegetables</category>
<productionarea>Hot Production</productionarea>
<nutrients>152|2.3|13.8|6.5|0|74|346|1.85|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
<ProteinProteingm>2.3</ProteinProteingm>
<FatFatgm>13.8</FatFatgm>
<CarbsTotalCarbohydratesgm>6.5</CarbsTotalCarbohydratesgm>
<CholestrolCholesterolmg>0</CholestrolCholesterolmg>
<CalciumCalciummg>74</CalciumCalciummg>
<SodiumSodiummg>346</SodiumSodiummg>
<IronIronmg>1.85</IronIronmg>
</RECIPE>
<RECIPE>
<id>6586-300</id>
<plucode />
<shortname>Asiago Crusted Chix</shortname>
<numservings>120</numservings>
<portion>3-3/4 ounces</portion>
<isselected>0</isselected>
<ismainitem>0</ismainitem>
<group>Main Fare</group>
<publishingdescription>Asiago Crusted Chicken</publishingdescription>
<publishingtext />
<enticingdescription />
<price>2.25</price>
<category>Chicken</category>
<productionarea>Hot Production</productionarea>
<nutrients>203|19.6|7.6|13.2|56|124|387|1.37|</nutrients>
<nutrientsuncertain>0|0|0|0|0|0|0|0|</nutrientsuncertain>
<CaloriesEnergykcalkcal>152</CaloriesEnergykcalkcal>
<ProteinProteingm>2.3</ProteinProteingm>
<FatFatgm>13.8</FatFatgm>
<CarbsTotalCarbohydratesgm>6.5</CarbsTotalCarbohydratesgm>
<CholestrolCholesterolmg>0</CholestrolCholesterolmg>
<CalciumCalciummg>74</CalciumCalciummg>
<SodiumSodiummg>346</SodiumSodiummg>
<IronIronmg>1.85</IronIronmg>
</RECIPE>
<!-- any number of <RECIPE> elements ... -->
</RECIPES>
</MENUS>

Related

XSLT to handle quotes and Pipe Delimited symbol

Experts, i need to write XSLT 1.0 code to remove the quotes for multiple conditions.
CASE1: Remove the double quotes
CASE2: Remove the double quotes + delete the PIPE symbol inside that double quotes (IF exist)
CASE3: Remove Single quote " from the input field.
Input:
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounting xmlns:ns0="http://sample.com">
<Record>
<DRCR>"DR"</DRCR>
<GLREFERENCE>"TEST|CASE"</GLREFERENCE>
<GLVALUEDATE>EXAM"PLE</GLVALUEDATE>
<GLACCOUNTNUMBER>"1160</GLACCOUNTNUMBER>
<GLEXAMPLE>123</GLEXAMPLE>
<GLEXAMPLE1>EXTRACT|2021-06-16|2853|1308026.7500|1176</GLEXAMPLE1>
</Record>
</ns0:Accounting>
** Desired Output:**
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<ns0:Accounting xmlns:ns0="http://sample.com">
<Record>
<DRCR>DR</DRCR>
<GLREFERENCE>TEST CASE</GLREFERENCE>
<GLVALUEDATE>EXAMPLE</GLVALUEDATE>
<GLACCOUNTNUMBER>1160</GLACCOUNTNUMBER>
<GLEXAMPLE>123</GLEXAMPLE>
<GLEXAMPLE1>EXTRACT|2021-06-16|2853|1308026.7500|1176</GLEXAMPLE1>
</Record>
</ns0:Accounting>
** XSLT I tried:**
<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="text()">
<xsl:call-template name="process">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:template>
<xsl:template name="process">
<xsl:param name="text"/>
<xsl:choose>
<xsl:when test="contains($text, '"')">
<xsl:value-of select="substring-before($text, '"')"/>
<xsl:value-of select="translate(substring-before(substring-after($text, '"'), '"'), '|', '')"/>
<xsl:call-template name="process">
<xsl:with-param name="text" select="substring-after(substring-after($text, '"'), '"')"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
This XSLT not handling case 3, which has single quote in the input field. Please assist here..
Maybe something like this could work for you:
XSLT 1.0 (+ EXSLT node-set function)
<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: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="text()">
<xsl:choose>
<xsl:when test="contains(., '"')">
<xsl:variable name="tokens">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="."/>
</xsl:call-template>
</xsl:variable>
<xsl:for-each select="exsl:node-set($tokens)/token">
<xsl:choose>
<xsl:when test="(position()=1 or position()=last()) and last() > 1">
<xsl:value-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="translate(., '|', '')"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'"'"/>
<xsl:variable name="token" select="substring-before(concat($text, $delimiter), $delimiter)" />
<xsl:if test="$token">
<token>
<xsl:value-of select="$token"/>
</token>
</xsl:if>
<xsl:if test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Note that this does not check the parity of the quotation marks. Any vertical bar character that is both preceded and followed by a quotation mark will be removed. For example, an input of:
<EXAMPLE>abc|123"def|456"ghi|789"jkl|012</EXAMPLE>
will be transformed to:
<EXAMPLE>abc|123def456ghi789jkl|012</EXAMPLE>

How to separate the values by tag using XSLT Transformation/ loop the tag

I am trying to separate the map output values by tag. Right now I am getting all the values in one (BOM) tag. I want the output separated by each BOMTransactionType(Deleted/Added). I am using XSLT Transformation to separate it. Could anyone please let me know how can I do that. Thanks.
XSLT Transformation Logic in DataProcess Shape:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/*">
<PLMData>
<ChangeOrders>
<AffectedItems>
<BOM>
<xsl:apply-templates/>
</BOM>
</AffectedItems>
</ChangeOrders>
</PLMData>
</xsl:template>
//Split the ItemNumber
<xsl:template match="ItemNumber/text()" name="split">
<xsl:param name="pText" select="."/>
<xsl:if test="$pText">
<xsl:element name="ItemNumber">
<xsl:value-of select= "substring-before(concat($pText, ','), ',')"/>
</xsl:element>
<xsl:call-template name="split">
<xsl:with-param name="pText" select="substring-after($pText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
//Split the BOMTransactionType
<xsl:template match="BOM/BOMTransactionType/text()" name="split1">
<xsl:param name="bText" select="."/>
<xsl:param name="bOrd" select="1"/>
<xsl:if test="$bText">
<xsl:element name="BOMTransactionType">
<xsl:value-of select= "substring-before(concat($bText, ','), ',')"/>
</xsl:element>
<xsl:call-template name="split1">
<xsl:with-param name="bText" select="substring-after($bText, ',')"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Input:
<PLMData>
<ChangeOrders>
<AffectedItems>
<BOM>
<ItemNumber>P00001,020-00003-01</ItemNumber>
<BOMTransactionType>Added,Deleted</BOMTransactionType>
</BOM>
</AffectedItems>
</ChangeOrders>
</PLMData>
Output:
<PLMData>
<ChangeOrders>
<AffectedItems>
<BOM>
<ItemNumber>P00001</ItemNumber>
<ItemNumber>020-00003-01</ItemNumber>
<BOMTransactionType>Added</BOMTransactionType>
<BOMTransactionType>Deleted</BOMTransactionType>
</BOM>
</AffectedItems>
</ChangeOrders>
</PLMData>
Expected Output:
<PLMData>
<ChangeOrders>
<AffectedItems>
<BOM>
<ItemNumber>P00001</ItemNumber>
<BOMTransactionType>Added</BOMTransactionType>
</BOM>
<BOM>
<ItemNumber>020-00003-01</ItemNumber>
<BOMTransactionType>Deleted</BOMTransactionType>
</BOM>
</AffectedItems>
</ChangeOrders>
</PLMData>
I would do it this way:
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="BOM">
<xsl:call-template name="tokenize">
<xsl:with-param name="item-numbers" select="ItemNumber"/>
<xsl:with-param name="transaction-types" select="BOMTransactionType"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="tokenize">
<xsl:param name="item-numbers"/>
<xsl:param name="transaction-types"/>
<xsl:param name="delimiter" select="','"/>
<BOM>
<ItemNumber>
<xsl:value-of select="substring-before(concat($item-numbers, $delimiter), $delimiter)" />
</ItemNumber>
<BOMTransactionType>
<xsl:value-of select="substring-before(concat($transaction-types, $delimiter), $delimiter)" />
</BOMTransactionType>
</BOM>
<xsl:if test="contains($item-numbers, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="tokenize">
<xsl:with-param name="item-numbers" select="substring-after($item-numbers, $delimiter)"/>
<xsl:with-param name="transaction-types" select="substring-after($transaction-types, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

How to Iterate through nodes and skip duplicate nodes with same value using a variable

i have a xml like,
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>xxx</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">ABC/DEF/123</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>yyy</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">LMN/OPQ/123</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>mmm</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">XYZ/GHY/456</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>nnn</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">AJK/UTL/456</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
My xslt,
<xsl:template name="substring-after-last">
<xsl:param name="string" />
<xsl:param name="delimiter" />
<xsl:choose>
<xsl:when test="contains($string, $delimiter)">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string"
select="substring-after($string, $delimiter)" />
<xsl:with-param name="delimiter" select="$delimiter" />
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$string" />
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:for-each select="select="//DESIGN-FUNCTION-PROTOTYPE/ea:TYPE-TREF[#TYPE='DESIGN-FUNCTION-TYPE']">
<xsl:variable name="myVar" select="current()"/>
<xsl:variable name="taskName" select="../ea:SHORT-NAME"/>
<xsl:variable name="Var7">
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="$myVar" />
<xsl:with-param name="delimiter" select="'/'" />
</xsl:call-template>
</xsl:variable>
<varoutput>
<xsl:value-of select="$Var7"/>
</varoutput>
</xsl:for-each>
My intention here is to iterate all the 'DESIGN-FUNCTION-PROTOTYPE' elements and display the sub-string of 'TYPE-TREF' value, but if a sub-string of 'TYPE-TREF' value has already been read..i must skip that element.
Expected output,
123
456
And Not,
123
123
456
456
In general I should consider only the first occurrence and skip the rest.
To do this in pure XSLT 1.0, without relying on processor-specific extensions, you could do:
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:key name="k1" match="DESIGN-FUNCTION-PROTOTYPE" use="substring-after(substring-after(TYPE-TREF, '/'), '/')"/>
<xsl:template match="/Root">
<root>
<xsl:for-each select="DESIGN-FUNCTION-PROTOTYPE[count(. | key('k1', substring-after(substring-after(TYPE-TREF, '/'), '/'))[1]) = 1]">
<varoutput>
<xsl:value-of select="substring-after(substring-after(TYPE-TREF, '/'), '/')" />
</varoutput>
</xsl:for-each>
</root>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/bFN1y9s
This is of course assuming that the value you're after is always the third "token" in TYPE-TREF. Otherwise you would have to do something similar to your attempt:
XSLT 1.0 + EXSLT node-set() function
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
exclude-result-prefixes="exsl" >
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="k1" match="value" use="."/>
<xsl:template match="/Root">
<!-- EXTRACT VALUES -->
<xsl:variable name="values">
<xsl:for-each select="DESIGN-FUNCTION-PROTOTYPE">
<value>
<xsl:call-template name="last-token">
<xsl:with-param name="text" select="TYPE-TREF"/>
</xsl:call-template>
</value>
</xsl:for-each>
</xsl:variable>
<!-- OUTPUT -->
<root>
<xsl:for-each select="exsl:node-set($values)/value[count(. | key('k1', .)[1]) = 1]">
<varoutput>
<xsl:value-of select="." />
</varoutput>
</xsl:for-each>
</root>
</xsl:template>
<xsl:template name="last-token">
<xsl:param name="text"/>
<xsl:param name="delimiter" select="'/'"/>
<xsl:choose>
<xsl:when test="contains($text, $delimiter)">
<!-- recursive call -->
<xsl:call-template name="last-token">
<xsl:with-param name="text" select="substring-after($text, $delimiter)"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$text"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
Demo: https://xsltfiddle.liberty-development.net/bFN1y9s/1
Assuming you use Xalan you should have access to the EXSLT str:split function (http://xalan.apache.org/xalan-j/apidocs/org/apache/xalan/lib/ExsltStrings.html#split(java.lang.String,%20java.lang.String):
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:str="http://exslt.org/strings" exclude-result-prefixes="str" version="1.0">
<xsl:key name="group" match="DESIGN-FUNCTION-PROTOTYPE/TYPE-TREF"
use="str:split(., '/')[last()]"/>
<xsl:template match="Root">
<xsl:for-each select="DESIGN-FUNCTION-PROTOTYPE/TYPE-TREF[generate-id() = generate-id(key('group', str:split(., '/')[last()])[1])]">
<varoutput>
<xsl:value-of select="str:split(., '/')[last()]"/>
</varoutput>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
Transforms
<?xml version="1.0" encoding="UTF-8"?>
<Root>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>xxx</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">ABC/DEF/123</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>yyy</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">LMN/OPQ/123</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>mmm</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">XYZ/GHY/456</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
<DESIGN-FUNCTION-PROTOTYPE>
<SHORT-NAME>nnn</SHORT-NAME>
<TYPE-TREF TYPE="DESIGN-FUNCTION-PROTOTYPE">AJK/UTL/456</TYPE-TREF>
</DESIGN-FUNCTION-PROTOTYPE>
</Root>
into
<?xml version="1.0" encoding="UTF-8"?><varoutput>123</varoutput><varoutput>456</varoutput>
with Xalan Java and Xalan Java XSLTC.
Or, as suggested in a comment, if you simply want to find the distinct values you can use set:distinct e.g.
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:exsl="http://exslt.org/common"
xmlns:str="http://exslt.org/strings"
xmlns:set="http://exslt.org/sets"
exclude-result-prefixes="exsl str set"
version="1.0">
<xsl:template match="Root">
<xsl:variable name="split-values">
<xsl:for-each select="DESIGN-FUNCTION-PROTOTYPE/TYPE-TREF">
<xsl:copy-of select="str:split(., '/')[last()]"/>
</xsl:for-each>
</xsl:variable>
<xsl:copy-of select="set:distinct(exsl:node-set($split-values)/node())"/>
</xsl:template>
</xsl:stylesheet>

Split a string with "|" Char to columns

I'm a bit new to this language so I have several doubts.
I'm working to process an xml to display some data on pdf form.
But there a few strings that have "|" so I can split the data to display properly.
Here is the example of the input data:
<root>
<reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<RazonRef>Data1|Data2|Data3|Data4|Data5|Data6|Data7</RazonRef>
</reference>
</root>
In the output I need something like this so I can display in order in row with cells so data must be clear to read.
<root>
<Reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<Data1>Data1</Data1>
<Data2>Data2</Data2>
<Data3>Data3</Data3>
<Data4>Data4</Data4>
<Data5>Data5</Data5>
<Data6>Data6</Data6>
<Data7>Data7</Data7>
</Reference>
</root>
To do this I have been using other code that is from another question but can't find how to get the name to be updated or customized.
And the output I get is actually like this:
<root>
<Reference>
<NroLinRef>12</NroLinRef>
<CodRef>I20</CodRef>
<Data>Data1</Data>
<Data>Data2</Data>
<Data>Data3</Data>
<Data>Data4</Data>
<Data>Data5</Data>
<Data>Data6</Data>
<Data>Data7</Data>
</Reference>
</root>
This is the XSL i'm using
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Referencia/RazonRef" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="'|'"/>
<xsl:choose>
<xsl:when test="not(contains($text, $separator))">
<Data>
<xsl:value-of select="normalize-space($text)"/>
</Data>
</xsl:when>
<xsl:otherwise>
<Data>
<xsl:value-of select="normalize-space(substring-before($text, $separator))"/>
</Data>
<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>
How can I get the output I want?
The expected result can be achieved by applying the following stylesheet:
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="RazonRef" name="tokenize">
<xsl:param name="text" select="."/>
<xsl:param name="separator" select="'|'"/>
<xsl:param name="i" select="1"/>
<xsl:element name="Data{$i}">
<xsl:value-of select="substring-before(concat($text, $separator), $separator)"/>
</xsl:element>
<xsl:if test="contains($text, $separator)">
<xsl:call-template name="tokenize">
<xsl:with-param name="text" select="substring-after($text, $separator)"/>
<xsl:with-param name="i" select="$i + 1"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>

print xpath and value of element and attribute using XSLT

I would like to print path of element and attributes if any along with values using XSLT. e.g
XML :
<root>
<node attr='abc' module='try'>
<subnode>Roshan</subnode>
<subnode>Chetan</subnode>
<subnode>Jennifer</subnode>
</node>
</root>
Output :
/root##
/root/node##
/root/node/#attr##abc
/root/node/#module##try
/root/node/subnode[1]##Roshan
/root/node/subnode[2]##Chetan
/root/node/subnode[3]##Jennifer
I am trying with below snippet, but could only print path of element and it's value
<xsl:template match="*">
<xsl:for-each select="ancestor-or-self::*">
<xsl:value-of select="concat('/',local-name())" />
<xsl:if
test="(preceding-sibling::*|following-sibling::*)[local-name()=local-name(current())]">
<xsl:value-of
select="concat('[',count(preceding-sibling::*[local-name()=local-name(current())])+1,']')" />
</xsl:if>
<!-- <xsl:call-template name="attrData"></xsl:call-template> -->
</xsl:for-each>
<xsl:text>##</xsl:text>
<xsl:apply-templates select="node()" />
</xsl:template>
I am new to XSLT. Please help!!!!
I made the following XSLT and added also the [position] to the output. You can remove that if you need.
This gives this output:
/root[1]
/root[1]/node[1]
/root[1]/node[1]/#attr[1]##abc
/root[1]/node[1]/#module[1]##try
/root[1]/node[1]/subnode[1]##Roshan
/root[1]/node[1]/subnode[2]##Chetan
/root[1]/node[1]/subnode[3]##Jennifer
With this XSLT. With the two output template you can choose how to print the Xpath.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns="" version="2.0">
<xsl:output method="text" encoding="utf-8" />
<xsl:template match="/">
<xsl:apply-templates select="*"/>
</xsl:template>
<xsl:template match="*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="''"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="generateXPath">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:if test="not(empty(.))">
<xsl:variable name="thisXPath" select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:variable>
<xsl:apply-templates select="." mode="output">
<xsl:with-param name="previous" select="$previous"/>
</xsl:apply-templates>
<xsl:text>
</xsl:text>
<xsl:for-each select="*|#*">
<xsl:call-template name="generateXPath">
<xsl:with-param name="previous" select="$thisXPath"/>
</xsl:call-template>
</xsl:for-each>
</xsl:if>
</xsl:template>
<xsl:template match="*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath">
<xsl:value-of select="concat($previous, '/', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']')"></xsl:value-of>
<xsl:if test="not(*)">
<xsl:value-of select="concat('##',text())"></xsl:value-of>
</xsl:if>
</xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
<xsl:template match="#*" mode="output">
<xsl:param name="previous" as="xs:string"/>
<xsl:variable name="this" select="." as="node()"/>
<xsl:variable name="thisXPath" select="concat($previous, '/#', name(.),'[', count(preceding-sibling::*[name() = name($this)])+1,']','##',.)"></xsl:variable>
<xsl:value-of select="$thisXPath" />
</xsl:template>
</xsl:stylesheet>