Transformation in XSLT with Recursive Nested Templates - xslt

I have this Entry Input XML
<BusinessInteractionTypes>
<BusinessInteractionType>
<TypeId>123</TypeId>
<Name>Foo</Name>
<Description>XSLT</Description>
</BusinessInteractionType>
...a lot BusinessInteractionType Elements
</BusinessInteractionTypes>
But a have a lot of transformation (to SOA case), I need Transform this input using XSLT to:
<businessInteractionTypes>
<businessInteractionType>
<BusinessInteractionType> <!-- Just first input node -->
<TypeId/>
</BusinessInteractionType>
</businessInteractionType>
<businessInteractionTypeHas> <!-- Recursive interaction began -->
<businessInteractionType>
<BusinessInteractionType>
<TypeId />
</BusinessInteractionType>
<businessInteractionTypeHas>
... recursive
</businessInteractionType>
</businessInteractionTypeHas>
</businessInteractionTypes>

Srs, I fix the issue... follow
Thanks all
<?xml version="1.0" encoding="UTF-8"?>
<xsl:template match="cus:BusinessInteractionType">
<xsl:call-template name="Master">
<xsl:with-param name="nodeD" select="bus1:BusinessInteractionType"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="Master">
<xsl:param name="nodeD" select="bus1:BusinessInteractionType"/>
<cas:businessInteractionTypes>
<cas:businessInteractionType>
<xsl:if test="bus1:BusinessInteractionType[position() = 1]">
<bus3:BusinessInteractionType>
<bus1:TypeId>
<xsl:value-of select="//bus1:TypeId"/>
</bus1:TypeId>
</bus3:BusinessInteractionType>
</xsl:if>
<xsl:call-template name="MasterNested">
<xsl:with-param name="current_node" select="bus1:BusinessInteractionType[position()>1]"/>
</xsl:call-template>
</cas:businessInteractionType>
</cas:businessInteractionTypes>
</xsl:template>
<xsl:template name="MasterNested">
<xsl:param name="current_node" select="bus1:BusinessInteractionType"/>
<xsl:if test="$current_node">
<cas:businessInteractionTypeHas>
<cas:businessInteractionType>
<bus3:TypeId>
<xsl:value-of select="$current_node//bus1:TypeId"/>
</bus3:TypeId>
<bus3:Name>
<xsl:value-of select="$current_node//bus1:Name"/>
</bus3:Name>
<bus3:Description>
<xsl:value-of select="$current_node//bus1:Description"/>
</bus3:Description>
</cas:businessInteractionType>
<xsl:call-template name="MasterNested">
<xsl:with-param name="current_node" select="$current_node/following-sibling::node()"/>
</xsl:call-template>
</cas:businessInteractionTypeHas>
</xsl:if>
</xsl:template>

Related

XSLT: Concatenate and select the element

XSLT:
Written my XSLT like this but I'm not getting required output as below that means I want to loop through each record and get frontimagefilename and rearimagefilename for each check.
<xsl:template name="ChequeDetailsTemplate">
<xsl:param name ="vItemcurrIndx" />
<xsl:variable name ="x" select ="$allRecs[$vItemcurrIndx]/Content/*" />
<xsl:call-template name="loop">
<xsl:with-param name="i" select="1"/>
<xsl:with-param name="limit" select="$x/NumberOfCheques"/>
<xsl:with-param name="vItemcurrIndx" select="position()"></xsl:with-param>
</xsl:call-template>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i"/>
<xsl:param name="limit"/>
<xsl:param name ="vItemcurrIndx" />
<xsl:variable name ="y" select ="$allRecs[$vItemcurrIndx]/Content/*" />
<xsl:if test="$i <= $limit">
<Check>
<CheckAmount><xsl:value-of select="concat(./CourtesyAmount,$i)"/> </CheckAmount>
<FrontImage><xsl:value-of select="concat(./FrontImageFilename,$i)"/></FrontImage>
<RearImage><xsl:value-of select="concat(./RearImageFilename,$i)"/></RearImage>
</Check>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
Output looking for:
-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318f001.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135318r001.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>-<Check><CheckAmount>300</CheckAmount><Codeline><006474< :########## 12345678<</Codeline><FrontImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320f002.tif</FrontImage><RearImage>C:\Jan292015\archive (1) - Copy\archive\BIN\TempChequeImages\CHQNCR-2VD8NP348TD20150129135320r002.tif</RearImage><CourtesyAmount>300</CourtesyAmount></Check>
But the output which im getting now is:
-<Check><CheckAmount>1</CheckAmount><FrontImage>1</FrontImage><RearImage>1</RearImage></Check>-<Check><CheckAmount>2</CheckAmount><FrontImage>2</FrontImage><RearImage>2</RearImage></Check>
If I am guessing (!) correctly, your input looks something like this:
<ChqDepAppSrvChequeScanComplete>
<CourtesyAmount1>100</CourtesyAmount1>
<FrontImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename1>
<RearImageFilename1 t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename1>
<RefuseReason1 t="String">None</RefuseReason1>
<CourtesyAmount2>200</CourtesyAmount2>
<FrontImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename2>
<RearImageFilename2 t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename2>
<RefuseReason2 t="String">None</RefuseReason2>
<NumberOfCheques t="Int32">2</NumberOfCheques>
</ChqDepAppSrvChequeScanComplete>
Your mistake is in the way you have built this expression:
<xsl:value-of select="concat(./CourtesyAmount,$i)"/>
There is no <CourtesyAmount> node in your input, so you are concatenating nothing and $i, resulting in the current value of $i.
What you really want to do here is get the value from a node whose name is the concatenation of the string "CourtesyAmount" and the current value of $i. Something like:
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="/ChqDepAppSrvChequeScanComplete">
<output>
<xsl:call-template name="loop">
<xsl:with-param name="limit" select="NumberOfCheques"/>
</xsl:call-template>
</output>
</xsl:template>
<xsl:template name="loop">
<xsl:param name="i" select="1"/>
<xsl:param name="limit"/>
<xsl:if test="$i <= $limit">
<Check>
<CheckAmount>
<xsl:value-of select="*[local-name()=concat('CourtesyAmount', $i)]"/>
</CheckAmount>
<FrontImage>
<xsl:value-of select="*[local-name()=concat('FrontImageFilename', $i)]"/>
</FrontImage>
<RearImage>
<xsl:value-of select="*[local-name()=concat('RearImageFilename', $i)]"/>
</RearImage>
</Check>
<xsl:call-template name="loop">
<xsl:with-param name="i" select="$i+1"/>
<xsl:with-param name="limit" select="$limit"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
which when applied to the above input will result in:
<?xml version="1.0" encoding="UTF-8"?>
<output>
<Check>
<CheckAmount>100</CheckAmount>
<FrontImage>R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImage>
<RearImage>R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImage>
</Check>
<Check>
<CheckAmount>200</CheckAmount>
<FrontImage>R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImage>
<RearImage>R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImage>
</Check>
</output>
Note:
The real problem here is the input. It should not be formatted this way. Things would be much easier if the person ahead of you did a proper job and supplied you with a well-structured XML, for example:
<ChqDepAppSrvChequeScanComplete>
<NumberOfCheques t="Int32">2</NumberOfCheques>
<Cheque>
<CourtesyAmount>100</CourtesyAmount>
<FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100f001.bmp</FrontImageFilename>
<RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\100r001.bmp</RearImageFilename>
<RefuseReason t="String">None</RefuseReason>
</Cheque>
<Cheque>
<CourtesyAmount>200</CourtesyAmount>
<FrontImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200f002.bmp</FrontImageFilename>
<RearImageFilename t="String">R:\bin_simulated\Images\TempChequeImages\200r002.bmp</RearImageFilename>
<RefuseReason t="String">None</RefuseReason>
</Cheque>
</ChqDepAppSrvChequeScanComplete>
with none of this numbered nodes nonsense.

How to programmatically iterate in XSLT across multiple files with similar name

A have following xmls:
data_0.xml
data_1.xml
data_3.xml
and so on...
And in xslt file I want to iterate through all files, so I tried for-each function.
<xsl:for-each select="document('data.xml')/*">
How to iterate on all of them? Add mask somehow? This surely won't work:
<xsl:for-each select="document('data_*.xml')/*">
Here is your solution in xslt 1.0:
I have four files in my filesystem:
Doc1.xml:
<p>Doc1</p>
Doc2.xml:
<p>Doc2</p>
Doc3.xml:
<p>Doc3</p>
Doc4.xml:
<p>Doc4</p>
and my xslt to get their output is:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<Root>
<xsl:call-template name="getDocuments"/>
</Root>
</xsl:template>
<xsl:template name="getDocuments">
<xsl:param name="fileStartWith" select="'Doc'"/>
<xsl:param name="endCounter">4</xsl:param>
<xsl:param name="startCounter">1</xsl:param>
<xsl:choose>
<xsl:when test="$endCounter > 0">
<xsl:variable name="fileName"><xsl:value-of select="concat($fileStartWith,$startCounter,'.xml')"/></xsl:variable>
<xsl:for-each select="document($fileName)/*">
<xsl:copy-of select="."/><xsl:text>
</xsl:text>
</xsl:for-each>
<xsl:call-template name="getDocuments">
<xsl:with-param name="startCounter" select="$startCounter + 1"/>
<xsl:with-param name="fileStartWith" select="$fileStartWith"/>
<xsl:with-param name="endCounter" select="$endCounter - 1"/>
</xsl:call-template>
</xsl:when>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
generated output is:
<Root>
<p>Doc1</p>
<p>Doc2</p>
<p>Doc3</p>
<p>Doc4</p>
</Root>
Please make sure that xslt and xml is on the same path otherwise you need to change the content of document function.

Split Attribute using ; as delimiter in XSLT

How to split an elements using ; as delimiter.my requirement is like below.
input:
<Element1>C:KEK39519US; U:085896395195; A:K39519US; B:S2345843</Element1>
output:
<CustItem>KEK39519US</CustItem>
<UNumber>085896395195</UNumber>
<ANumber>K39519US</ANumber>
<BNumber>S2345843</BNumber>
the input is every time not same.some times it comes like C:KEK39519US; U:085896395195; B:S2345843
some time like this C:KEK39519US; A:K39519US; B:S2345843
sometime like this U:085896395195; A:K39519US;
sometime like this C:KEK39519US; U:085896395195; A:K39519US;
To solve this in XSLT 1.0 you may need a named template which recursively calls itself. The template will process of the string before the first semi-colon, and output the element accordingly. It will then recursively call itself with the remaining part of the string after this semi-colon (if there is one)
Here is the full XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:template match="Element1">
<xsl:call-template name="outputElements">
<xsl:with-param name="list" select="." />
</xsl:call-template>
</xsl:template>
<xsl:template name="outputElements">
<xsl:param name="list"/>
<xsl:variable name="first" select="normalize-space(substring-before(concat($list, ';'), ';'))"/>
<xsl:variable name="remaining" select="normalize-space(substring-after($list, ';'))"/>
<xsl:call-template name="createElement">
<xsl:with-param name="element" select="$first" />
</xsl:call-template>
<!-- If there are still elements left in the list, call the template recursively -->
<xsl:if test="$remaining">
<xsl:call-template name="outputElements">
<xsl:with-param name="list" select="$remaining"/>
</xsl:call-template>
</xsl:if>
</xsl:template>
<xsl:template name="createElement">
<xsl:param name="element"/>
<xsl:variable name="elementName">
<xsl:choose>
<xsl:when test="substring-before($element, ':') = 'C'">CustItem</xsl:when>
<xsl:otherwise><xsl:value-of select="concat(substring-before($element, ':'), 'Number')" /></xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$elementName}">
<xsl:value-of select="substring-after($element, ':')" />
</xsl:element>
</xsl:template>
</xsl:stylesheet>
When applied to you XML, the following is output
<CustItem>KEK39519US</CustItem>
<UNumber>085896395195</UNumber>
<ANumber>K39519US</ANumber>
<BNumber>S2345843</BNumber>
Note the use of Attribute Value Templates in specifying the name of each new element.

XSLT : Looping from 1 to 60

What is the best way to loop in XSLT from 1 to 60?
I research in net, there are some templates to do this, is there any other way for example like a built-in function?
In XSLT 2.0,
<xsl:for-each select="1 to 60">...</xsl:for-each>
But I guess that you must be using XSLT 1.0, otherwise you wouldn't be asking.
In XSLT 1.0 you should use recursion: a template that calls itself with a counter that's incremented on each call, and the recursion terminates when the required value is reached.
Alternatively there's a workaround in XSLT 1.0: provided your source document contains at least 60 nodes, you can do
<xsl:for-each select="(//node())[60 >= position()]">...</xsl:for-each>
The problem with simple recursion when processing long sequences is that often the space for the call stack becomes insufficient and the processing ends due to stack overflow. This typically happens with sequence length >= 1000.
A general technique to avoid this (implementable with any XSLT processor, even if it doesn't recognize tail-recursion) is DVC (Divide and Conquer) style recursion.
Here is an example of a transformation that successfully prints the numbers from 1 to 1000000 (1M):
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text"/>
<xsl:template match="/">
<xsl:call-template name="displayNumbers">
<xsl:with-param name="pStart" select="1"/>
<xsl:with-param name="pEnd" select="1000000"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="displayNumbers">
<xsl:param name="pStart"/>
<xsl:param name="pEnd"/>
<xsl:if test="not($pStart > $pEnd)">
<xsl:choose>
<xsl:when test="$pStart = $pEnd">
<xsl:value-of select="$pStart"/>
<xsl:text>
</xsl:text>
</xsl:when>
<xsl:otherwise>
<xsl:variable name="vMid" select=
"floor(($pStart + $pEnd) div 2)"/>
<xsl:call-template name="displayNumbers">
<xsl:with-param name="pStart" select="$pStart"/>
<xsl:with-param name="pEnd" select="$vMid"/>
</xsl:call-template>
<xsl:call-template name="displayNumbers">
<xsl:with-param name="pStart" select="$vMid+1"/>
<xsl:with-param name="pEnd" select="$pEnd"/>
</xsl:call-template>
</xsl:otherwise>
</xsl:choose>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
When applied on any XML document (not used) this transformation produces the wanted result -- all the numbers from 1 to 1000000.
You can use/adapt this transformation for any task that needs to "do something N times".
Very simple check inside the foreach-loop
<xsl:if test="$maxItems > position()">
do something
</xsl:if>
Based on Dimitre Novatchev's answer.
Example:
<xsl:variable name="maxItems" select="10" />
<xsl:variable name="sequence" select="any-sequence"/>
<xsl:for-each select="$sequence">
<!-- Maybe sort first -->
<xsl:sort select="#sort-by" order="descending" />
<!-- where the magic happens -->
<xsl:if test="$maxItems > position()">
do something
</xsl:if>
</xsl:for-each>
The basic example for V1.0 using recursion would it be like this:
<xsl:template match="/">
<Root>
<!-- Main Call to MyTemplate -->
<xsl:call-template name="MyTemplate" />
</Root>
</xsl:template>
<xsl:template name="MyTemplate">
<xsl:param name="index" select="1" />
<xsl:param name="maxValue" select="60" />
<MyCodeHere>
<xsl:value-of select="$index"/>
</MyCodeHere>
<!-- < represents "<" for html entities -->
<xsl:if test="$index < $maxValue">
<xsl:call-template name="MyTemplate">
<xsl:with-param name="index" select="$index + 1" />
<xsl:with-param name="total" select="$maxValue" />
</xsl:call-template>
</xsl:if>
</xsl:template>
XSLT works based on templates and you'll need a template do run that loop.
You'll need to build a template receiving start and end values and, inside it, make a recursive call computing with start + 1. When $start equals $end, you do return your template, without another call.
In practice: http://www.ibm.com/developerworks/xml/library/x-tiploop/index.html

xslt: create new node from mixed content within parent element

I'm trying to write a template that will grab a mixture of text nodes and elements within a parent element and create a new node. I've done a lot of searching and couldn't find what I was looking for...so hopefully I'm not asking to basic a question.
Here is a sample of xml I want to transform:
<?xml version="1.0"?>
<root>
<para>Here is some text that will ask users to enter a <rule-line/> [<emph type="it">date</emph>], and maybe their <rule-line/> [<emph type="it">name</emph>]. The text could come in different [<emph type="it">order</emph>] <rule-line/>, and their could be any number of instances.</para>
</root>
I want to group the bracketed text and the rule into a new element like so:
<entry>[<emph type"it">date</emph>]</entry>
I have a template that can identify the text I want to change, and I can change it, but I don't know how to add the text I want to the result tree and omit the old text.
Here are the relevant templates:
<xsl:template match="para">
<xsl:for-each select="* | text()">
<xsl:choose>
<xsl:when test="self::rule-line and following-sibling::node()[1][starts-with(., ' [')] and string(node-name(following-sibling::node()[2])) = 'emph' and following-sibling::node()[3][starts-with(., ']')]">
<xsl:comment>made match</xsl:comment>
<xsl:call-template name="codeEntry">
<xsl:with-param name="rule" select="."/>
<xsl:with-param name="openBracket" select="following-sibling::node()[1]"/>
<xsl:with-param name="emphTag" select="following-sibling::node()[2]"/>
<xsl:with-param name="closeBracketString" select="following-sibling::node()[3]"/>
</xsl:call-template>
</xsl:when>
<xsl:otherwise>
<xsl:copy-of select="."/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</xsl:template>
<xsl:template name="codeEntry">
<xsl:param name="rule"/>
<xsl:param name="openBracket"/>
<xsl:param name="emphTag"/>
<xsl:param name="closeBracketString"/>
<entry>
<xsl:copy-of select="$openBracket"/>
<xsl:copy-of select="$emphTag"/>
<xsl:text>] </xsl:text>
</entry>
<xsl:value-of select="substring-after($closeBracketString, ']')"/>
</xsl:template>
Obviously, the when statement grabs a group of nodes, but when each node goes through the otherwise block it gets copied to the result tree. I'm not really sure how to handle this since the para could have any number of these node groupings in any order, or none. (Once I figure this out I'll add another when block that deals with the bracketed text before the rule)
I think creating a variable that tells the template to ignore the node is the way to go...but I'm a little foggy on the immutable variables and their scope...
I was also trying to think of a way I could try to do this recursively...but that would require adding a start tag at one point, an end tag in another, or no tag if the node being processed is in the middle of the sequence...and I know that can get weird in xslt.
Anyone run into this type of situation before?
thanks,
jason
any ideas
Just for fun (What a mess of a schema!), this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="rule-line"/>
<xsl:template match="emph">
<entry>
<xsl:text>[</xsl:text>
<xsl:call-template name="identity"/>
<xsl:text>]</xsl:text>
</entry>
</xsl:template>
<xsl:template match="text()[normalize-space()='[']
[following-sibling::*[1][self::emph]] |
text()[normalize-space()=']']
[preceding-sibling::*[1][self::emph]]"
priority="1"/>
<xsl:template match="text()[starts-with(normalize-space(),']')]
[preceding-sibling::*[1][self::emph]]">
<xsl:value-of select="substring-after(.,']')"/>
</xsl:template>
<xsl:template match="text()[substring(normalize-space(),
string-length(normalize-space()),
1) = '[']
[following-sibling::*[1][self::emph]]">
<xsl:call-template name="crop-both">
<xsl:with-param name="pString" select="concat(']',.)"/>
</xsl:call-template>
</xsl:template>
<xsl:template match="text()[starts-with(normalize-space(),']')]
[substring(normalize-space(),
string-length(normalize-space()),
1) = '[']
[preceding-sibling::*[1][self::emph]]
[following-sibling::*[1][self::emph]]"
priority="1" name="crop-both">
<xsl:param name="pString" select="."/>
<xsl:variable name="vReverse">
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring-after(.,']')"/>
</xsl:call-template>
</xsl:variable>
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring-after($vReverse,'[')"/>
</xsl:call-template>
</xsl:template>
<xsl:template name="reverse">
<xsl:param name="pString"/>
<xsl:if test="$pString!=''">
<xsl:call-template name="reverse">
<xsl:with-param name="pString"
select="substring($pString,2)"/>
</xsl:call-template>
<xsl:value-of select="substring($pString,1,1)"/>
</xsl:if>
</xsl:template>
</xsl:stylesheet>
Output:
<root>
<para>Here is some text that will ask users to enter a <entry>[<emph type="it">date</emph>]</entry>, and maybe their <entry>[<emph type="it">name</emph>]</entry>. The text could come in different <entry>[<emph type="it">order</emph>]</entry>, and their could be any number of instances.</para>
</root>