Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XSLT
|
XSLT General questions and answers about XSLT. For issues strictly specific to the book XSLT 1.1 Programmers Reference, please post to that forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XSLT section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 7th, 2016, 08:52 AM
Registered User
 
Join Date: Oct 2015
Posts: 2
Thanks: 0
Thanked 0 Times in 0 Posts
Question XSLT Code

Hi,

I am BizTalk developer and working on mapping part and I have below issue,

The input file is :

<IPBJs xmlns="DAF.ITD.MAID.IPBJ.V1.0">
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1220</KLSKOD>
<KLSKODSUB>00</KLSKODSUB>
<TALKODISO>CS</TALKODISO>
<KLSVTLNAM>Z�kl.software VECU</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1220</KLSKOD>
<KLSKODSUB>01</KLSKODSUB>
<TALKODISO>DE</TALKODISO>
<KLSVTLNAM>VECU Basissoftware</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1220</KLSKOD>
<KLSKODSUB>00</KLSKODSUB>
<TALKODISO>XE</TALKODISO>
<KLSVTLNAM>VECU Basic Software</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1220</KLSKOD>
<KLSKODSUB>00</KLSKODSUB>
<TALKODISO>XF</TALKODISO>
<KLSVTLNAM>Logiciel base VECU</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1221</KLSKOD>
<KLSKODSUB>00</KLSKODSUB>
<TALKODISO>XP</TALKODISO>
<KLSVTLNAM>Software basico VECU</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
<IPBJ>
<TRAKOD>IPBJ</TRAKOD>
<TRAYMD>20151222</TRAYMD>
<TRATYD>09181689</TRATYD>
<MODIDE>N</MODIDE>
<KLSKOD>1221</KLSKOD>
<KLSKODSUB>00</KLSKODSUB>
<TALKODISO>XS</TALKODISO>
<KLSVTLNAM>Software VECU basico</KLSVTLNAM>
<NAMIND>A</NAMIND>
</IPBJ>
</IPBJs>



I have written below XSLT code to do the transformation.
This code will create ProductMainGroup element for only unique KLSKOD values.



<?xml version="1.0" encoding="UTF-16"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:msxsl="urn:schemas-microsoft-com:xslt" xmlns:var="http://schemas.microsoft.com/BizTalk/2003/var" exclude-result-prefixes="msxsl var s0 userCSharp" version="1.0" xmlns:s0="DAF.ITD.MAID.IPBJ.V1.0" xmlns:common="PACCAR.Common" xmlns:ns0="DAF.AfterSales" xmlns:userCSharp="http://schemas.microsoft.com/BizTalk/2003/userCSharp">
<xsl:output omit-xml-declaration="yes" method="xml" version="1.0" />

<xsl:key name="KLSKOD" match="/s0:IPBJs/s0:IPBJ" use="s0:KLSKOD" />

<xsl:template match="/s0:IPBJs">
<ns0:ProductGroupsChanges>
<xsl:apply-templates select="s0:IPBJ[generate-id() = generate-id(key('KLSKOD', s0:KLSKOD)[1])]"/>
</ns0:ProductGroupsChanges>
</xsl:template>

<xsl:template match="s0:IPBJ">
<xsl:call-template name="KLSKODType"/>
</xsl:template>


<xsl:template name="KLSKODType">

<xsl:variable name="var:v1" select="userCSharp:LogicalEq(string(s0:MODIDE/text()) , &quot;N&quot;)" />
<xsl:variable name="var:v3" select="string(s0:MODIDE/text())" />
<xsl:variable name="var:v4" select="userCSharp:LogicalEq($var:v3 , &quot;D&quot;)" />
<xsl:variable name="var:v6" select="userCSharp:StringSubstring(string(s0:TRAYM D/text()) , &quot;1&quot; , &quot;4&quot;)" />
<xsl:variable name="var:v7" select="userCSharp:StringConcat(&quot;-&quot;)" />
<xsl:variable name="var:v8" select="string(s0:TRAYMD/text())" />
<xsl:variable name="var:v9" select="userCSharp:StringSubstring($var:v8 , &quot;5&quot; , &quot;6&quot;)" />
<xsl:variable name="var:v10" select="userCSharp:StringSubstring($var:v8 , &quot;7&quot; , &quot;8&quot;)" />
<xsl:variable name="var:v11" select="userCSharp:StringConcat(&quot;T&quot;)" />
<xsl:variable name="var:v12" select="userCSharp:StringSubstring(string(s0:TRATY D/text()) , &quot;1&quot; , &quot;2&quot;)" />
<xsl:variable name="var:v13" select="userCSharp:StringConcat(&quot;:&quot;)" />
<xsl:variable name="var:v14" select="string(s0:TRATYD/text())" />
<xsl:variable name="var:v15" select="userCSharp:StringSubstring($var:v14 , &quot;3&quot; , &quot;4&quot;)" />
<xsl:variable name="var:v16" select="userCSharp:StringSubstring($var:v14 , &quot;5&quot; , &quot;6&quot;)" />
<xsl:variable name="var:v17" select="userCSharp:StringConcat(string($var:v6) , string($var:v7) , string($var:v9) , string($var:v7) , string($var:v10) , string($var:v11) , string($var:v12) , string($var:v13) , string($var:v15) , string($var:v13) , string($var:v16))" />
<ns0:ProductMainGroup>
<xsl:attribute name="ID">
<xsl:value-of select="s0:KLSKOD/text()" />
</xsl:attribute>
<xsl:for-each select="(key('KLSKOD', s0:KLSKOD))">
<ns0:ProductSubGroup>
<xsl:attribute name="ID">
<xsl:value-of select="s0:KLSKODSUB/text()" />
</xsl:attribute>
<ns0:Description>
<xsl:attribute name="language">
<xsl:value-of select="s0:TALKODISO/text()" />
</xsl:attribute>
<xsl:attribute name="type">
<xsl:value-of select="s0:NAMIND/text()" />
</xsl:attribute>
<xsl:value-of select="s0:KLSVTLNAM/text()" />
</ns0:Description>
</ns0:ProductSubGroup>
</xsl:for-each>
<xsl:if test="string($var:v1)='true'">
<xsl:variable name="var:v2" select="userCSharp:StringConcat(&quot;Create&quot; )" />
<ns0:ChangeType>
<xsl:value-of select="$var:v2" />
</ns0:ChangeType>
</xsl:if>
<xsl:if test="string($var:v4)='true'">
<xsl:variable name="var:v5" select="userCSharp:StringConcat(&quot;Delete&quot; )" />
<ns0:ChangeType>
<xsl:value-of select="$var:v5" />
</ns0:ChangeType>
</xsl:if>
<ns0:ChangeDateTime>
<xsl:value-of select="$var:v17" />
</ns0:ChangeDateTime>
</ns0:ProductMainGroup>
</xsl:template>
<msxsl:script language="C#" implements-prefix="userCSharp"><![CDATA[
public string StringSubstring(string str, string left, string right)
{
string retval = "";
double dleft = 0;
double dright = 0;
if (str != null && IsNumeric(left, ref dleft) && IsNumeric(right, ref dright))
{
int lt = (int)dleft;
int rt = (int)dright;
lt--; rt--;
if (lt >= 0 && rt >= lt && lt < str.Length)
{
if (rt < str.Length)
{
retval = str.Substring(lt, rt-lt+1);
}
else
{
retval = str.Substring(lt, str.Length-lt);
}
}
}
return retval;
}


public string StringConcat(string param0)
{
return param0;
}


public string StringConcat(string param0, string param1, string param2, string param3, string param4, string param5, string param6, string param7, string param8, string param9, string param10)
{
return param0 + param1 + param2 + param3 + param4 + param5 + param6 + param7 + param8 + param9 + param10;
}


public bool LogicalEq(string val1, string val2)
{
bool ret = false;
double d1 = 0;
double d2 = 0;
if (IsNumeric(val1, ref d1) && IsNumeric(val2, ref d2))
{
ret = d1 == d2;
}
else
{
ret = String.Compare(val1, val2, StringComparison.Ordinal) == 0;
}
return ret;
}


public bool IsNumeric(string val)
{
if (val == null)
{
return false;
}
double d = 0;
return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}

public bool IsNumeric(string val, ref double d)
{
if (val == null)
{
return false;
}
return Double.TryParse(val, System.Globalization.NumberStyles.AllowThousands | System.Globalization.NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out d);
}


]]></msxsl:script>
</xsl:stylesheet>



And output is :

<ns0:ProductGroupsChanges xmlns:ns0="DAF.AfterSales" xmlns:common="PACCAR.Common">
<ns0:ProductMainGroup ID="1220">
<ns0:ProductSubGroup ID="00">
<ns0:Description language="CS" type="A">Z�kl.software VECU</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ProductSubGroup ID="01">
<ns0:Description language="DE" type="A">VECU Basissoftware</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ProductSubGroup ID="00">
<ns0:Description language="XE" type="A">VECU Basic Software</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ProductSubGroup ID="00">
<ns0:Description language="XF" type="A">Logiciel base VECU</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ChangeType>Create</ns0:ChangeType>
<ns0:ChangeDateTime>2015-12-22T09:18:16</ns0:ChangeDateTime>
</ns0:ProductMainGroup>
<ns0:ProductMainGroup ID="1221">
<ns0:ProductSubGroup ID="00">
<ns0:Description language="XP" type="A">Software basico VECU</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ProductSubGroup ID="00">
<ns0:Description language="XS" type="A">Software VECU basico</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ChangeType>Create</ns0:ChangeType>
<ns0:ChangeDateTime>2015-12-22T09:18:16</ns0:ChangeDateTime>
</ns0:ProductMainGroup>
</ns0:ProductGroupsChanges>

But now I am looking for below output where ProductSubGroup element only be created for unique KLSKODSUB values.

Expected output is :


<ns0:ProductGroupsChanges xmlns:ns0="DAF.AfterSales" xmlns:common="PACCAR.Common">
<ns0:ProductMainGroup ID="1220">
<ns0:ProductSubGroup ID="00">
<ns0:Description language="CS" type="A">Z�kl.software VECU</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ProductSubGroup ID="01">
<ns0:Description language="DE" type="A">VECU Basissoftware</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ChangeType>Create</ns0:ChangeType>
<ns0:ChangeDateTime>2015-12-22T09:18:16</ns0:ChangeDateTime>
</ns0:ProductMainGroup>
<ns0:ProductMainGroup ID="1221">
<ns0:ProductSubGroup ID="00">
<ns0:Description language="XP" type="A">Software basico VECU</ns0:Description>
</ns0:ProductSubGroup>
<ns0:ChangeType>Create</ns0:ChangeType>
<ns0:ChangeDateTime>2015-12-22T09:18:16</ns0:ChangeDateTime>
</ns0:ProductMainGroup>
</ns0:ProductGroupsChanges>

I tried it using composite key(KLSKOD and KLSKODSUB) but not getting correct result.

Please let me know if you have any suggestions.





Similar Threads
Thread Thread Starter Forum Replies Last Post
XSLT code RD@xslt XSLT 1 October 21st, 2015 09:48 AM
NEED XSLT CODE FOR XML suji XSLT 2 February 7th, 2008 11:54 PM
xslt code for xml suji XSLT 2 February 5th, 2008 12:18 AM
XSLT code generator for C# asap XSLT 0 April 14th, 2006 03:54 PM
ascii code in my XSLT bluetorch XSLT 1 October 28th, 2005 01:23 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.