Search code examples
xsltxml-serializationxstream

unwanted elements in the xml


I have following DTO

@XStreamAlias("outline")
public class OutlineItem implements java.io.Serializable {

    private static final long serialVersionUID = -2321669186524783800L;

    @XStreamAlias("text")
    @XStreamAsAttribute
    private String text;

    @XStreamAsAttribute
    @XStreamImplicit
    private List<OutlineItem> childItems;
}

once i do

XStream stream = new XStream();
stream.processAnnotations(OutlineItem.class);
stream.toXML(outlineItem.getChildItems()); //This is a List of all the child items

i get this as my output text

<List>
    <outline text="Test Section1">
        <outline text="Sub Section1 1">
        </outline>
        <outline text="Sub Section1 2">
        </outline>
    </outline>
    <outline text="Test Section 2">
        <outline text="Test Section2 1">
        </outline>
    </outline>
</List>

whereas i want the output to be:

<outline text="Test Section1">
    <outline text="Sub Section1 1">
    </outline>
    <outline text="Sub Section1 2">
    </outline>
</outline>
<outline text="Test Section 2">
    <outline text="Test Section2 1">
    </outline>
</outline>

How do i get rid of the Initial List tag? any help is greatly appreciated.

PS> This is a extension of the question i had asked a couple of weeks back

Can this be achieved by XSLT at all?


Solution

  • The XSLT answer is immediate:

    • identity transformation (a variant seems enough given your input sample)
    • "uncopy" List rule

    [XSLT 1.0]

    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    
    <xsl:template match="*">
      <xsl:copy>
      <xsl:copy-of select="@*"/>
      <xsl:apply-templates/>
      </xsl:copy>
    </xsl:template>
    
    <xsl:template match="List">
      <xsl:apply-templates/>
    </xsl:template>
    
    </xsl:stylesheet>