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

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>

Related

How to pass a collection of nodes as a param of an XSL template?

This is my template (a simplification of a real situation):
<xsl:template name="i">
<xsl:param name="args"/>
<xsl:for-each select="$args/*">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
I call it like this:
<xsl:template match="f">
<xsl:call-template name="i">
<xsl:with-param name="args"/>
<a><xsl:value-of select="./#one"/></a>
<a><xsl:value-of select="./#two"/></a>
</xsl:with-param>
</xsl:call-template>
</xsl:template>
Basically, I create artificial nodes <a/>, which looks ugly to me. Is there a better way?
My example would use functions but I have done both:
<?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"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:function name="mf:f1" as="text()">
<xsl:param name="items" as="item()*"/>
<xsl:value-of select="$items"/>
</xsl:function>
<xsl:output method="xml" indent="yes"/>
<xsl:template match="foo">
<function-example>
<xsl:sequence select="mf:f1((#one, #two))"/>
</function-example>
<template-example1>
<xsl:call-template name="h">
<xsl:with-param name="args" select="#one, #two"/>
</xsl:call-template>
</template-example1>
<template-example2>
<xsl:call-template name="i">
<xsl:with-param name="args" select="#one, #two"/>
</xsl:call-template>
</template-example2>
</xsl:template>
<xsl:template name="h">
<xsl:param name="args"/>
<xsl:value-of select="$args"/>
</xsl:template>
<xsl:template name="i">
<xsl:param name="args"/>
<xsl:for-each select="$args">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
transforms
<foo one="1" two="2"/>
into
<function-example>1 2</function-example>
<template-example1>1 2</template-example1>
<template-example2>12</template-example2>
https://xsltfiddle.liberty-development.net/jyfAiD9
Of course, for the sole xsl:value-of use, if you don't want to have the default space separator between the args values, use xsl:value-of separator="".
Consider the following simple example:
XML
<f one="alpha" two="bravo"/>
template call
<xsl:template match="f">
<xsl:call-template name="i">
<xsl:with-param name="args" select="#one, #two"/>
</xsl:call-template>
</xsl:template>
template execution
<xsl:template name="i">
<xsl:param name="args"/>
<xsl:for-each select="$args">
<xsl:value-of select="."/>
</xsl:for-each>
</xsl:template>
Demo: https://xsltfiddle.liberty-development.net/93PXKr2

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 read 2 comma separated string in single loop - XSLT

I have below two variables which have multiple commas separated string values in different XSLT variable
Variable_01 : 88888,777777
Variable_02 : abc,xyz
Now I am looking for below output
[{"Group":"88888", "Name":"abc"},{"Group":"777777", "Name":"xyz"}]
Could you please help me what is correct XSLT code for the above output.
Here's one way you could look at it:
XSLT 2.0
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="Variable_01">88888,777777</xsl:variable>
<xsl:variable name="Variable_02">abc,xyz</xsl:variable>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:for-each select="tokenize($Variable_01, ',')">
<xsl:variable name="i" select="position()"/>
<xsl:text>{"Group":"</xsl:text>
<xsl:value-of select="." />
<xsl:text>", "Name":"</xsl:text>
<xsl:value-of select="tokenize($Variable_02, ',')[$i]" />
<xsl:text>"}</xsl:text>
<xsl:if test="position()!=last()">
<xsl:text>,</xsl:text>
</xsl:if>
</xsl:for-each>
<xsl:text>]</xsl:text>
</xsl:template>
</xsl:stylesheet>
Result
[{"Group":"88888", "Name":"abc"},{"Group":"777777", "Name":"xyz"}]
Demo: http://xsltransform.hikmatu.com/jyH9rLV
In XSLT 3 with higher-order function for-each-pair and JSON map and array support this would be as easy as
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="json" indent="yes"/>
<xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
<xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>
<xsl:template match="/" name="xsl:initial-template">
<xsl:sequence
select="array {
for-each-pair(
tokenize($Variable_01, ','),
tokenize($Variable_02, ','),
function($a, $b) { map { 'Group' : $a, 'Name' : $b } }
)
}"/>
</xsl:template>
</xsl:stylesheet>
In XSLT 3 without higher-order function support for for-each-pair you could implement your own function along the lines of the defintion:
<?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"
xmlns:math="http://www.w3.org/2005/xpath-functions/math"
xmlns:map="http://www.w3.org/2005/xpath-functions/map"
xmlns:array="http://www.w3.org/2005/xpath-functions/array"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="3.0">
<xsl:output method="json" indent="yes"/>
<xsl:function name="mf:object-for-each-pair" as="map(xs:string, xs:string)*">
<xsl:param name="seq1"/>
<xsl:param name="seq2"/>
<xsl:param name="action"/>
<xsl:if test="exists($seq1) and exists($seq2)">
<xsl:sequence select="map { 'Group' : head($seq1), 'Name' : head($seq2) }"/>
<xsl:sequence select="mf:object-for-each-pair(tail($seq1), tail($seq2))"/>
</xsl:if>
</xsl:function>
<xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
<xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>
<xsl:template match="/" name="xsl:initial-template">
<xsl:sequence select="array { mf:object-for-each-pair(tokenize($Variable_01, ','), tokenize($Variable_02, ',')) }"/>
</xsl:template>
</xsl:stylesheet>
https://xsltfiddle.liberty-development.net/6qVRKxk
Finally in XSLT 2 without JSON array and map support you could use the same approach to create text output instead:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
exclude-result-prefixes="#all"
version="2.0">
<xsl:output method="text"/>
<xsl:function name="mf:object-for-each-pair" as="xs:string*">
<xsl:param name="seq1"/>
<xsl:param name="seq2"/>
<xsl:if test="exists($seq1) and exists($seq2)">
<xsl:sequence select="concat('{ "Group" : "', $seq1[1], '", "Name" : "', $seq2[1], '" }')"/>
<xsl:sequence select="mf:object-for-each-pair(subsequence($seq1, 2), subsequence($seq2, 2))"/>
</xsl:if>
</xsl:function>
<xsl:param name="Variable_01" as="xs:string">88888,777777</xsl:param>
<xsl:param name="Variable_02" as="xs:string">abc,xyz</xsl:param>
<xsl:template match="/">
<xsl:text>[ </xsl:text>
<xsl:value-of select="mf:object-for-each-pair(tokenize($Variable_01, ','), tokenize($Variable_02, ','))" separator=", "/>
<xsl:text> ]</xsl:text>
</xsl:template>
</xsl:stylesheet>
http://xsltransform.hikmatu.com/nc4NzPX
This is somewhat crude. But it gets the job done.
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="Variable_01">88888,777777</xsl:variable>
<xsl:variable name="Variable_02">abc,xyz</xsl:variable>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="$Variable_01"/>
<xsl:with-param name="list2" select="$Variable_02"/>
<xsl:with-param name="delimiter" select="','"/>
</xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template name="tokenizeString">
<!--passed template parameter -->
<xsl:param name="list"/>
<xsl:param name="list2"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list, $delimiter)">
<!-- get everything in front of the first delimiter -->
<xsl:text>{"Group":"</xsl:text>
<xsl:value-of select="substring-before($list,$delimiter)"/>
<xsl:text>", "Name":"</xsl:text>
<xsl:call-template name="tokenizeAnother">
<xsl:with-param name="list2" select="$list2"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
<xsl:text>"},</xsl:text>
<xsl:call-template name="tokenizeString">
<!-- store anything left in another variable -->
<xsl:with-param name="list" select="substring-after($list,$delimiter)"/>
<xsl:with-param name="list2" select="substring-after($list2,$delimiter)"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:text>{"Group":"</xsl:text>
<xsl:value-of select="$list"/>
<xsl:text>", "Name":"</xsl:text>
<xsl:call-template name="tokenizeAnother">
<xsl:with-param name="list2" select="$list2"/>
<xsl:with-param name="delimiter" select="$delimiter"/>
</xsl:call-template>
<xsl:text>"}</xsl:text>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="tokenizeAnother">
<xsl:param name="list2"/>
<xsl:param name="delimiter"/>
<xsl:choose>
<xsl:when test="contains($list2, $delimiter)">
<!-- get everything in front of the first delimiter -->
<xsl:value-of select="substring-before($list2,$delimiter)"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$list2"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
See it in action here.
For completion, here's a purely XSLT 1.0 solution that does not require any extension functions:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="UTF-8"/>
<xsl:variable name="Variable_01">88888,777777</xsl:variable>
<xsl:variable name="Variable_02">abc,xyz</xsl:variable>
<xsl:template match="/">
<xsl:text>[</xsl:text>
<xsl:call-template name="tokenize-to-pairs">
<xsl:with-param name="left-text" select="$Variable_01"/>
<xsl:with-param name="right-text" select="$Variable_02"/>
</xsl:call-template>
<xsl:text>]</xsl:text>
</xsl:template>
<xsl:template name="tokenize-to-pairs">
<xsl:param name="left-text"/>
<xsl:param name="right-text"/>
<xsl:param name="delimiter" select="','"/>
<!-- output -->
<xsl:text>{"Group":"</xsl:text>
<xsl:value-of select="substring-before(concat($left-text, $delimiter), $delimiter)" />
<xsl:text>", "Name":"</xsl:text>
<xsl:value-of select="substring-before(concat($right-text, $delimiter), $delimiter)" />
<xsl:text>"}</xsl:text>
<!-- recursive call -->
<xsl:if test="contains($left-text, $delimiter)">
<xsl:text>,</xsl:text>
<xsl:call-template name="tokenize-to-pairs">
<xsl:with-param name="left-text" select="substring-after($left-text, $delimiter)"/>
<xsl:with-param name="right-text" select="substring-after($right-text, $delimiter)"/>
</xsl:call-template>
</xsl:if>
</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>

XSLT Transform to XML

I have some xml in the following format:
<top>
<topValue Value="1#1#5" />
<topValue Value="2#2#10" />
<topValue Value="1#1#3" />
<topValue Value="2#2#30" />
</top>
and output should look like that:
<boo>
<booEnrty>
<v>5</v>
<v>10</v>
</booEnrty>
<booEnrty>
<v>3</v>
<v>30</v>
</booEnrty>
</boo>
my XSLT to transform
<boo>
<xsl:for-each select="top/topValue">
<xsl:if test="position() mod 2 = 0">
<booEnrty>
<v><xsl:value-of select="substring-after(substring-after(#Value,'#'),'#')"/></v>
</booEnrty>
</xsl:if>
</xsl:for-each>
</boo>
What should the XSLT document look like to do this transform?
Any ideas?
Thanks
Maybe someone got a better approach to this, but the XSLT below works for your case.
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:template match="/">
<xsl:apply-templates/>
</xsl:template>
<xsl:template match="top">
<boo>
<xsl:apply-templates select="topValue[position() mod 2 = 1]"/>
</boo>
</xsl:template>
<xsl:template match="topValue[position() mod 2 = 1]">
<booEntry>
<v>
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="#Value" />
<xsl:with-param name="delimiter" select="'#'" />
</xsl:call-template>
</v>
<xsl:apply-templates select="following-sibling::*[1]"/>
</booEntry>
</xsl:template>
<xsl:template match="topValue">
<v>
<xsl:call-template name="substring-after-last">
<xsl:with-param name="string" select="#Value" />
<xsl:with-param name="delimiter" select="'#'" />
</xsl:call-template>
</v>
</xsl:template>
<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:stylesheet>
How about something short and simple?
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="/top">
<boo>
<xsl:for-each select="topValue[position() mod 2 = 1]">
<booEnrty>
<xsl:for-each select=". | following-sibling::topValue[1]">
<v>
<xsl:value-of select="substring-after(substring-after(#Value,'#'),'#')"/>
</v>
</xsl:for-each>
</booEnrty>
</xsl:for-each>
</boo>
</xsl:template>
</xsl:stylesheet>