on 03-13-2018 5:16 PM
Hi SDNites,
I wanted to fetch value from a node whose value will be fetched only when its sibling value matches with the value of other node. Below is the example,
<Parent>
<AllSalesOrder>
<Sales Order>
<lineitem>10 </lineitem>
<shelflife>9 days</shelflife>
</Sales Order>
<Sales Order>
<lineitem>20 </lineitem>
<shelflife>7 days</shelflife>
</Sales Order>
<AllSalesOrder>
<Payload2>
<matchrecord>
<record1>10</record1>
<record2>random val for 10</record2>
</matchrecord>
<matchrecord>
<record1>20</record1>
<record2>random val for 20</record2>
</matchrecord>
</Payload2>
</Parent>
In the above, I have to match the value of,
matchrecord/record1 with AllSalesOrder/Sales Order/lineitem and get the value of
AllSalesOrder/Sales Order/shelflife
Can you please advise how to achieve this in XSLT.
Regards,
Abhi
Hi Abhishek!
Input:
<?xml version="1.0" encoding="UTF-8"?>
<Parent>
<AllSalesOrder>
<SalesOrder>
<lineitem>10</lineitem>
<shelflife>9 days</shelflife>
</SalesOrder>
<SalesOrder>
<lineitem>20</lineitem>
<shelflife>7 days</shelflife>
</SalesOrder>
</AllSalesOrder>
<Payload2>
<matchrecord>
<record1>10</record1>
<record2>random val for 10</record2>
</matchrecord>
<matchrecord>
<record1>20</record1>
<record2>random val for 20</record2>
</matchrecord>
</Payload2>
</Parent>
XSLT:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:template match="/">
<output>
<xsl:for-each select="Parent/AllSalesOrder/SalesOrder">
<matchrecord>
<record1><xsl:value-of select="lineitem"/></record1>
<record2>
<xsl:variable name="id" select="lineitem"/>
<xsl:value-of select="/Parent/Payload2/matchrecord[record1 = $id]/record2"/></record2>
<record3><xsl:value-of select="shelflife"/></record3>
</matchrecord>
</xsl:for-each>
</output>
</xsl:template>
</xsl:stylesheet>
Output:
<?xml version="1.0" encoding="utf-8"?>
<output>
<matchrecord>
<record1>10</record1>
<record2>random val for 10</record2>
<record3>9 days</record3>
</matchrecord>
<matchrecord>
<record1>20</record1>
<record2>random val for 20</record2>
<record3>7 days</record3>
</matchrecord>
</output>
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Thanks Evgeniy for the response.
Output should look like below,
<output>
<matchrecord>
<record1>10</record1>
<record2>random val for 10</record2>
<record3>9 days</record3>
</matchrecord>
<matchrecord>
<record1>20</record1>
<record2>random val for 20</record2>
<record3>7 days</record3>
</matchrecord>
</output>
In the above, record1 from matchrecord has been matched with Sales Order->lineitem and then populated that record in the output.
Regards,
Abhi
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Abhishek!
Wouldn't you clarify how output structure should look like?
Regards, Evgeniy.
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
93 | |
10 | |
10 | |
9 | |
9 | |
7 | |
6 | |
5 | |
5 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.