Setting XSLT variables within for-each select - xslt

Good Day,
I have an XSLT template I'm assembling that look like:
<xsl:for-each select="CarParts/Items">
<div class="columns"><xsl:value-of select="Quantity"/></div>
<div class="columns"><xsl:value-of select="Amount"/></div>
<div class="columns">[SUBTOTAL]</div><br />
</xsl:for-each>
I know that I can define an XSLT variable like this:
<xsl:variable name="totalAmount" select="sum(CarParts/Items/Amount)" />
But I want my XSLT variable to be [SUBTOTAL] which is equal to Quantity * Amount within the for-each select loop. Is this possible? If this was SQL, this would be the equivalent of a computed column.
Any suggestions?
TIA,
coson

What you want to do is cast the value to a number, then you can multiply it as desired:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<results>
<xsl:for-each select="CarParts/Items">
<Item id="{position()}">
<q><xsl:value-of select="Quantity"/></q>
<a><xsl:value-of select="Amount"/></a>
<st><xsl:value-of select="number(Quantity)*number(Amount)"/></st>
</Item>
</xsl:for-each>
</results>
</xsl:template>
</xsl:stylesheet>
I changed the formatting a little since there was no provided input/CSS, but you should see what I was going for. Running it on my sample input of
<CarParts>
<Items>
<Quantity>1</Quantity>
<Amount>100.00</Amount>
</Items>
<Items>
<Quantity>2</Quantity>
<Amount>25.00</Amount>
</Items>
<Items>
<Quantity>3</Quantity>
<Amount>6</Amount>
</Items>
</CarParts>
I get the result of
<?xml version="1.0" encoding="utf-8"?>
<results>
<Item id="1">
<q>1</q>
<a>100.00</a>
<st>100</st>
</Item>
<Item id="2">
<q>2</q>
<a>25.00</a>
<st>50</st>
</Item>
<Item id="3">
<q>3</q>
<a>6</a>
<st>18</st>
</Item>
</results>

Related

Usage of the Variable inside the select-value clause to traverse through the XML path

I am trying to fetch the XML value based on a condition, if the variable value matches the value of the XML path mentioned then to obtain the value of its own sub elements.
The Input XML looks like below
<ns1:productSpecificationFullDTO xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple">
<ns1:product>
<ns1:name>Test Component 1</ns1:name>
<ns1:parent>false</ns1:parent>
</ns1:product>
<ns1:product>
<ns1:name>Test Component 2</ns1:name>
<ns1:parent>false</ns1:parent>
</ns1:product>
<ns1:specification>
<ns1:name>Test Component 1</ns1:name>
<ns1:parent>false</ns1:parent>
<ns1:Labeling>
<ns1:mainProductTitle>Test1</ns1:ns1:mainProductTitle>
</ns1:Labeling>
</ns1:specification>
<ns1:specification>
<ns1:name>Test Component 2</ns1:name>
<ns1:parent>false</ns1:parent>
<ns1:Labeling>
<ns1:mainProductTitle>Test2</ns1:ns1:mainProductTitle>
</ns1:Labeling>
</ns1:specification>
My XSLT Definition is below
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full" xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple" exclude-result-prefixes="ns1 ns1">
<xsl:template match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<xsl:for-each select="/ns1:productSpecificationFullDTO/ns1:product/ns1:parent[text() != 'true']/../ns1:name[text() != 'Parent']/..">
<xsl:variable name="subItem" select="ns1:name/text()"/>
<Item>
<name>
<xsl:value-of select="$subItem"/>
</name>
<LongDescription>
<xsl:value-of select="normalize-space(ns1:productSpecificationFullDTO/ns1:specification/ns1:parent[text() != 'true']/../ns1:name[text() = '''$subItem''']/../ns1:Labeling/ns1:mainProductTitle/text())"/>
</LongDescription>
</Item>
</xsl:for-each>
</Items>
</ItemDetails>
</xsl:template>
The output is as below
<Items>
<Item>
<name>Test Component 1</name>
<LongDescription/>
</Item>
<Item>
<name>Test Component 2</name>
<LongDescription/>
</Item>
Desired Output is
<Items>
<Item>
<name>Test Component 1</name>
<LongDescription>Test1<LongDescription/>
</Item>
<Item>
<name>Test Component 2</name>
<LongDescription>Test2<LongDescription/>
</Item>
As Seen above i'm unable to fetch the value of that variable's sub element.
Please advise, Thanks
I think this solves what you are trying to accomplish, simplifying your XPath expressions and using a key to get to the linked descriptions.
<xsl:stylesheet version="3.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:ns1="http://www.micros.com/creations/core/domain/dto/v1p0/full"
xmlns:ns2="http://www.micros.com/creations/core/domain/dto/v1p0/simple"
exclude-result-prefixes="ns1 ns2">
<xsl:output method="xml" indent="yes"/>
<xsl:key name="keySpec" match="ns1:specification" use="ns1:name"/>
<xsl:template match="/">
<ItemDetails>
<Items>
<!-- Food section start here -->
<xsl:for-each select="/ns1:productSpecificationFullDTO/ns1:product[not(ns1:parent='true') and not(ns1:name='Parent')]">
<Item>
<name>
<xsl:value-of select="ns1:name"/>
</name>
<LongDescription>
<xsl:value-of select="key('keySpec',ns1:name)/ns1:Labeling/ns1:mainProductTitle"/>
</LongDescription>
</Item>
</xsl:for-each>
</Items>
</ItemDetails>
</xsl:template>
</xsl:stylesheet>
See it working here : https://xsltfiddle.liberty-development.net/6qjt5Sw/1

What does the expression $data[not(key('myKey',#myRef))] in XSLT mean?

I read expressions like this
<xsl:variable name="myVar" select="$data[not(key('myKey',#myRef))]"/>
in legacy code. Most likeley it is code from experts ;-). I'm wondering what it does, how it works and how i could reeng it in order to make it more readable. Thank you.
Keys are an important aspect of XSLT. Instead of re-engineering them, it's better to learn the concept.
Keys can be understood as tables with nodes stored under specific keys. They are defined like this:
<xsl:key name="addressByStreet" match="address" use="street"/>
The name attribute is just a QName (similar to a variable name). The match attribute holds an XPath expression that works similarly to the match attribute of <xsl:template>. When the processor finds a node that matches the expression, it evaluates the XPath expression of the use attribute in the context of the matched element. If this expression returns values, they will be used to create new entries in the "key table" for the matched element.
To illustrate that: The above key creates a table with all the <address> elements in the processed document, keyed by the value of their <street> child. This means, if you have these elements:
<address>
<street>Main Street</street>
<number>123</number>
</address>
<address>
<street>Main Street</street>
<number>456</number>
</address>
<address>
<street>Country Road</street>
<street>Country Rd.</street>
<number>789</number>
</address>
… you could then use key('addressByStreet', 'Main Street') to retrieve all the listed addresses in Main Street.
You can use both key('addressByStreet', 'Country Road') and key('addressByStreet', 'Country Rd.') to retrieve the last address.
Why use keys here? The above expression could be re-implemented like //address[street='Main Street'], but now every time this expression is called, the XSLT processor likely goes through the entire document again. That's a problem if a template or loop is called often. Keys can have huge performance benefits (e.g. reduce complexity from O(n²) to O(n)) because the results are "cached".
There are many applications and patterns in which keys are used. For example if you have this XML:
<street-list>
<street>Main Street</street>
<street>Bumpy Road</street>
</street-list>
The expression street-list/street[not(key('addressByStreet', .))] will filter the list of streets and only return streets for which there is no address in the above list – i.e. only "Bumpy Road" in this case because for "Main Street", a key entry exists.
A typical application of keys in XSLT 1 is Muenchian grouping.
I've got the use case now. No, it is not legacy code. This is clear from context and definition of key and data.
If we have data like this:
<xsl:variable name="dict">
<ITEMS>
<ITEM id="1" content="it1">
<ITEM-REF ref="3"/>
</ITEM>
<ITEM id="2" content="it2">
<ITEM-REF ref="1"/>
</ITEM>
<ITEM id="3" content="it3">
<ITEM-REF ref="6"/>
</ITEM>
<ITEM id="4" content="it4">
<ITEM-REF ref="3"/>
</ITEM>
<ITEM id="5" content="it5">
<ITEM-REF ref="5"/>
</ITEM>
<ITEM id="6" content="it6">
<ITEM-REF ref="8"/>
</ITEM>
<ITEM id="7" content="it7">
<ITEM-REF ref="9"/>
</ITEM>
</ITEMS>
</xsl:variable>
And we want to get all ITEM-REF elements with #ref values where there is no ITEM with the same #id value (broken links) the expression can help out:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:msxsl="urn:schemas-microsoft-com:xslt" exclude-result-prefixes="msxsl">
<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:variable name="dict">
<ITEMS>
<ITEM id="1" content="it1">
<ITEM-REF ref="3"/>
</ITEM>
<ITEM id="2" content="it2">
<ITEM-REF ref="1"/>
</ITEM>
<ITEM id="3" content="it3">
<ITEM-REF ref="6"/>
</ITEM>
<ITEM id="4" content="it4">
<ITEM-REF ref="3"/>
</ITEM>
<ITEM id="5" content="it5">
<ITEM-REF ref="5"/>
</ITEM>
<ITEM id="6" content="it6">
<ITEM-REF ref="8"/>
</ITEM>
<ITEM id="7" content="it7">
<ITEM-REF ref="9"/>
</ITEM>
</ITEMS>
</xsl:variable>
<xsl:key name="itemkey" match="ITEM" use="#id"/>
<xsl:template match="START">
<xsl:variable name="allItems" select="msxsl:node-set($dict)//ITEM"/>
<xsl:variable name="allItemRefs" select="msxsl:node-set($dict)//ITEM-REF"/>
<xsl:variable name="itemRefsNotReferencingOtherItems" select="$allItemRefs[not(key('itemkey',#ref))]"/>
<REFERENCED-NOT-EXISTING>
<xsl:for-each select="msxsl:node-set($itemRefsNotReferencingOtherItems)">
<ITEM>
<xsl:attribute name="id">
<xsl:value-of select="#ref"/>
</xsl:attribute>
</ITEM>
</xsl:for-each>
</REFERENCED-NOT-EXISTING>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<REFERENCED-NOT-EXISTING>
<ITEM id="8" />
<ITEM id="9" />
</REFERENCED-NOT-EXISTING>
Input file:
<?xml version="1.0" encoding="utf-8"?>
<START/>

xslt date conversion to UTC

I am very new to xslt programming. Can any one help with this:
input xml:
<?xml version="1.0" encoding="UTF-8"?>
<MESSAGE>
<ER>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<ITEMNUM>I1001</ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
</ER>
<IR>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<ITEMNUM>I1001</ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
</IR>
</MESSAGE>
My XSLT is:
<?xml version="1.0"?>
<!--
This XSL is supposed to change the ITEMNUM tag value by prepending 001 to the existing value.
It also strips off the MESSAGE and IR wrapper tags so that the resultant data is consistent with
our XML schema
-->
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:mro="http://www.mro.com/mx/integration" version="1.0" exclude-result-prefixes="mro">
<xsl:template match="/MESSAGE">
<xsl:apply-templates select="IR"/>
</xsl:template>
<xsl:template match="IR">
<xsl:apply-templates select="#*|*|text()"/>
</xsl:template>
<xsl:template match="#*|*|text()">
<xsl:copy>
<xsl:apply-templates select="#*|*|text()"/>
</xsl:copy>
</xsl:template>
<!--
Change the ITEMNUM tag value by prepending 001 to the existing value
-->
<xsl:template match="mro:ITEMNUM">
<xsl:element name="ITEMNUM" namespace="http://www.mro.com/mx/integration">
<xsl:text>MAX-</xsl:text><xsl:value-of select="."/>
</xsl:element>
</xsl:template>
</xsl:stylesheet>
Current output is:
<?xml version="1.0" encoding="UTF-8"?>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<mro:ITEMNUM xmlns:mro="http://www.mro.com/mx/integration">MAX-I1001</mro:ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T13:00:46+10:00</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
Expected output is:
<?xml version="1.0" encoding="UTF-8"?>
<MXITEMOUT xmlns="http://www.mro.com/mx/integration" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" language="EN">
<Header operation="Notify" event="1">
<SenderID type="MAXIMO" majorversion="6" minorversion="0" build="02" dbbuild="V600-76">MX</SenderID>
<CreationDateTime>2005-08-15T14:28:06-05:00</CreationDateTime>
<RecipientID>EXTSYS1234</RecipientID>
<MessageID>1124</MessageID>
</Header>
<Content>
<MXITEM>
<ITEM action="Add">
<mro:ITEMNUM xmlns:mro="http://www.mro.com/mx/integration">MAX-I1001</mro:ITEMNUM>
<DESCRIPTION langenabled="1">test item</DESCRIPTION>
<ROTATING>1</ROTATING>
<LOTTYPE maxvalue="NOLOT">NOLOT</LOTTYPE>
<CAPITALIZED>0</CAPITALIZED>
<CREATEDDATE>2014-05-22T23:00:46.000Z</CREATEDDATE>
</ITEM>
</MXITEM>
</Content>
</MXITEMOUT>
I am trying find logic in google to convert date to utc format in xslt.
It is throwing strange errors.
XML date:2014-05-22T13:00:46+10:00 Expected Date: 2014-05-22T23:00:46.000Z

Issue while using Preceding axis in XPath and XSLT

I am learning XSLT 2.0 and XPath. While creating the examples I came across to preceding axis available in XPath and created the below example.
Please find the below order.xml file used as input.
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<Order id="12345">
<Item>
<ItemId>007</ItemId>
<ItemName>iPhone 5</ItemName>
<Price>500</Price>
<Quantity>1</Quantity>
</Item>
<Item>
<ItemId>456</ItemId>
<ItemName>Ipad</ItemName>
<Price>600</Price>
<Quantity>2</Quantity>
</Item>
<Item>
<ItemId>7864567</ItemId>
<ItemName>Personal Development Book</ItemName>
<Price>10</Price>
<Quantity>10</Quantity>
</Item>
<Item>
<ItemId>123</ItemId>
<ItemName>Java Book</ItemName>
<Price>20</Price>
<Quantity>12</Quantity>
</Item>
</Order>
Please find the below XSLT testaxis.xsl file used for transformation of the above XML.
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:value-of select="count(/Order/Item[ItemName='Ipad']/ItemName/preceding::*)" />
</xsl:template>
The output after transformation is
6
Here context node is below one if I am not wrong.
<ItemName>Ipad</ItemName>
If we count all the nodes which are before the context node then counts come to 5 .
Now coming to the question, why it is showing the count of the nodes as 6 in the output?
Please do let me know if I misunderstood anything
Thanks in advance.
You are correct about which node is the context node, and that node does have 6 preceding elements:
The first <Item> element.
The four elements inside that.
The <ItemId> element immediately before the context node.
That makes six. You can verify this by doing the following:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:template match="/">
<xsl:for-each select="/Order/Item[ItemName='Ipad']/ItemName/preceding::*">
<xsl:value-of select="concat(name(), '
')" />
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
The preceding elements are the ones marked below with a "*"
<*Item>
<*ItemId>007</ItemId>
<*ItemName>iPhone 5</ItemName>
<*Price>500</Price>
<*Quantity>1</Quantity>
</Item>
<Item>
<*ItemId>456</ItemId>
You will see that there are six of them.

XSLT 1.0: Muenchian grouping does not work

I have the following simplified XML structure:
<?xml version="1.0" encoding="utf-8"?>
<list>
<INVOIC>
<M_INVOIC>
<G_SG25>
<S_LIN>
<id>LIN</id>
<D_1082>1</D_1082>
<C_C212>
<D_7140>7610400271943</D_7140>
<D_7143_3>EN</D_7143_3>
</C_C212>
</S_LIN>
</G_SG25>
<G_SG25>
<S_LIN>
<id>LIN</id>
<D_1082>2</D_1082>
<C_C212>
<D_7140>1234567890123</D_7140>
<D_7143_3>EN</D_7143_3>
</C_C212>
</S_LIN>
</G_SG25>
</M_INVOIC>
</INVOIC>
<INVOIC>
<SALESORDER>
<ET_VBAP>
<item>
<VBELN>0010002695</VBELN>
<POSNR>000010</POSNR>
<MATNR>000000000000400487</MATNR>
<EAN11>1234567890123</EAN11>
</item>
<item>
<VBELN>0010002695</VBELN>
<POSNR>000020</POSNR>
<MATNR>000000000000002054</MATNR>
<EAN11>5012454920549</EAN11>
</item>
<item>
<VBELN>0010002695</VBELN>
<POSNR>000030</POSNR>
<MATNR>000000000000392104</MATNR>
<EAN11>3046920921046</EAN11>
</item>
<item>
<VBELN>0010002695</VBELN>
<POSNR>000040</POSNR>
<MATNR>000000000000859146</MATNR>
<EAN11>8003340591469</EAN11>
</item>
<item>
<VBELN>0010002695</VBELN>
<POSNR>000050</POSNR>
<MATNR>000000000000727194</MATNR>
<EAN11>7610400271943</EAN11>
</item>
</ET_VBAP>
</SALESORDER>
</INVOIC>
</list>
my XSLT:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
version="1.0">
<xsl:output method="xml" encoding="UTF-8" indent="yes"/>
<xsl:key name="kByEanPos" match="G_SG25" use="S_LIN/C_C212/D_7140"/>
<xsl:template match="/">
<xsl:variable name="uniqueSet" select="G_SG25[generate-id()=generate-id(key('kByEanPos',S_LIN/C_C212/D_7140))]"/>
<list>
<xsl:for-each select="list/INVOIC/M_INVOIC/G_SG25[generate-id()=
generate-id(key('kByEanPos',S_LIN/C_C212/D_7140))]">
<ean>
<xsl:value-of select="parent::M_INVOIC/parent::INVOIC/parent::list/INVOIC/SALESORDER/ET_VBAP/item/MATNR"/>
</ean>
</xsl:for-each>
</list>
</xsl:template>
</xsl:stylesheet>
gives me this XML output:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<ean>000000000000400487</ean>
<ean>000000000000400487</ean>
</list>
But my expected XML output is:
<?xml version="1.0" encoding="UTF-8"?>
<list>
<ean>000000000000727194</ean>
<ean>000000000000400487</ean>
</list>
I am not sure of what I am doing wrong, I can't find my mistake. I think it has to do with the key that I defined.
Basically I need a key on <D_7140> and then look for that number in the structure below in EAN11 and output the MATNR right before.
In this line:
<xsl:value-of
select="parent::M_INVOIC/parent::INVOIC/parent::list/INVOIC/SALESORDER/ET_VBAP/item/MATNR"/>
You're climbing all the way up the node tree and back down to MATNR, so the only thing this is ever going to find is the first MATNR in the document. To locate the MATNR that corresponds to the current D_7140 in your for-each, this should work:
<xsl:value-of
select="/list/INVOIC/SALESORDER/ET_VBAP/item[EAN11 = current()/S_LIN/C_C212/D_7140]/MATNR"/>
If you are trying to look up item elements based on their EAN11 value, it might be worth considering using a key to do this too
<xsl:key name="item" match="item" use="EAN11" />
That way, you can reduce your xsl:value-of to just this
<xsl:value-of select="key('item', S_LIN/C_C212/D_7140)/MATNR"/>