1

I have an XML file like:

<Documents>
  <Document>
     <firstname>Vaneet</firstname>
     <lastname>chendra</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>html</Doc_Type>
  </Document>
  <Document>
     <firstname>joshna</firstname>
     <lastname>shekar</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>Text</Doc_Type>
  </Document>
</Documents>

and my output XML should be like

<Documents>
  <Document>
     <firstname>Vaneet</firstname>
     <lastname>chendra</lastname> 
     <ID_DOC>101121</ID_DOC>
     <Doc_Type>html</Doc_Type>
     <firstname>joshna</firstname>
     <lastname>shekar</lastname>
     <Doc_Type>Text</Doc_Type>
  </Document>
</Documents>

I tried the Below XSLT but cant get the output the child nodes of same value of different parent node should be removed and the remaining child nodes of parent of the duplicate child node needs to be appended to the previous parent nodeenter code here

    <?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h3>Details of each Documents. Xpath expression = "/Documents/Document"</h3>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>ID_DOC</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Doc_Type</th>
               </tr>

            <xsl:for-each select = "/Documents/Document">
                <xsl:variable name="myVar" select="@ID_DOC"
               <xsl:for-each select = "/Documents/Document">
                    <xsl:variable name="i" select="position()+1" />
                    <xsl:variable name="myVar1" select="ID_DOC[i]"
                  <xsl:if test="$myVar != $myVar1">
            <!comment : i am trying to compare the previous node value with current node value>   
            <!comment : and also to append it to the previous parent node if present and i am not able to do so>      
                <tr>
                     <td><xsl:value-of select = "ID_DOC"/></td>
                     <td><xsl:value-of select = "firstname"/></td>
                     <td><xsl:value-of select = "lastname"/></td>
                     <td><xsl:value-of select = "Doc_Type"/></td>
                  </tr>
                   </xsl:if>
            </xsl:for-each>  
               </xsl:for-each>
            </table> 
            </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

I have tried as you explained through the link but i am getting errors like net.sf.saxon.s9api.SaxonApiException and I dont know where is the error so can you tell me where did i go wrong. Here is my code that i have Tried -->@michael.hor257k

<?xml version = "1.0" encoding = "UTF-8"?>
<xsl:stylesheet version = "1.0"
   xmlns:xsl = "http://www.w3.org/1999/XSL/Transform">

   <xsl:template match = "/">
      <html>
         <body>
            <h3>Details of each Documents. Xpath expression = "/Documents/Document"</h3>

            <table border = "1">
               <tr bgcolor = "#9acd32">
                  <th>ID_DOC</th>
                  <th>First Name</th>
                  <th>Last Name</th>
                  <th>Doc_Type</th>
               </tr>
                <xsl:key name = "Documents-by-ID_DOC" match="Document" use="ID_DOC" />
                <xsl:template match="Documents">
                    <xsl:for-each select="Document[count(. | key('Documents-by-ID_DOC', ID_DOC)[1]) = 1]">
                        <xsl:sort select="ID_DOC" />
                        <xsl:value-of select="ID_DOC" />,<br />
                        <xsl:for-each select="key('Documents-by-ID_DOC', ID_DOC)">
                                <xsl:sort select="Doc_Type" />
                                <xsl:value-of select="Doc_Type" /> (<xsl:value-of select="firstname" />)<br />
                        </xsl:for-each>
                    </xsl:for-each>
                </xsl:template> 

            </table> 
            </body>
      </html>
   </xsl:template>
</xsl:stylesheet>

Can u help with getting the output using XSLT? THANKS IN ADVANCE

  • Welcome to Stack Overflow. SO is not a "do-it-for-me" website. Please provide us your first tests and if you encountered some difficulties, we will help you. – Alexandre Tranchant Jan 12 '17 at 06:47
  • You need to learn about grouping concepts in XSLT. If you're using XSLT 2.0 you can use . In XSLT 1.0 and 2.0 you might also use an definition. For more information see how to apply group by on xslt elements. http://stackoverflow.com/questions/2146648/how-to-apply-group-by-on-xslt-elements Request by Kai Weber and Umar Faraz. – umar faraz Jan 12 '17 at 08:16
  • Start here: http://www.jenitennison.com/xslt/grouping/muenchian.html – michael.hor257k Jan 12 '17 at 09:48
  • Can you give detailed Explanation on it by an Example @Kai Weber and Umar Faraz Thanks a lot – Harsha Chinni Jan 12 '17 at 10:52
  • Your desired xml output does not match attempted XSLT as you are transforming to html. For your html table, you neither need looping nor grouping. – Parfait Jan 13 '17 at 04:25

0 Answers0