Perform an XSLT Logic - xslt

input:
<Move-Afile>
<Afile>
<Item>
<suppliercode>1</suppliercode>
<PackNumber>1234</PackNumber>
<Item85>
<Quantity>12</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>2</suppliercode>
<PackNumber>567</PackNumber>
<Item85>
<Quantity>3</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>1</suppliercode>
<PackNumber>567</PackNumber>
<Item85>
<Quantity>8</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>3</suppliercode>
<PackNumber>126</PackNumber>
<Item85>
<Quantity>11</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>4</suppliercode>
<PackNumber>876</PackNumber>
<Item85>
<Quantity>32</Quantity>
</Item85>
</Item>
</Afile>
</Move-Afile>
xslt:
i need the solution like the xsl should contain for-each structure like below.
If supplier codes are equal then we have to sum the quantity values of those nodes,otherwise directly map the quantity value.
<xsl:for-each select="/Move-Afile/Afile/Item">
<xsl:choose>
<xsl:when test="suppliercode=suppliercode>
<xsl:value-of select=sum(Quantity)"/><!-- sum of quantity where nodes have equal supplier code-->
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Quantity"/><!-- map directly the quantity value-->
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
output:
<A>
<target>
<Item>
<Quantity>20</Quantity>
</Item>
<Item>
<Quantity>3</Quantity>
</Item>
<Item>
<Quantity>20</Quantity>
</Item>
<Item>
<Quantity>11</Quantity>
</Item>
<Item>
<Quantity>32</Quantity>
</Item>
</target>
</A>

You don't really need an xsl:choose here, just a single sum statement to sum all Quantity elements for Item elements with the same suppliercode (which include the current node you are on).
<xsl:value-of select="sum(//Item[suppliercode = current()/suppliercode]/Item85/Quantity)"/>
However, it would be more efficient to make use of a key to look for matching Item elements:
<xsl:key name="Item" match="Item" use="suppliercode" />
Then the sum statement is simplified to this
<xsl:value-of select="sum(key('Item', suppliercode)/Item85/Quantity)"/>
You might also like to consider using the XSLT identity transform in building your output XML, as this would be more flexible. You would probably only need a template matching the Quantity element in this case
Try this XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="Item" match="Item" use="suppliercode" />
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Quantity">
<xsl:copy>
<xsl:value-of select="sum(key('Item', ../../suppliercode)/Item85/Quantity)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When applied to your XML, the following is output
<Move-Afile>
<Afile>
<Item>
<suppliercode>1</suppliercode>
<PackNumber>1234</PackNumber>
<Item85>
<Quantity>20</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>2</suppliercode>
<PackNumber>567</PackNumber>
<Item85>
<Quantity>3</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>1</suppliercode>
<PackNumber>567</PackNumber>
<Item85>
<Quantity>20</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>3</suppliercode>
<PackNumber>126</PackNumber>
<Item85>
<Quantity>11</Quantity>
</Item85>
</Item>
<Item>
<suppliercode>4</suppliercode>
<PackNumber>876</PackNumber>
<Item85>
<Quantity>32</Quantity>
</Item85>
</Item>
</Afile>
</Move-Afile>
If you do want to change or remove other elements, just add separate templates for each case accordingly.

Related

xslt 1.0. I need to select few nodes and exclude others in the following expression

I need to include all 01 recordCode nodes only from E0 sibling but need to ignore the ones under E2 and E3 and i am getting all of 01 followed by E0 using the code below. Not sure how to create the key above that would just result in a node with all 01s that follow only E0.
Here is my current xslt.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<!-- Generate keys for Approver, VAT, and Narrative Arrays -->
<xsl:key name="kApproverDetailArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kVATDetailArray" match="faRecord[recordCode='02']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kNarrativeArray" match="faRecord[recordCode='03']" use="generate-id(preceding-sibling::faRecord[recordCode='E0'][1])" />
<xsl:key name="kItemWorkCodeNarrativeArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E2'][1])" />
<xsl:key name="kOrderRecordApproverArray" match="faRecord[recordCode='01']" use="generate-id(preceding-sibling::faRecord[recordCode='E3'][1])" />
<xsl:template match="InvoiceDisplayRequest">
<MCSResponse>
<InvoiceDisplayResponse>
<xsl:apply-templates select="*/faRecord[recordCode='E0']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E1']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E3']" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="facXML/faRecord[recordCode='E2']" mode="InvoiceDisplayResponse" />
</InvoiceDisplayResponse>
</MCSResponse>
</xsl:template>
<xsl:template match="faRecord[recordCode='E0']" mode="InvoiceDisplayResponse">
<InvoiceDisplayDetail>
<xsl:apply-templates select="item" mode="InvoiceDisplayDetail" />
<xsl:apply-templates select="key('kApproverDetailArray', generate-id())" mode="InvoiceDisplayResponse" />
<xsl:apply-templates select="key('kVATDetailArray', generate-id())" mode="InvoiceDisplayResponse"/>
<xsl:apply-templates select="key('kNarrativeArray', generate-id())" mode="InvoiceDisplayResponse"/>
</InvoiceDisplayDetail>
</xsl:template>
<!-- InvoiceDisplayDetail fields -->
<xsl:template match="item[itemMapCode='1']" mode="InvoiceDisplayDetail">
<LogNumber>
<xsl:value-of select="./itemValue" />
</LogNumber>
</xsl:template>
<xsl:template match="item[itemMapCode='2']" mode="InvoiceDisplayDetail">
<LogAddedDate>
<xsl:value-of select="./itemValue" />
</LogAddedDate>
</xsl:template>
<!-- Approver Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceDisplayResponse">
<ApproverArray>
<xsl:apply-templates select="item" mode="ApproverArray" />
</ApproverArray>
</xsl:template>
<!-- VAT Array fields -->
<xsl:template match="faRecord[recordCode='02']" mode="InvoiceDisplayResponse">
<VATDetailArray>
<xsl:apply-templates select="item" mode="VATDetailArray" />
</VATDetailArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="VATDetailArray">
<VATSequence>
<xsl:value-of select="./itemValue" />
</VATSequence>
</xsl:template>
<!-- Narrative Array fields -->
<xsl:template match="faRecord[recordCode='03']" mode="InvoiceDisplayResponse">
<NarrativeArray>
<xsl:apply-templates select="item" mode="NarrativeArray" />
</NarrativeArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="NarrativeArray">
<Narrative>
<xsl:value-of select="./itemValue" />
</Narrative>
</xsl:template>
<xsl:template match="faRecord[recordCode='E1']" mode="InvoiceDisplayResponse">
<ItemRecord>
<xsl:apply-templates select="item" mode="InvoiceItemRecord" />
</ItemRecord>
</xsl:template>
<xsl:template match="faRecord[recordCode='E2']" mode="InvoiceDisplayResponse">
<ItemWorkCodeRecord>
<xsl:apply-templates select="item" mode="InvoiceItemWorkCodeRecord" />
<xsl:apply-templates select="key('kItemWorkCodeNarrativeArray', generate-id())" mode="InvoiceItemWorkCodeRecord"/>
</ItemWorkCodeRecord>
</xsl:template>
<!-- Invoice Item WorkCode (E2 response) Narrative Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceItemWorkCodeRecord">
<NarrativeArray>
<xsl:apply-templates select="item" mode="ItemWorkCodeNarrativeArray" />
</NarrativeArray>
</xsl:template>
<xsl:template match="item[itemMapCode='1']" mode="ItemWorkCodeNarrativeArray">
<Narrative>
<xsl:value-of select="./itemValue" />
</Narrative>
</xsl:template>
<xsl:template match="faRecord[recordCode='E3']" mode="InvoiceDisplayResponse">
<OrderRecord>
<xsl:apply-templates select="item" mode="OrderArray" />
<xsl:apply-templates select="key('kOrderRecordApproverArray', generate-id())" mode="InvoiceOrderRecord" />
</OrderRecord>
</xsl:template>
<!-- Invoice order record (E3 response) Approver Array fields -->
<xsl:template match="faRecord[recordCode='01']" mode="InvoiceOrderRecord">
<ApproverArray>
<xsl:apply-templates select="item" mode="ApproverArray" />
</ApproverArray>
</xsl:template>
<!-- Ignore any other fields -->
<xsl:template match="*" mode="InvoiceDisplayDetail"/>
</xsl:stylesheet>
Here is my source xml.
<?xml version="1.0" encoding="UTF-8"?>
<InvoiceDisplayRequest>
<facXML>
<faRecord>
<recordCode>E0</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>B000011</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2000</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2000</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E0</recordCode>
<item>
<itemMapCode>41</itemMapCode>
<itemValue>N</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>02</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>S</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>STRD</itemValue>
</item>
<item>
<itemMapCode>4</itemMapCode>
<itemValue>3080</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>0</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>03</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>narrative at invoice level here. Invoice log will only allow me to input 100 characters I believe. E</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E1</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>TFR</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E2</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>02</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>12345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789012345678901234567890</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>E2</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>2</itemMapCode>
<itemValue>2</itemValue>
</item>
<item>
<itemMapCode>3</itemMapCode>
<itemValue>AC</itemValue>
</item>
<item>
<itemMapCode>4</itemMapCode>
<itemValue>Actor</itemValue>
</item>
<item>
<itemMapCode>5</itemMapCode>
<itemValue>5400</itemValue>
</item>
<item>
<itemMapCode>6</itemMapCode>
<itemValue>0</itemValue>
</item>
<item>
<itemMapCode>7</itemMapCode>
<itemValue>1</itemValue>
</item>
<item>
<itemMapCode>8</itemMapCode>
<itemValue>1080</itemValue>
</item>
<item>
<itemMapCode>9</itemMapCode>
<itemValue>0</itemValue>
</item>
</faRecord>
<faRecord>
<recordCode>01</recordCode>
<item>
<itemMapCode>1</itemMapCode>
<itemValue>narrative at gird level here</itemValue>
</item>
</faRecord>
</facXML>
</InvoiceDisplayRequest>
Only guessing. But basically you should change the key as following:
(update with key fix)
<xsl:key name="kApproverDetailArray" match="faRecord[recordCode='01']"
use="generate-id(
preceding-sibling::faRecord[recordCode='E0' or recordCode='E2'][1])" />
With that you can loop over all "faRecord[recordCode='01'] following faRecord[recordCode='E0' ]:
<xsl:template match="facXML">
<xsl:for-each select="faRecord[recordCode='E0' ]">
<xsl:for-each select="key('kApproverDetailArray', generate-id()) ">
<!-- do something -->
</xsl:for-each>
</xsl:for-each>
</xsl:template>

XSLT 1.0: rename elements with same content

It seemed like an easy task but I am totally stuck now. I have the following XML:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<ITEM_CODE>ITEM_CODE</ITEM_CODE>
<ITEM_NAME>ITEM_NAME</ITEM_NAME>
<ITEM_ALTERNATE_NAME>ITEM_ALTERNATE_NAME</ITEM_ALTERNATE_NAME>
<ITEM_CATEGORY_CODE>ITEM_CATEGORY_CODE</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>15031</ITEM_CODE>
<ITEM_NAME>Outer Carton</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150529</ITEM_CODE>
<ITEM_NAME>Outer Carton</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150999</ITEM_CODE>
<ITEM_NAME>Outer Carton</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150988</ITEM_CODE>
<ITEM_NAME>test</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
</Items>
If <ITEM_NAME> elements have duplicate contents those should be renamed with a suffix, e.g. a counter value. I came up with this XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
<xsl:key name="keyItemName" match="Item" use="concat(ITEM_CODE , '|', ITEM_NAME)"/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Items">
<Items>
<xsl:apply-templates select="#*|node()"/>
</Items>
</xsl:template>
<xsl:template match="ITEM_NAME">
<xsl:for-each select="parent::Item[generate-id()=generate-id(key('keyItemName',concat(ITEM_CODE , '|', ITEM_NAME))[1])]">
<xsl:variable name="number">
<xsl:number/>
</xsl:variable>
<ITEM_NAME>
<xsl:value-of select="concat(ITEM_NAME,'-',$number)"/>
</ITEM_NAME>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
It gives me this output:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<ITEM_CODE>ITEM_CODE</ITEM_CODE>
<ITEM_NAME>ITEM_NAME-1</ITEM_NAME>
<ITEM_ALTERNATE_NAME>ITEM_ALTERNATE_NAME</ITEM_ALTERNATE_NAME>
<ITEM_CATEGORY_CODE>ITEM_CATEGORY_CODE</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>15031</ITEM_CODE>
<ITEM_NAME>Outer Carton-2</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150529</ITEM_CODE>
<ITEM_NAME>Outer Carton-3</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150999</ITEM_CODE>
<ITEM_NAME>Outer Carton-4</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150988</ITEM_CODE>
<ITEM_NAME>test-5</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
</Items>
But I expect this output:
<?xml version="1.0" encoding="UTF-8"?>
<Items>
<Item>
<ITEM_CODE>ITEM_CODE</ITEM_CODE>
<ITEM_NAME>ITEM_NAME</ITEM_NAME>
<ITEM_ALTERNATE_NAME>ITEM_ALTERNATE_NAME</ITEM_ALTERNATE_NAME>
<ITEM_CATEGORY_CODE>ITEM_CATEGORY_CODE</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>15031</ITEM_CODE>
<ITEM_NAME>Outer Carton-2</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150529</ITEM_CODE>
<ITEM_NAME>Outer Carton-3</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150999</ITEM_CODE>
<ITEM_NAME>Outer Carton-4</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150988</ITEM_CODE>
<ITEM_NAME>test</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
</Items>
In the last <Item> the ITEM_NAME should not be renamed because it is not called "Outer Carton". Also in the first <Item> element no renaming should be happening.
Using preceding:: or preceding-sibling:: to count prior instances is not very efficient computationally, but I don't see a way around it here. The approach below does have the benefit that it only counts preceding instances when after checking (with a key, which is very quick) that there are other items with the same name:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
<xsl:key name="keyItemName" match="ITEM_NAME" use="."/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Items">
<Items>
<xsl:apply-templates select="#*|node()"/>
</Items>
</xsl:template>
<xsl:template match="ITEM_NAME">
<xsl:copy>
<xsl:value-of select="." />
<xsl:if test="count(key('keyItemName', .)) > 1">
<xsl:value-of select="concat('-', count(preceding::ITEM_NAME[. = current()]) + 2)"/>
</xsl:if>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
When run on your sample input, this produces:
<Items>
<Item>
<ITEM_CODE>ITEM_CODE</ITEM_CODE>
<ITEM_NAME>ITEM_NAME</ITEM_NAME>
<ITEM_ALTERNATE_NAME>ITEM_ALTERNATE_NAME</ITEM_ALTERNATE_NAME>
<ITEM_CATEGORY_CODE>ITEM_CATEGORY_CODE</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>15031</ITEM_CODE>
<ITEM_NAME>Outer Carton-2</ITEM_NAME>
<ITEM_ALTERNATE_NAME />
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150529</ITEM_CODE>
<ITEM_NAME>Outer Carton-3</ITEM_NAME>
<ITEM_ALTERNATE_NAME />
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150999</ITEM_CODE>
<ITEM_NAME>Outer Carton-4</ITEM_NAME>
<ITEM_ALTERNATE_NAME />
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150988</ITEM_CODE>
<ITEM_NAME>test</ITEM_NAME>
<ITEM_ALTERNATE_NAME />
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
</Items>
You current key seems to join ITEM_NAME and ITEM_CODE, but it looks like you only want ITEM_NAME here
<xsl:key name="keyItemName" match="ITEM_NAME" use="."/>
It also looks like you want the numbering for the suffix to be based on the position of the parent item element. One way to achieve this is to have a template to match the item element, and then pass the number as a parameter to the subsequent mathching templates
<xsl:template match="Item">
<Item>
<xsl:apply-templates select="#*|node()">
<xsl:with-param name="number">
<xsl:number/>
</xsl:with-param>
</xsl:apply-templates>
</Item>
</xsl:template>
Then, you need a template to match ITEM_NAME elements for which duplicate occurs. This can be done simply by checking there is at least a second element defined in the group for the key:
<xsl:template match="ITEM_NAME[key('keyItemName', .)[2]]">
<xsl:param name="number"/>
Then, you can just output the element with the suffix.
Here is the full XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output encoding="UTF-8" method="xml" indent="yes"/>
<xsl:key name="keyItemName" match="ITEM_NAME" use="."/>
<xsl:template match="#*|node()">
<xsl:copy>
<xsl:apply-templates select="#*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Item">
<Item>
<xsl:apply-templates select="#*|node()">
<xsl:with-param name="number">
<xsl:number/>
</xsl:with-param>
</xsl:apply-templates>
</Item>
</xsl:template>
<xsl:template match="ITEM_NAME[key('keyItemName', .)[2]]">
<xsl:param name="number"/>
<ITEM_NAME>
<xsl:value-of select="concat(.,'-',$number)"/>
</ITEM_NAME>
</xsl:template>
</xsl:stylesheet>
When applied to your XML, the following is output
<Items>
<Item>
<ITEM_CODE>ITEM_CODE</ITEM_CODE>
<ITEM_NAME>ITEM_NAME</ITEM_NAME>
<ITEM_ALTERNATE_NAME>ITEM_ALTERNATE_NAME</ITEM_ALTERNATE_NAME>
<ITEM_CATEGORY_CODE>ITEM_CATEGORY_CODE</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>15031</ITEM_CODE>
<ITEM_NAME>Outer Carton-2</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150529</ITEM_CODE>
<ITEM_NAME>Outer Carton-3</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150999</ITEM_CODE>
<ITEM_NAME>Outer Carton-4</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
<Item>
<ITEM_CODE>150988</ITEM_CODE>
<ITEM_NAME>test</ITEM_NAME>
<ITEM_ALTERNATE_NAME/>
<ITEM_CATEGORY_CODE>52401</ITEM_CATEGORY_CODE>
</Item>
</Items>

XSLT Grouping elements inside two siblings

I'm trying to wrap my head around this and I think the easiest way to explain it is to just show you, below. I've seen this but it doesn't always apply due to the fact I have standalone items also at the end which would match.
The seemingly tricky part is Whatever3 to Whatever6, and then Whatever7 and Whatever8, then finally the new position of Whatever9 - they need to be grouped and the original sequence maintained. (Ignore my naming, there is no way to use xsl:sort)
I've considered xsl:for-each with xsl:if inside, but issue is you can't guarantee how many "groups" vs "non-group" items there are.
Thanks!
XML
<root>
<item>
<position>1</position>
<label>Whatever1</label>
</item>
<item>
<position>2</position>
<label>Whatever2</label>
</item>
<item>
<position>3</position>
<label>Whatever3</label>
<marker id="unique1">start_group</marker>
</item>
<item>
<position>4</position>
<label>Whatever4</label>
</item>
<item>
<position>5</position>
<label>Whatever5</label>
</item>
<item>
<position>6</position>
<label>Whatever6</label>
<marker>last_in_group</marker>
</item>
<item>
<position>7</position>
<label>Whatever7</label>
<marker id="unique2">start_group</marker>
</item>
<item>
<position>8</position>
<label>Whatever8</label>
<marker>last_in_group</marker>
</item>
<item>
<position>9</position>
<label>Whatever9</label>
</item>
</root>
Result
<structure>
<item>
<position>1</position>
<label>Whatever1</label>
</item>
<item>
<position>2</position>
<label>Whatever2</label>
</item>
<group position="3" id="unique1">
<item>
<position>1</position>
<label>Whatever3</label>
</item>
<item>
<position>2</position>
<label>Whatever4</label>
</item>
<item>
<position>3</position>
<label>Whatever5</label>
</item>
<item>
<position>4</position>
<label>Whatever6</label>
</item>
</group>
<group position="4" id="uniqueid2">
<item>
<position>1</position>
<label>Whatever7</label>
</item>
<item>
<position>2</position>
<label>Whatever8</label>
</item>
</group>
<item>
<position>**5**</position>
<label>Whatever9</label>
</item>
</structure>
======================
Here is what I have so far, the only problem I have (besides it being messy) is Whatever4 and Whatever5 are showing up outside the Group.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:template match="root">
<structure>
<xsl:apply-templates select="item[not(marker)] | item[marker='start_group']"/>
</structure>
</xsl:template>
<xsl:template match="item[marker='start_group']">
<group>
<xsl:variable name="currentPosition" select="number(position/text())"/>
<xsl:variable name="lastGroup" select="count(following-sibling::*[local-name() = 'item' and marker='last_in_group'][1]/preceding-sibling::*) + 1"/>
<xsl:attribute name="position">
<xsl:value-of select="$currentPosition"/>
</xsl:attribute>
<xsl:attribute name="id">
<xsl:value-of select="marker/#id"/>
</xsl:attribute>
<item>
<position><xsl:value-of select="number(position/text()) - $currentPosition + 1"/></position>
<label><xsl:value-of select="label/text()"/></label>
</item>
<!-- position() gets reset in for-loop, so need to adjust with outer position -->
<xsl:for-each select="following-sibling::item[(position() + $currentPosition) <= $lastGroup]">
<item>
<position><xsl:value-of select="number(position/text()) - $currentPosition + 1"/></position>
<label><xsl:value-of select="label/text()"/></label>
</item>
</xsl:for-each>
</group>
</xsl:template>
<xsl:template match="item[not(marker)]">
<item>
<position><xsl:value-of select="position/text()"/></position>
<label><xsl:value-of select="label/text()"/></label>
</item>
</xsl:template>
</xsl:stylesheet>
I. XSLT 1.0 Solution:
This transformation:
<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:key name="kFollowing"
match="item[not(marker[. = 'start_group'])
and
preceding-sibling::*[marker][1]/marker = 'start_group'
]"
use="generate-id(preceding-sibling::*
[marker[. = 'start_group']]
[1])"/>
<xsl:template match="node()|#*">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="item[marker[. = 'start_group']]">
<group position="{1 +count(preceding-sibling::*[. = 'start_group'])}"
id="{marker/#id}">
<xsl:copy-of select=".|key('kFollowing', generate-id())"/>
</group>
</xsl:template>
<xsl:template match=
"item[not(marker[. = 'start_group'])
and
preceding-sibling::*[marker][1]/marker = 'start_group'
]"/>
</xsl:stylesheet>
when applied on the provided XML document:
<root>
<item>
<position>1</position>
<label>Whatever1</label>
</item>
<item>
<position>2</position>
<label>Whatever2</label>
</item>
<item>
<position>3</position>
<label>Whatever3</label>
<marker id="unique1">start_group</marker>
</item>
<item>
<position>4</position>
<label>Whatever4</label>
</item>
<item>
<position>5</position>
<label>Whatever5</label>
</item>
<item>
<position>6</position>
<label>Whatever6</label>
<marker>last_in_group</marker>
</item>
<item>
<position>7</position>
<label>Whatever7</label>
<marker id="unique2">start_group</marker>
</item>
<item>
<position>8</position>
<label>Whatever8</label>
<marker>last_in_group</marker>
</item>
<item>
<position>9</position>
<label>Whatever9</label>
</item>
</root>
produces the wanted, correct result:
<root>
<item>
<position>1</position>
<label>Whatever1</label>
</item>
<item>
<position>2</position>
<label>Whatever2</label>
</item>
<group position="1" id="unique1">
<item>
<position>3</position>
<label>Whatever3</label>
<marker id="unique1">start_group</marker>
</item>
<item>
<position>4</position>
<label>Whatever4</label>
</item>
<item>
<position>5</position>
<label>Whatever5</label>
</item>
<item>
<position>6</position>
<label>Whatever6</label>
<marker>last_in_group</marker>
</item>
</group>
<group position="1" id="unique2">
<item>
<position>7</position>
<label>Whatever7</label>
<marker id="unique2">start_group</marker>
</item>
<item>
<position>8</position>
<label>Whatever8</label>
<marker>last_in_group</marker>
</item>
</group>
<item>
<position>9</position>
<label>Whatever9</label>
</item>
</root>
II. XSLT 2.0 solution:
<xsl:stylesheet version="2.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema" exclude-result-prefixes="xs">
<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="/*">
<root>
<xsl:for-each-group select="item" group-starting-with=
"*[marker eq 'start_group'
or
not(marker)
and
preceding-sibling::*[marker][1]/marker eq 'last_in_group'
]
">
<xsl:choose>
<xsl:when test="current-group()[1]/marker">
<group position=
"{1 +count(current-group()[1]
/preceding-sibling::*
[marker = 'start_group'])}"
id="{marker/#id}">
<xsl:apply-templates select="current-group()"/>
</group>
</xsl:when>
<xsl:otherwise>
<xsl:apply-templates select="current-group()"/>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each-group>
</root>
</xsl:template>
</xsl:stylesheet>
When this XSLT 2.0 transformation is applied on the same XML document (above), the same correct result is produced.

XSLT conditions matching with list stored in external file

I am trying to write a XSLT which extracts items matches the conditions listed in another file.
INPUT FILE (input.xml)
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>DEF</Product>
<Price>20.00</Price>
</Item>
<Item>
<Product>GHI</Product>
<Price>30.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
External List File (Codes.xml)
<ProductCodeList>
<ProductCode>ABC</ProductCode>
<ProductCode>JKL</ProductCode>
</ProductCodeList>
Expected Output (output.xml)
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
Could you please show me which one is not working?
<xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />`
<xsl:template match="/">
<xsl:apply-templates select="/ItemList/Item[Product=$productCodeList]"/>
</xsl:template>
<xsl:template match="/ItemList/Item">
<xsl:copy-of select="."/>
</xsl:template>
This simple (no conditionals, no current()) transformation:
<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:variable name="vProds" select=
"document('file:///c:/temp/delete/ProductList.xml')"/>
<xsl:template match="/">
<ItemList>
<xsl:copy-of select=
"/*/Item
[Product
=
$vProds/*/ProductCode
]
"/>
</ItemList>
</xsl:template>
</xsl:stylesheet>
when applied on the provided XML document:
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>DEF</Product>
<Price>20.00</Price>
</Item>
<Item>
<Product>GHI</Product>
<Price>30.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
and having the provided Productlist.xml stored at c:\temp\delete:
<ProductCodeList>
<ProductCode>ABC</ProductCode>
<ProductCode>JKL</ProductCode>
</ProductCodeList>
produces the wanted, correct result:
<ItemList>
<Item>
<Product>ABC</Product>
<Price>10.00</Price>
</Item>
<Item>
<Product>JKL</Product>
<Price>40.00</Price>
</Item>
</ItemList>
Does that maybe work better?
<xsl:variable name="productCodeList" select="document('Codes.xml')/ProductCodeList/ProductCode" />
<xsl:template match="/">
<xsl:apply-templates select="/ItemList/Item"/>
</xsl:template>
<xsl:template match="/ItemList/Item">
<xsl:if test="$productCodeList[.=current()/Product]">
<xsl:copy-of select="."/>
</xsl:if>
</xsl:template>

extract and move nodes via XSLT

I need to transform the incoming XML so that I can extract all "item" with "categorie" is equal "two" and move these into a separate "records" node with an attribute initialized as type="two".
Here's a sample of the incoming XML.
<datafeed>
<records type="one">
<purchases>
<items>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<exchange/>
<context/>
<pilotage/>
</datafeed>
Here's what I would like:
<datafeed>
<records type="one">
<purchases>
<items>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<records type="two">
<purchases>
<items>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<exchange/>
<context/>
<pilotage/>
</datafeed>
I now have two "records" both initialize with it's predefined type (always one or two). The records that were extracted, were moved, hence deleted from the original record.
Thanks
This transformation:
<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:key name="kitemByCategory" match="item"
use="categorie"/>
<xsl:template match="node()|#*" name="identity">
<xsl:copy>
<xsl:apply-templates select="node()|#*"/>
</xsl:copy>
</xsl:template>
<xsl:template match="records">
<xsl:call-template name="identity"/>
<xsl:variable name="vCat2Items" select=
"key('kitemByCategory', 'two')"/>
<xsl:if test="$vCat2Items">
<records type="two">
<purchases>
<items>
<xsl:copy-of select="$vCat2Items"/>
</items>
</purchases>
</records>
</xsl:if>
</xsl:template>
<xsl:template match="item[categorie = 'two']"/>
</xsl:stylesheet>
when applied on the provided XML document, produces the wanted, correct result:
<datafeed>
<records type="one">
<purchases>
<items>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<records type="two">
<purchases>
<items>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<exchange/>
<context/>
<pilotage/>
</datafeed>
Do note:
The use and overriding of the identity rule.
How items of categorie "two" are excluded from processing by using an empty template matching them.
The use of keys for efficient and convenient locationg of items by categorie.
With this stylesheet:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:key name="itemsBycategorie" match="item" use="categorie"/>
<xsl:template match="#*|node()">
<xsl:param name="items"/>
<xsl:copy>
<xsl:apply-templates select="#*|node()">
<xsl:with-param name="items" select="$items"/>
</xsl:apply-templates>
</xsl:copy>
</xsl:template>
<xsl:template match="records">
<xsl:variable name="me" select="."/>
<xsl:for-each select="*/*/*[count(.|key('itemsBycategorie',categorie)[1])=1]">
<records type="{categorie}">
<xsl:apply-templates select="$me/node()">
<xsl:with-param name="items" select="key('itemsBycategorie',categorie)"/>
</xsl:apply-templates>
</records>
</xsl:for-each>
</xsl:template>
<xsl:template match="items">
<xsl:param name="items"/>
<xsl:copy>
<xsl:apply-templates select="$items"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Result:
<datafeed>
<records type="one">
<purchases>
<items>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>one</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<records type="two">
<purchases>
<items>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
<item>
<categorie>two</categorie>
<intrant>String</intrant>
</item>
</items>
</purchases>
</records>
<exchange></exchange>
<context></context>
<pilotage></pilotage>
</datafeed>
Note: Muenchian Method of grouping. And "poor man's tunnel" params (Dimitre quot).