Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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

>Solution :

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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading