XSLT1.0 Remove repeat nodes - xslt-1.0

I am trying to apply Muenchian grouping to eliminate duplicate nodes, but I seem not to be able to make it work.
Input:
<GetUsersByRoleRspMsg>
<UserList>
<User>
<UserId>PPAUSER1</UserId>
<Email>xyz#gmail.com</Email>
</User>
<User>
<UserId>PPAUSER1</UserId>
<Email>xyz#gmail.com</Email>
</User>
</UserList>
</GetUsersByRoleRspMsg>
Required:
<GetUsersByRoleRspMsg>
<UserList>
<User>
<UserId>PPAUSER1</UserId>
<Email>xyz#gmail.com</Email>
</User>
</UserList>
</GetUsersByRoleRspMsg>
The critical part of my code is
<xsl:key name="userIdEmailPair"
match="User"
use="concat(UserId,' ',Email)"/>
<xsl:copy>
<xsl:for-each select="User[
count(. | key('userIdEmailPair',
concat(UserId,' ',Email))[1])
= 1]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>

I don't see any problem with your grouping code. If you try:
<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="*"/>
<xsl:key name="userIdEmailPair" match="User" use="concat(UserId,' ',Email)"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="UserList">
<xsl:copy>
<xsl:for-each select="User[count(. | key('userIdEmailPair',concat(UserId,' ',Email))[1]) = 1]">
<xsl:copy-of select="."/>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
you will get the result you're looking for.
Of course, you could shorten the last template to:
<xsl:template match="UserList">
<xsl:copy>
<xsl:copy-of select="User[count(. | key('userIdEmailPair',concat(UserId,' ',Email))[1]) = 1]"/>
</xsl:copy>
</xsl:template>

Related

How to do for-each-group group-adjacent in XSLT 1.0

Can someone help me convert the below XSLT 2.0 code to XSLT 1.0? Thanks.
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="*[E1EDL24]">
<xsl:copy>
<xsl:apply-templates select="#*"/>
<xsl:for-each-group select="node()" group-adjacent="boolean(self::E1EDL24)">
<xsl:choose>
<xsl:when test="current-grouping-key()">
<xsl:apply-templates select="current-group()">
<xsl:sort select="MATNR" order="descending"/>
</xsl:apply-templates>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
Try whether the following works:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output indent="yes"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="#*|node()" mode="sort">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:key name="adjacent-group" match="E1EDL24[preceding-sibling::*[1][self::E1EDL24]]" use="generate-id(preceding-sibling::E1EDL24[not(preceding-sibling::*[1][self::E1EDL24])][1])"/>
<xsl:template match="E1EDL24[not(preceding-sibling::*[1][self::E1EDL24])]">
<xsl:apply-templates select=". | key('adjacent-group', generate-id())" mode="sort">
<xsl:sort select="MATNR" order="descending"/>
</xsl:apply-templates>
</xsl:template>
<xsl:template match="E1EDL24[preceding-sibling::*[1][self::E1EDL24]]"/>
</xsl:stylesheet>

xsl 1.0 muenchian sum to sum quantity field per compound key

For a SAP PO xsl 1.0 mapping I need to map an 'exotic' file coming from our warehouse where I need to group/sum per order (RECH) a QTY field on a combination of a line identifier (MASTER_PO_LINE_NO) and an Itemnumber (SKU). Each line (RECL) is in a header (RECH).
INPUT:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<MARCXML>
<RCIC>
<RECH>
<SALESID>24000</SALESID>
<RECL>
<LINE_NO>1</LINE_NO>
<QTY_EXPE>1592</QTY_EXPE>
<SKU>11207-210</SKU>
<MASTER_PO_LINE_NO>10</MASTER_PO_LINE_NO>
</RECL>
<RECL>
<LINE_NO>2</LINE_NO>
<QTY_EXPE>1000</QTY_EXPE>
<SKU>11207-210</SKU>
<MASTER_PO_LINE_NO>10</MASTER_PO_LINE_NO>
</RECL>
<RECL>
<LINE_NO>3</LINE_NO>
<QTY_EXPE>175</QTY_EXPE>
<SKU>11207-210</SKU>
<MASTER_PO_LINE_NO>20</MASTER_PO_LINE_NO>
</RECL>
</RECH>
<RECH>
<SALESID>25001</SALESID>
<RECL>
<LINE_NO>1</LINE_NO>
<QTY_EXPE>440</QTY_EXPE>
<SKU>20000-210</SKU>
<MASTER_PO_LINE_NO>10</MASTER_PO_LINE_NO>
</RECL>
</RECH>
</RCIC>
</MARCXML>
</ns0:Message1>
</ns0:Messages>
Required OUTPUT (Qty for 11207-210 = 1592 + 1000):
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<MARCXML>
<RCIC>
<RECH>
<SALESID>24000</SALESID>
<RECL>
<LINE_NO>1</LINE_NO>
<QTY_EXPE>2592</QTY_EXPE>
<SKU>11207-210</SKU>
<MASTER_PO_LINE_NO>10</MASTER_PO_LINE_NO>
</RECL>
<RECL>
<LINE_NO>3</LINE_NO>
<QTY_EXPE>175</QTY_EXPE>
<SKU>11207-210</SKU>
<MASTER_PO_LINE_NO>20</MASTER_PO_LINE_NO>
</RECL>
</RECH>
<RECH>
<SALESID>25001</SALESID>
<RECL>
<LINE_NO>1</LINE_NO>
<QTY_EXPE>440</QTY_EXPE>
<SKU>20000-210</SKU>
<MASTER_PO_LINE_NO>10</MASTER_PO_LINE_NO>
</RECL>
</RECH>
</RCIC>
</MARCXML>
</ns0:Message1>
</ns0:Messages>
There are plenty of examples about muenchian grouping but I didn't manage to solve my requirement yet. To be honest I got as far as:
<?xml version="1.0" encoding="UTF-8"?>
<ns0:Messages xmlns:ns0="http://sap.com/xi/XI/SplitAndMerge">
<ns0:Message1>
<MARCXML>
<RCIC>
<RECH>5284</RECH>
<RECH>5284</RECH>
<RECH>5284</RECH>
</RCIC>
</MARCXML>
</ns0:Message1>
</ns0:Messages>
With the following xsl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kMSTR_PO_LN_SKU" match="RECL"
use="concat(MASTER_PO_LINE_NO, '#', SKU)"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RECH">
<xsl:copy>
<xsl:value-of select="sum(//MARCXML/RCIC/RECH/RECL/QTY_CHECKED_IN)" />
<xsl:for-each select="/RECL[generate-id()=generate-id(key('kMSTR_PO_LN_SKU',concat(MASTER_PO_LINE_NO,'-',SKU))[1])]">
<xsl:copy>
<SKU>
<xsl:value-of select="SKU"/>
</SKU>
<Quantity>
<xsl:value-of select="sum(key('kMSTR_PO_LN_SKU',concat(MASTER_PO_LINE_NO,'-',RECL))/QTY_EXPE)"/>
</Quantity>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Hope someone can help me on my way here
AFAICT, this produces the required output - but having only a single example with somewhat ambiguous rules it could be a coincidence. In any case, it should provide you with a starting point.
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="*"/>
<xsl:key name="k1" match="RECL" use="concat(MASTER_PO_LINE_NO, '|', SKU)"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RECH">
<xsl:copy>
<xsl:copy-of select="SALESID"/>
<xsl:for-each select="RECL[generate-id()=generate-id(key('k1', concat(MASTER_PO_LINE_NO,'|',SKU))[1])]">
<xsl:copy>
<xsl:copy-of select="LINE_NO"/>
<QTY_EXPE>
<xsl:value-of select="sum(key('k1', concat(MASTER_PO_LINE_NO,'|',SKU))/QTY_EXPE)" />
</QTY_EXPE>
<xsl:copy-of select="SKU | MASTER_PO_LINE_NO"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Added:
In order to summarize every order (RECH) separately, you must add an identifier of the order to the key:
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="*"/>
<xsl:key name="k1" match="RECL" use="concat(MASTER_PO_LINE_NO, '|', SKU, '|', generate-id(..))"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="RECH">
<xsl:copy>
<xsl:copy-of select="SALESID"/>
<xsl:for-each select="RECL[generate-id()=generate-id(key('k1', concat(MASTER_PO_LINE_NO,'|',SKU, '|', generate-id(..)))[1])]">
<xsl:copy>
<xsl:copy-of select="LINE_NO"/>
<QTY_EXPE>
<xsl:value-of select="sum(key('k1', concat(MASTER_PO_LINE_NO,'|',SKU, '|', generate-id(..)))/QTY_EXPE)" />
</QTY_EXPE>
<xsl:copy-of select="SKU | MASTER_PO_LINE_NO"/>
</xsl:copy>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Modifying the XML element value upon condiion by XSLT

I have xml
<PhoneNumberDetails>
<PhoneNumber>
<Number>4162880001</Number>
<TimeStamp>2016-08-16T07:07:44-04:00</TimeStamp>
</PhoneNumber>
<PhoneNumber>
<Number>4162880002</Number>
<TimeStamp>2016-08-16T07:07:44-04:00</TimeStamp>
</PhoneNumber>
</PhoneNumberDetails>
This is input to a XSL. In this XSL i retrieve a phone number from context variable stored in one of the previous XSLT and if it matches to any phone number in above xml, I need to update the TimeStamp to new current Timestamp. I need help in this part.
The XSLT Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:dp="http://www.datapower.com/extensions" xmlns:date="http://exslt.org/dates-and-times" extension-element-prefixes="dp date" exclude-result-prefixes="dp">
<xsl:variable name="currentTimeStamp" select="date:date-time()"/>
<xsl:output method="xml"/>
<xsl:template match="PhoneNumberDetails">
<xsl:variable name="savedPhoneNum" select="dp:variable('var://context/name/phonenum')"/>
<xsl:choose>
<xsl:when test="/*[local-name()='PhoneNumberDetails']/*[local-name()='PhoneNumber']/*[local-name()='Number']=$savedPhoneNum">
<xsl:copy-of select="."/>
</xsl:when>
<xsl:otherwise>
<xsl:call-template name="phonenumber"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
<xsl:template name="phonenumber" match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PhoneNumberDetails">
<PhoneNumberDetails>
<xsl:apply-templates select="#* | *"/>
<PhoneNumber>
<Number>
<xsl:value-of select="dp:variable('var://context/name/phonenum')"/>
</Number>
<TimeStamp>
<xsl:value-of select="$currentTimeStamp"/>
</TimeStamp>
</PhoneNumber>
</PhoneNumberDetails>
</xsl:template>
</xsl:stylesheet>
If the phone number does not matches any number in XML i need to add a new element to the above xml and this part works. For e.g.
<PhoneNumber>
<Number>New number</Number>
<TimeStamp>Current TimeStamp</TimeStamp>
</PhoneNumber>
Why can't you do simply:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:dp="http://www.datapower.com/extensions"
xmlns:date="http://exslt.org/dates-and-times"
extension-element-prefixes="dp date">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:param name="savedPhoneNum" select="dp:variable('var://context/name/phonenum')"/>
<!-- identity transform -->
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="TimeStamp[../Number=$savedPhoneNum]">
<xsl:copy>
<xsl:value-of select="date:date-time()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="PhoneNumberDetails[not(PhoneNumber/Number=$savedPhoneNum)]">
<xsl:copy>
<xsl:apply-templates/>
<PhoneNumber>
<Number>New number</Number>
<TimeStamp>Current TimeStamp</TimeStamp>
</PhoneNumber>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
I suggest you start with something like this. Don't use an absolute path to select the phone number, use it from that context node where the template matches.
<xsl:template match="PhoneNumber">
<xsl:copy>
<xsl:copy-of select="Number"/>
<xsl:variable name="savedPhoneNum" select="dp:variable('var://context/name/phonenum')"/>
<TimeStamp>
<xsl:choose>
<xsl:when test="Number=$savedPhoneNum">
<xsl:value-of select="$currentTimeStamp"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="TimeStamp"/>
</xsl:otherwise>
</xsl:choose>
</TimeStamp>
</xsl:copy>
</xsl:template>
Note that I assume that you don't have a (non-shown) namespace on the elements of your input xml. If yes, keep your *[local-name()=""] construct, but then the namespace would have to be added to the match="PhoneNumber" and to the TimeStamp output as well.

Remove Duplicates from a list XSLT 1.0 based on element value

I need help with writing a function in xslt 1.0 that I can pass a list and it'll return the list with duplicates removed. It needs to be a template that I can easily clone or modify for other lists since there are several lists that I want to be able to run this.
Here is one example:
Input list:
<Diagnoses>
<Code>444.4</Code>
<Code>959.99</Code>
<Code>524</Code>
<Code>444.4</Code>
</Diagnoses>
Desired Output, after the duplicate code value 444.4 is removed:
<Diagnoses>
<Code>444.4</Code>
<Code>959.99</Code>
<Code>524</Code>
</Diagnoses>
Here is what I have so far, but it doesn't seem to be working:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:math="http://exslt.org/math"
xmlns:exsl="http://exslt.org/common"
xmlns:data="http://example.com/data" version="1.0"
extension-element-prefixes="math exsl"
exclude-result-prefixes="math exsl data">
<xsl:output omit-xml-declaration="yes" />
<xsl:variable name="DiagnosesList" select="Diagnoses"/>
<Diagnoses>
<xsl:call-template name="DedupeLists">
<xsl:with-param name="Input" select = "exsl:node-set($DiagnosesList)" />
</xsl:call-template>
</Diagnoses>
<xsl:template name="DedupeLists">
<xsl:param name = "Input" />
<xsl:for-each select="$Input/*">
<xsl:if test="Code[not(preceding::Code)]">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
#lingamurthy,
Just
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Code[. = preceding-sibling::Code]"/>
</xsl:stylesheet>
This is one way:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:strip-space elements="*"/>
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="#* | node()">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Code[not(. = preceding-sibling::Code)]">
<xsl:copy>
<xsl:apply-templates select="#* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Code"/>
</xsl:stylesheet>

XSLT- trying to avoid for each looping

I have the source collection as below.
<SourceCollection>
<Row1>
<Col1>Store1</Col1>
<Col2>Med1</Col2>
<Col3>Val1</Col3>
</Row1>
<Row2>
<Col1>Store1</Col1>
<Col2>Med1</Col2>
<Col3>Val2</Col3>
</Row2>
<Row3>
<Col1>Store1</Col1>
<Col2>Med2</Col2>
<Col3>Val2</Col3>
</Row3>
<Row4>
<Col1>Store2</Col1>
<Col2>Med1</Col2>
<Col3>Val4</Col3>
</Row4>
</SourceCollection>
I want the target collection as below with minimal usage of for each.
<TargetCollection>
<Store value=Store1>
<Dim value=Med1>
<DimCode>Val1</DimCode>
<DimCode>Val2</DimCode>
</Dim>
<Dim value=Med2>
<DimCode>Val3</DimCode>
</Dim>
</Store>
<Store value=Store2>
<Dim value=Med1>
<DimCode>Val4</DimCode>
</Dim>
</Store>
</TargetCollection> `
In my initial design I used 3 for-each loops on the same source collection to form the target xml as required. But,the source collection contains millions of rows, which is making my transformation to traverse the whole list (Rows)to the power of 3 times, which is hitting the system performance.
Please let me know any ideas to avoid this.
Generally, using xsl:key can help speed up transforms. Try something like this:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="Col1" match="Col1" use="."/>
<xsl:key name="Col2byCol1" match="Col2" use="concat(../Col1, '-', .)"/>
<xsl:key name="Col3byCol1andCol2"
match="Col3" use="concat(../Col1, '-', ../Col2, '-', .)"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="SourceCollection">
<TargetCollection>
<xsl:apply-templates
select="*/Col1[generate-id() =
generate-id(key(local-name(), .)[1])]"/>
</TargetCollection>
</xsl:template>
<xsl:template match="Col1">
<xsl:variable name="Column1" select="."/>
<Store value="{.}">
<xsl:apply-templates select="../../*/Col2[generate-id() =
generate-id(key('Col2byCol1', concat($Column1, '-' ,.))[1])]"/>
</Store>
</xsl:template>
<xsl:template match="Col2">
<xsl:variable name="Column1" select="../Col1"/>
<xsl:variable name="Column2" select="."/>
<Dim value="{.}">
<xsl:apply-templates
select="../../*[Col2=$Column2]/Col3[generate-id() =
generate-id(key('Col3byCol1andCol2',
concat($Column1, '-', $Column2, '-',.)))]"/>
</Dim>
</xsl:template>
<xsl:template match="Col3">
<DimCode value="{.}">
<xsl:apply-templates />
</DimCode>
</xsl:template>
</xsl:stylesheet>