0

How do I construct the for loop for this below logic in Java. I have a list of items that i need to end that with the KT1 tag for every KT2 item. Please let me know what is the for loop logic to apply

This is the format that i have as Input

<KT1>
  <KT2>
     String1
  </KT2>
  <KT2>
     String2
  </KT2>
  .
  .
  .

</KT1>

Expected output

 <KT1>
   <KT2>
     String1
   </KT2>
 </KT1>

 <KT1>
   <KT2>
   String2
   </KT2>
  </KT1>

.
.
.

</KT1>
2
  • Do you need to do this in a particular programming language, if so what? If you have access to it, XSLT would be the ideal choice for this type of manipulation Commented Sep 3, 2014 at 12:15
  • @GHC - i'm trying to implement in java Commented Sep 4, 2014 at 4:57

1 Answer 1

0

OK, first up, to use XSLT with Java, see the answer to this question: java xslt tutorial

The XSLT you would need is as follows (disclaimer: I'm learning XSLT myself so, if anyone has any better solutions, please post them and comment here!)

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <!-- Template 1 (see below) -->
    <xsl:template match="/">
        <xsl:element name="Root">
            <xsl:apply-templates />
        </xsl:element>
    </xsl:template>
    <!-- Template 2 (see below) -->
    <xsl:template match="KT2">
        <xsl:element name="KT1">
            <xsl:copy>
                <xsl:apply-templates select="text()" />
            </xsl:copy>
        </xsl:element>
    </xsl:template>
    <!-- Template 3 (see below) -->
    <xsl:template match="text()">
        <xsl:copy />
    </xsl:template>
</xsl:stylesheet>

The results of applying this XSLT to your input XML are as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Root>
   <KT1>
       <KT2>String1</KT2>
   </KT1>
   <KT1>
       <KT2>String2</KT2>
   </KT1>
.
.
.
</Root>

My understanding of the way XSLT works is that it will apply templates in order of specificity. A dry run through the XLST above would be as follows:

1) The root of the XML document matches exactly to Template 1 - this tells it to output a root node and then carry on processing other elements 2) The first KT1 element doesn't match to any templates, so is not output 3) The first KT2 child of the first KT1 element matches to Template 2 - this tell it output a KT1 node, and then copy the found KT2 node and then to continue processing 4) The text of the KT2 node matches to Template 3, which just copies it out 5) The second KT2 child of the first KT1 element matches to Template 2 - and so on

The XSLT above is quite specific, it can easily be made more general - google "XSLT Identity Transforms" for a good start.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.