XSLT to create copy of segment for multiple Items

  Kiến thức lập trình

I am trying to write an XSLT to check if L24/ITEM segment is repeating or not, if its repeating then we need create separate L24 segment with one ITEM segment, remaining segments will be same. which means ITEM is the key to repeat its header node L24. my XSLT is not giving the expected results, can you please assist here.

Input:

<?xml version="1.1"?>
<EXTR001>
    <DOC BEGIN="1">
        <DC4 SEGMENT="1">
            <TAB>DC4</TAB>
            <MAN>010</MAN>          
        </DC4>
        <L20 SEGMENT="1">
            <VBE>424</VBE>      
            <L22 SEGMENT="1">
                <VKORG_BEZ>FIN</VKORG_BEZ>
            </L22>          
            <L24 SEGMENT="1">
                <POSNR>000010</POSNR>
                <MATNR>000909</MATNR>               
                <DP05 SEGMENT="1">
                    <Z1CUSTCODE>340</Z1CUSTCODE>
                    <QUALF>90</QUALF>
                </DP05>
                <Z1CUEAN SEGMENT="1">
                    <Z2CUEAN>871132</Z2CUEAN>               
                </Z1CUEAN>
                <ITEM SEGMENT="1">
                    <POSNR>000010</POSNR>
                    <EAN11>87113273</EAN11> 
                    <SSCC>OCR1</SSCC>                   
                </ITEM>
                <Z1MBXGLN SEGMENT="1">
                    <BBBNR>0000000</BBBNR>
                </Z1MBXGLN>             
                <L26 SEGMENT="1">
                    <PSTYV>Z112</PSTYV>             
                    <L27 SEGMENT="1">
                        <PRODH_BEZ>Cor</PRODH_BEZ>                  
                    </L27>
                </L26>              
            </L24>  
           <L24 SEGMENT="1">
                <POSNR>000020</POSNR>
                <MATNR>18830</MATNR>                
                <DP05 SEGMENT="1">
                    <Z1CUSTCODE>34720</Z1CUSTCODE>
                    <QUALF>901</QUALF>
                </DP05>
                <Z1CUEAN SEGMENT="1">
                    <Z2CUEAN>0909</Z2CUEAN>             
                </Z1CUEAN>
                <ITEM SEGMENT="1">
                    <POSNR>000020</POSNR>
                    <EAN11>8711327347205</EAN11>
                     <SSCC>1234</SSCC>                  
                </ITEM>             
                    <ITEM SEGMENT="1">
                    <POSNR>000020</POSNR>
                    <EAN11>8711327347205</EAN11>    
                     <SSCC>4321</SSCC>                  
                </ITEM>
                <Z1MBXGLN SEGMENT="1">
                    <BBBNR>0000000</BBBNR>
                    <BBSNR>00000</BBSNR>
                </Z1MBXGLN>             
                <L26 SEGMENT="1">
                    <PSTYV>Z112</PSTYV>
                    <MATKL>UNASSIGND</MATKL>
                    <L27 SEGMENT="1">
                        <PRODH_BEZ>C13</PRODH_BEZ>                  
                    </L27>
                </L26>              
            </L24>  
              
        </L20>
    </DOC>
</EXTR001>

** Desired Output:**

<?xml version="1.1"?>
<EXTR001>
    <DOC BEGIN="1">
        <DC4 SEGMENT="1">
            <TAB>DC4</TAB>
            <MAN>010</MAN>          
        </DC4>
        <L20 SEGMENT="1">
            <VBE>424</VBE>      
            <L22 SEGMENT="1">
                <VKORG_BEZ>FIN</VKORG_BEZ>
            </L22>          
            <L24 SEGMENT="1">
                <POSNR>000010</POSNR>
                <MATNR>000909</MATNR>               
                <DP05 SEGMENT="1">
                    <Z1CUSTCODE>340</Z1CUSTCODE>
                    <QUALF>90</QUALF>
                </DP05>
                <Z1CUEAN SEGMENT="1">
                    <Z2CUEAN>871132</Z2CUEAN>               
                </Z1CUEAN>
                <ITEM SEGMENT="1">
                    <POSNR>000010</POSNR>
                    <EAN11>87113273</EAN11> 
                    <SSCC>OCR1</SSCC>                   
                </ITEM>
                <Z1MBXGLN SEGMENT="1">
                    <BBBNR>0000000</BBBNR>
                </Z1MBXGLN>             
                <L26 SEGMENT="1">
                    <PSTYV>Z112</PSTYV>             
                    <L27 SEGMENT="1">
                        <PRODH_BEZ>Cor</PRODH_BEZ>                  
                    </L27>
                </L26>              
            </L24>  
           <L24 SEGMENT="1">
                <POSNR>000020</POSNR>
                <MATNR>18830</MATNR>                
                <DP05 SEGMENT="1">
                    <Z1CUSTCODE>34720</Z1CUSTCODE>
                    <QUALF>901</QUALF>
                </DP05>
                <Z1CUEAN SEGMENT="1">
                    <Z2CUEAN>0909</Z2CUEAN>             
                </Z1CUEAN>
                <ITEM SEGMENT="1">
                    <POSNR>000020</POSNR>
                    <EAN11>8711327347205</EAN11>
                     <SSCC>1234</SSCC>                  
                </ITEM>             
                <Z1MBXGLN SEGMENT="1">
                    <BBBNR>0000000</BBBNR>
                    <BBSNR>00000</BBSNR>
                </Z1MBXGLN>             
                <L26 SEGMENT="1">
                    <PSTYV>Z112</PSTYV>
                    <MATKL>UNASSIGND</MATKL>
                    <L27 SEGMENT="1">
                        <PRODH_BEZ>C13</PRODH_BEZ>                  
                    </L27>
                </L26>              
            </L24>  
              <L24 SEGMENT="1">
                <POSNR>000020</POSNR>
                <MATNR>18830</MATNR>                
                <DP05 SEGMENT="1">
                    <Z1CUSTCODE>34720</Z1CUSTCODE>
                    <QUALF>901</QUALF>
                </DP05>
                <Z1CUEAN SEGMENT="1">
                    <Z2CUEAN>0909</Z2CUEAN>             
                </Z1CUEAN>          
                <ITEM SEGMENT="1">
                    <POSNR>000020</POSNR>
                    <EAN11>8711327347205</EAN11>    
                     <SSCC>4321</SSCC>                  
                </ITEM>
                <Z1MBXGLN SEGMENT="1">
                    <BBBNR>0000000</BBBNR>
                    <BBSNR>00000</BBSNR>
                </Z1MBXGLN>             
                <L26 SEGMENT="1">
                    <PSTYV>Z112</PSTYV>
                    <MATKL>UNASSIGND</MATKL>
                    <L27 SEGMENT="1">
                        <PRODH_BEZ>C13</PRODH_BEZ>                  
                    </L27>
                </L26>              
            </L24>              
        </L20>
    </DOC>
</EXTR001>

** XSLT I used is below:**

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="@*|node()">
        <xsl:copy>
            <xsl:apply-templates select="@*|node()"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="/EXTR001">
        <xsl:copy>
            <xsl:apply-templates select="EXTR001"/>
            <xsl:apply-templates select="ITEM"/>
        </xsl:copy>
    </xsl:template>

    <xsl:template match="ITEM">
        <xsl:if test="count(ITEM) >1">
            <xsl:apply-templates select="ITEM"/>
        </xsl:if>
    </xsl:template>

   
    <xsl:template match="ITEM">
       
        <xsl:variable name="EXTR001" select="/EXTR001"/>
        <xsl:copy-of select="$EXTR001"/>
        <xsl:copy-of select="."/>
    </xsl:template>

</xsl:stylesheet>

2

I would approach it like this, using template modes:

  • Have a specialized template that matches the L24 that have multiple ITEM (you could either test count(ITEM) > 1 or a simple predicate to test if there is ITEM[2]

  • Inside of that template that matches L24 that have multiple ITEM, apply-templates in the mode="split"

  • Have a template matching ITEM in mode=”split that applies templates to it’s parent, L24 in that mode and passes itself as a parameter to the template

  • In the template matching L24 in the mode=”split”, copy the element and apply-templates to all of it’s attributes and the preceding-sibling::node() of the ITEM (excluding any other ITEM), then copy the ITEM, and finally apply-templates to it’s following-sibling::node() (except for any other ITEM)

    <xsl:stylesheet version="1.0"
      xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
      <xsl:template match="@*|node()">
          <xsl:copy>
              <xsl:apply-templates select="@*|node()"/>
          </xsl:copy>
      </xsl:template>
    
      <xsl:template match="L24[ITEM[1]]">
          <xsl:apply-templates select="ITEM" mode="split" />
      </xsl:template>    
    
      <xsl:template match="ITEM" mode="split">
          <xsl:apply-templates select=".." mode="split">
              <xsl:with-param name="item" select="."/>
          </xsl:apply-templates>
      </xsl:template>
    
      <xsl:template match="L24" mode="split">
          <xsl:param name="item"/>
          <xsl:copy>
              <xsl:apply-templates select="@*|$item/preceding-sibling::node()[not(self::ITEM)]" />
              <xsl:copy-of select="$item"/>
              <xsl:apply-templates select="$item/following-sibling::node()[not(self::ITEM)]" />    
          </xsl:copy>
      </xsl:template>
    
    </xsl:stylesheet>
    

0

I would approach it like this:

<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform">

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

  <xsl:template match="L24[ITEM[2]]">
    <xsl:for-each select="ITEM">
      <L24>
        <xsl:copy-of select="
          parent::L24/@* | 
          preceding-sibling::node()[not(self::ITEM)] |
          . |
          following-sibling::node()[not(self::ITEM)]
        "/>
      </L24>
    </xsl:for-each>
  </xsl:template>    

</xsl:stylesheet>

An identity template copies most of the document.

The other template handles any L24 which contains at least 2 ITEM elements (i.e. an L24 which contains an ITEM which is the second ITEM child of its parent).

For each ITEM in the L24, the template makes a new L24 element and populates it with the parent L24‘s attributes, the preceding siblings of the current ITEM (excluding other ITEM elements), the current ITEM, and the current ITEM‘s following siblings.

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT