Skip to Content

XSLT Mapping - Fetch details based on value from another node value

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

Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

3 Answers

  • Best Answer
    Mar 15 at 10:25 AM

    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.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 14 at 06:45 AM

    Hi Abhishek!

    Wouldn't you clarify how output structure should look like?

    Regards, Evgeniy.

    Add comment
    10|10000 characters needed characters exceeded

  • Mar 14 at 09:27 AM

    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

    Add comment
    10|10000 characters needed characters exceeded