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

XSLT – Different context of the FOR extract the value

I need some hits about different context with check of text to extract the correct value.

I already did for inside for but no works proper.

The XML 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

<?xml version="1.0" encoding="UTF-8"?>
<ORDERS05>
    <IDOC BEGIN="1">
        <E1EDP01 SEGMENT="1">
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total item price</KOTXT>
                <KRATE>20.55</KRATE>
            </E1EDP05>
        </E1EDP01>
        <E1EDP01 SEGMENT="1">
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total item price</KOTXT>
                <KRATE>8.03</KRATE>
            </E1EDP05>
        </E1EDP01>
        <E1EDP01 SEGMENT="1">
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total item price</KOTXT>
                <KRATE>99999999</KRATE>
            </E1EDP05>
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total net price</KOTXT>
                <KRATE>9.80</KRATE>
            </E1EDP05>
        </E1EDP01>
        <E1EDP01 SEGMENT="1">
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total item price</KOTXT>
                <KRATE>100000</KRATE>
            </E1EDP05> 
        </E1EDP01>
        <E1EDP01 SEGMENT="1"> 
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total item price</KOTXT>
                <KRATE>10.32</KRATE>
            </E1EDP05>
            <E1EDP05 SEGMENT="1">
                <ALCKZ>-</ALCKZ>
                <KOTXT>Discount</KOTXT>
            </E1EDP05>
            <E1EDP05 SEGMENT="1">
                <KOTXT>Total net price</KOTXT>
                <KRATE>9.80</KRATE>
            </E1EDP05>      
        </E1EDP01>
    </IDOC>
</ORDERS05>

Basically the rules is, in the children node E1EDP05 if the tag KOTXT contains ‘Total item price’ extract the value from KRATE.

XSLT:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:strip-space elements="*"/>
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
   <xsl:template match="/">
      <request>
         <import>
            <fields>
               <xsl:for-each select="//E1EDP01">
                  <Line>
                    <UnitPriceGross fid="7053">
                         <xsl:value-of select="//E1EDP05[KOTXT='Total item price']/KRATE"/>
                     </UnitPriceGross>
                  </Line>
               </xsl:for-each>
            </fields>
         </import>
      </request>
   </xsl:template>
</xsl:stylesheet>

Current result:

<?xml version="1.0" encoding="UTF-8"?>
<request>
   <import>
      <fields>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
      </fields>
   </import>
</request>

The correct result should be with accordioning values not only for the first KRATE as (20.55)

Correct Result:

<?xml version="1.0" encoding="UTF-8"?>
<request>
   <import>
      <fields>
         <Line>
            <UnitPriceGross fid="7053">20.55</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">8.03</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">99999999</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">100000</UnitPriceGross>
         </Line>
         <Line>
            <UnitPriceGross fid="7053">10.32</UnitPriceGross>
         </Line>
      </fields>
   </import>
</request>

Thank you for any idea.

Kind regards,

>Solution :

You are using an incorrect XPath selector insinde xsl:value-of: there you want to match E1EDP05 elements descendant of the currently iterated E1EDP01 context node, but the // operator used like that matches every E1EDP05 descendant of the document root.

You should change it by explicitly adding the context node to make it relative, like this:

.//E1EDP05[KOTXT='Total item price']/KRATE

See the complete result: http://xsltransform.net/bEzknsQ

See also:
https://www.w3.org/TR/xpath-10/#path-abbrev:~:text=//para%20selects%20all%20the%20para%20descendants%20of%20the%20document%20root%20and%20thus%20selects%20all%20para%20elements%20in%20the%20same%20document%20as%20the%20context%20node
https://www.w3.org/TR/xpath-10/#path-abbrev:~:text=.//para%20selects%20the%20para%20element%20descendants%20of%20the%20context%20node

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