Krisshan Sirohiya
Krisshan Sirohiya

Reputation: 69

XSLT Comparison between 2 for loop and take values if matching

I am new to XSLT. I am trying to compare 2 for loop for its node value and if only node value is matching. I wanted to take it. For example input XML as below.

<Order>
    <Lines>
        <Line LineNo="1">
            <OEPLIST>
                <OEP Node="A"/>
                <OEP Node="B"/>
            </OEPLIST>
            <INPLIST>
                <INP ExtnNode="Y"/>
                <INP ExtnNode="B"/>
            </INPLIST>
        </Line>
        <Line LineNo="2">
            <OEPLIST>
                <OEP Node="C"/>
                <OEP Node="D"/>
            </OEPLIST>
            <INPLIST>
                <INP ExtnNode="M"/>
                <INP ExtnNode="N"/>
            </INPLIST>
        </Line>
        <Line LineNo="3">
            <OEPLIST>
                <OEP Node="E"/>
                <OEP Node="F"/>
            </OEPLIST>
            <INPLIST>
                <INP ExtnNode="E"/>
                <INP ExtnNode="F"/>
            </INPLIST>
        </Line>
    </Lines>
</Order>

and I want to have output as below.

<Order>
    <Lines>
        <Line LineNo="1" >
            <store Node="B" />
        </Line>
        
        <Line LineNo="2">
        </Line>
        
        
        <Line LineNo="3">
            <store Node="E" />
            <store Node="F" />
        </Line>
    </Lines>
</Order>

So that I am taking only if Nodes are matching in for loop. So can anyone help me here.

Upvotes: 0

Views: 45

Answers (1)

michael.hor257k
michael.hor257k

Reputation: 117018

If I understand correctly, you want to do something like:

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="*"/>

<!-- identity transform -->
<xsl:template match="@*|node()">
    <xsl:copy>
        <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
</xsl:template>

<xsl:template match="Line">
    <Line LineNo="{@LineNo}">
        <xsl:apply-templates select="OEPLIST/OEP[@Node=ancestor::Line/INPLIST/INP/@ExtnNode]"/>
    </Line>
</xsl:template>

<xsl:template match="OEP">
    <store Node="{@Node}" />
</xsl:template>

</xsl:stylesheet>

Upvotes: 1

Related Questions