I need to start the delem (concept-expected output) after r4 (conbody- expected output), which is not happening

Viewed 49

I need to start the delem (concept-expected output) after r4 (conbody- expected output), currently with respect to r4 mapping, conbody is wrapping the concept (delem).

Basically, I want r4 conbody to end before delem (concept) element.

I am stuck, Need help with the solution.

Input xml

<?xml version="1.0" encoding="UTF-8"?>
<specif>
  <rtit>Test specif</rtit>
 <r3>
  <rtit>Test r3</rtit>
  <r4>
  <rtit>Test r4</rtit>
   <p> para Test r4</p>
   <delem>
   <p>para Test delem</p>
   </delem> 
  <r6>
  <rtit>Test r6</rtit>   
   <p>para Test r6</p>   
  </r6>
 </r4>
 </r3>
</specif>

current output

<?xml version="1.0" encoding="UTF-8"?>
<concept>
  <title>Test specif</title>
  <conbody/>
 <concept>
  <title>Test r3</title>
  <conbody/>
  <concept>
  <title>Test r4</title>
  <conbody>
   <p> para Test r4</p>
   <concept>
    <title></title>
    <conbody> <p> para Test delem</p> </conbody>
   </concept>
   </conbody>
   <concept>
    <title>Test r6</title>   
   <conbody> <p> para Test r6</p> </conbody>  
  </concept>
  </concept>
 </concept>
</concept>

Expected output

<?xml version="1.0" encoding="UTF-8"?>
<concept>
  <title>Test specif</title>
  <conbody/>
 <concept>
  <title>Test r3</title>
  <conbody/>
  <concept>
  <title>Test r4</title>
  <conbody><p> para Test r4</p></conbody>
   <concept>
   <title></title>
   <conbody> <p> para Test delem</p> </conbody>
   </concept>
   <concept>
  <title>Test r6</title>   
   <conbody> <p> para Test r6</p> </conbody>  
  </concept>
  </concept>  
 </concept>
</concept>

XSL

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="2.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform"   
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    
    <xsl:strip-space elements="*"/>  
    
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    
    <xsl:template match="/">
        <xsl:apply-templates select="sb/body/specif"/> 
    </xsl:template>
    
    <xsl:template match="specif">
        <concept>
            <xsl:attribute name="xsi:noNamespaceSchemaLocation" select="'techinfo.xsd'"/>
            <xsl:choose>                
                <xsl:when test="not(@id)">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id ='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id !='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:attribute name="xml:lang">
                <xsl:value-of select="'en'"/>
            </xsl:attribute>
            
            <xsl:apply-templates select="*[matches(local-name(),'r3')]"/>
            
        </concept>           
    </xsl:template>
    
    <xsl:template match="*[matches(local-name(),'r3')]">
        <concept>
            <xsl:choose>                
                <xsl:when test="not(@id)">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id ='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id !='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:attribute name="xml:lang">
                <xsl:value-of select="'en'"/>
            </xsl:attribute>
            <title>
                <xsl:for-each select="child::rtit">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute> 
                    <xsl:apply-templates select="node()|@*|text()"/>
                </xsl:for-each>
            </title>    
            <conbody>
                <xsl:apply-templates select="*[not(starts-with(local-name(),'r'))]"/>                       
            </conbody>
            <!-- apply recursive -->
            <xsl:apply-templates select="*[matches(local-name(),'r4')]"/>        
        </concept>           
    </xsl:template>
    
    <xsl:template match="*[matches(local-name(),'r4')]">
        <concept>
            <xsl:choose>                
                <xsl:when test="not(@id)">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id ='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id !='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:attribute name="xml:lang">
                <xsl:value-of select="'en'"/>
            </xsl:attribute>
            <title>
                <xsl:for-each select="child::rtit">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute> 
                    <xsl:apply-templates select="node()|@*|text()"/>
                </xsl:for-each>
            </title>    
            <conbody>
                <xsl:choose>
                    <xsl:when test="child::r5|r6|r7|r8|r9">
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'r'))]"/>
                    </xsl:when>
                    <xsl:when test="child::delem">
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'delem'))]"/>
                    </xsl:when>
                </xsl:choose>              
            </conbody>
            <!-- apply recursive -->
            <xsl:choose>
                <xsl:when test="child::r5|r6|r7|r8|r9">
                    <xsl:apply-templates select="*[matches(local-name(),'r[5-9]')]"/>                   
                </xsl:when>
                <xsl:when test="child::delem">                  
                    <xsl:apply-templates select="delem"/>                    
                </xsl:when>
            </xsl:choose>          
        </concept>           
    </xsl:template>
    
    <!--tch on all nodes like r1, r2,r4,r5,r6 etc-->
    <xsl:template match="*[matches(local-name(),'r[5-9]')]">
        <concept>
            <xsl:choose>                
                <xsl:when test="not(@id)">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id ='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="'concept'"/>
                        <xsl:value-of select="generate-id(.)"/>
                    </xsl:attribute>
                </xsl:when>
                <xsl:when test="(@id !='')">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute>
                </xsl:when>
            </xsl:choose>
            <xsl:attribute name="xml:lang">
                <xsl:value-of select="'en'"/>
            </xsl:attribute>
            <title>
                <xsl:for-each select="child::rtit">
                    <xsl:attribute name="id">
                        <xsl:value-of select="@id"/>
                    </xsl:attribute> 
                    <xsl:apply-templates select="node()|@*|text()"/>
                </xsl:for-each>
            </title>    
            <conbody>
                <xsl:choose>                    
                    <xsl:when test="*[matches(local-name(),'r[5-9]')]">                
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'r'))]"/>
                    </xsl:when>
                    <xsl:when test="*[matches(local-name(),'delem')]">                 
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'delem'))]"/>                        
                    </xsl:when>
                  <xsl:otherwise>                
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'r'))]"/>
                    </xsl:otherwise>
                </xsl:choose>                
            </conbody>
            <!-- apply recursive -->
            <xsl:choose>
                <xsl:when test="*[matches(local-name(),'r[5-9]')]">
                    <xsl:apply-templates select="*[matches(local-name(),'r[5-9]')]"/>
                   
                </xsl:when>
                <xsl:when test="*[matches(local-name(),'delem')]">                  
                    <xsl:apply-templates select="*[matches(local-name(),'delem')]"/>                    
                </xsl:when>
            </xsl:choose>           
        </concept>           
    </xsl:template>
    
    <xsl:template match="delem">
        <xsl:choose>
            <xsl:when test="*[matches(local-name(),'r[3-9]')]">
                <concept>
                    <xsl:choose>                
                        <xsl:when test="not(@id)">
                            <xsl:attribute name="id">
                                <xsl:value-of select="'concept'"/>
                                <xsl:value-of select="generate-id(.)"/>
                            </xsl:attribute>
                        </xsl:when>
                        <xsl:when test="(@id ='')">
                            <xsl:attribute name="id">
                                <xsl:value-of select="'concept'"/>
                                <xsl:value-of select="generate-id(.)"/>
                            </xsl:attribute>
                        </xsl:when>
                        <xsl:when test="(@id !='')">
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:attribute name="xml:lang">
                        <xsl:value-of select="'en'"/>
                    </xsl:attribute>
                    <title>
                        <xsl:for-each select="child::rtit">
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute> 
                            <xsl:apply-templates select="node()|@*|text()"/>
                        </xsl:for-each>
                    </title>
                    <xsl:for-each select="child::subtitle">
                        <shortdesc>
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute> 
                            <xsl:apply-templates select="node()|@*|text()"/>    
                        </shortdesc>
                    </xsl:for-each>
                    <conbody>
                        <!-- the logic of @id/@group attributes is not clear -->
                        <xsl:apply-templates select="*[not(starts-with(local-name(),'r')) ]"/>
                        
                    </conbody>
                    <!-- apply recursive -->
                    <xsl:apply-templates select="*[matches(local-name(),'r[3-9]')]"/> 
                </concept>              
            </xsl:when>
            
            <xsl:when test="*[not(matches(local-name(),'r[3-9]'))]">
                <concept>
                    <xsl:choose>                
                        <xsl:when test="not(@id)">
                            <xsl:attribute name="id">
                                <xsl:value-of select="'concept'"/>
                                <xsl:value-of select="generate-id(.)"/>
                            </xsl:attribute>
                        </xsl:when>
                        <xsl:when test="(@id ='')">
                            <xsl:attribute name="id">
                                <xsl:value-of select="'concept'"/>
                                <xsl:value-of select="generate-id(.)"/>
                            </xsl:attribute>
                        </xsl:when>
                        <xsl:when test="(@id !='')">
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute>
                        </xsl:when>
                    </xsl:choose>
                    <xsl:attribute name="xml:lang">
                        <xsl:value-of select="'en'"/>
                    </xsl:attribute>
                    
                    <title>
                        <xsl:for-each select="child::rtit">
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute> 
                            <xsl:apply-templates select="node()|@*|text()"/>
                        </xsl:for-each>
                    </title>
                    <xsl:for-each select="child::subtitle">
                        <shortdesc>
                            <xsl:attribute name="id">
                                <xsl:value-of select="@id"/>
                            </xsl:attribute> 
                            <xsl:apply-templates select="node()|@*|text()"/>    
                        </shortdesc>
                    </xsl:for-each>                    
                    <conbody>                                                         
                      <xsl:apply-templates/>                       
                    </conbody>                  
                </concept>                
            </xsl:when>   
        </xsl:choose>
    </xsl:template>  
    <xsl:template match="rtit"/>   
</xsl:stylesheet>
0 Answers
Related