Home > Blockchain >  How to assign the first occurrence set one of the variable value to for-each all lines
How to assign the first occurrence set one of the variable value to for-each all lines

Time:07-06

i am trying to assign the satisfied lines of first set value to target always with in for-each.

Is there any filter we can add it to target element to hold the value????

My Input XML like below

<process>
<TransactionType>
    <data_xml>
        <transaction>
            
            <sub_documents>
                <transactionLine>
                    <bPALine_l>false</bPALine_l>
                    <part>9W0019468</part>
                </transactionLine>
                <transactionLine>
                    <bPALine_l>true</bPALine_l>
                    <part>9W0019233</part>
                </transactionLine>
                <transactionLine>
                    <bPALine_l>true</bPALine_l>
                    <part>9W0019567</part>
                </transactionLine>
                <transactionLine>
                    <bPALine_l>true</bPALine_l>
                    <part>9W0021928</part>
                </transactionLine>
            </sub_documents>
        </transaction>
    </data_xml>
</TransactionType>

Xpath code defined below

    <?xml version="1.0" encoding="UTF-8" ?>
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
    <xsl:output method="html" doctype-public="XSLT-compat" omit-xml-declaration="yes" encoding="UTF-8" indent="yes" />
    <xsl:template match="/">
<POLInes>
    <xsl:for-each select="/process/TransactionType/data_xml/transaction/sub_documents/transactionLine[bPALine_l='true']">      
      <LINE_KEY>
            <xsl:value-of select="position()"/>
       </LINE_KEY>
               <xsl:choose> 
                <xsl:when test="position() = 1">
       <part_display_number>
            <xsl:value-of select="''"/>
       </part_display_number>
       </xsl:when>
       <xsl:otherwise>
            <part_display_number>
           <xsl:value-of select="part"/>
               </part_display_number>
     </xsl:otherwise>
     </xsl:choose>
</xsl:for-each>
</POLInes>
</xsl:template>
</xsl:transform>

I am excepting the output would be

<!DOCTYPE html
  PUBLIC "XSLT-compat">
<POLInes>
   <LINE_KEY>1</LINE_KEY>
   <part_display_number></part_display_number>
   <LINE_KEY>2</LINE_KEY>
   <part_display_number>9W0019233</part_display_number>
    <LINE_KEY>3</LINE_KEY>
   <part_display_number>9W0019233</part_display_number>
  </POLInes>

but as per the above Xpath it's assgning all the values/

Code click here

CodePudding user response:

The logic required here is somewhat vague. It seems like you want to do:

<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:template match="/process">
    <xsl:variable name="lines" select="TransactionType/data_xml/transaction/sub_documents/transactionLine[bPALine_l='true']"/> 
    <POLInes>
        <xsl:for-each select="$lines">
            <LINE_KEY>
                <xsl:value-of select="position()"/>
            </LINE_KEY>
            <part_display_number>
                <xsl:if test="position() > 1">
                    <xsl:value-of select="$lines[1]/part"/>
                </xsl:if>
            </part_display_number>
        </xsl:for-each>
    </POLInes>
</xsl:template>

</xsl:stylesheet>
  • Related