2011-08-22 41 views
1

我有以下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; 
} 

一旦我做

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

我得到這個作爲我的文本輸出

<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> 

,而我所要的輸出是:

<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> 

我該如何擺脫初始列表標記?任何幫助是極大的讚賞。

PS>這是問題的一個擴展我問過幾個星期back

可以這樣用XSLT在所有實現?

回答

0

的XSLT答案是直接:

  • 身份轉換(變體似乎足以給你的輸入樣本)
  • 「uncopy」 列表規則

[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> 
+0

其實,身份規則是完全不同的你稱之爲「身份轉換」的模板。請改正。 –

+0

對不起,但這不僅是術語。您所謂的「身份轉換」不會複製到輸出某些類型的節點,如註釋和處理指令。請糾正誤導性文字。 –

+0

完全同意,與您的答案唯一的問題是調用「身份轉換」不是這樣的模板。請問,請你糾正? –