XSLT转换问题:转换后Xml以纯文本形式出现

时间:2013-09-23 12:44:23

标签: xml xslt groovy

我得到了预期的输出Xml,其中部分xml在输出xml的末尾打印为纯文本。
如何在这里删除纯文本xml?

输入Xml

<Coverage>
  <CoverageInformation>
    <IssueStateCT>AK</IssueStateCT>
    <OptionCodeCT>ADR</OptionCodeCT>
    <SegmentStatusCT>Frozen</SegmentStatusCT>
    <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffectiveDate>09/17/2013</EffectiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/16/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
  </CoverageInformation>
  <RiderInformation>
    <IssueStateCT>AK</IssueStateCT>
    <OptionCodeCT>ADR</OptionCodeCT>
    <SegmentStatusCT>Frozen</SegmentStatusCT>
    <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffectiveDate>09/17/2013</EffectiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/16/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
  </RiderInformation>
  <RiderInformation>
    <IssueStateCT>AK</IssueStateCT>
    <OptionCodeCT>ADR</OptionCodeCT>
    <SegmentStatusCT>Frozen</SegmentStatusCT>
    <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffectiveDate>09/17/2013</EffectiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/16/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
  </RiderInformation>
</Coverage>

XSLT

<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="CoverageInformation">
      <SegmentVO>
        <xsl:for-each select="*">
          <xsl:copy>
            <xsl:apply-templates/>
          </xsl:copy>
        </xsl:for-each>
        <xsl:for-each select="../RiderInformation">
          <SegmentVO>
            <xsl:for-each select="*">
              <xsl:copy>
                <xsl:apply-templates/>
              </xsl:copy>
            </xsl:for-each>
          </SegmentVO>
        </xsl:for-each>
      </SegmentVO>>
    </xsl:template>
</xsl:stylesheet>

代码:

def w = new StringWriter()
        TransformerFactory.newInstance()
                          .newTransformer( new StreamSource( new StringReader( xslt ) ) )
                          .transform( new StreamSource( new StringReader( xmlAsString ) ),
                          new StreamResult( w ) )
        println groovy.xml.XmlUtil.serialize( w.toString() )

输出Xml

ERROR:  'Content is not allowed in trailing section.'
<?xml version="1.0" encoding="UTF-8"?>
<SegmentVO>
  <IssueStateCT>AK</IssueStateCT>
  <OptionCodeCT>ADR</OptionCodeCT>
  <SegmentStatusCT>Frozen</SegmentStatusCT>
  <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
  <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
  <CreationDate>09/17/2013</CreationDate>
  <EffectiveDate>09/17/2013</EffectiveDate>
  <IssueDate>09/17/2013</IssueDate>
  <TerminationDate>09/16/2013</TerminationDate>
  <RateSeriesDate>09/17/2013</RateSeriesDate>
  <SegmentVO>
    <IssueStateCT>AK</IssueStateCT>
    <OptionCodeCT>ADR</OptionCodeCT>
    <SegmentStatusCT>Frozen</SegmentStatusCT>
    <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffectiveDate>09/17/2013</EffectiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/16/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
  </SegmentVO>
  <SegmentVO>
    <IssueStateCT>AK</IssueStateCT>
    <OptionCodeCT>ADR</OptionCodeCT>
    <SegmentStatusCT>Frozen</SegmentStatusCT>
    <ApplicationReceivedDate>09/18/2013</ApplicationReceivedDate>
    <ApplicationSignedDate>09/10/2013</ApplicationSignedDate>
    <CreationDate>09/17/2013</CreationDate>
    <EffectiveDate>09/17/2013</EffectiveDate>
    <IssueDate>09/17/2013</IssueDate>
    <TerminationDate>09/16/2013</TerminationDate>
    <RateSeriesDate>09/17/2013</RateSeriesDate>
  </SegmentVO>
</SegmentVO>
CT
AggPremium
IssuePendingPremium
08/15/2013
08/15/2013
08/05/2013
08/05/2013
09/19/2013
08/15/2013
09/20/2013


CT
AggPremium
IssuePendingPremium
08/15/2013
08/15/2013
08/05/2013
08/05/2013
09/19/2013
08/15/2013
09/20/2013

1 个答案:

答案 0 :(得分:1)

XSLT具有默认built-in template rules,适用于尚未指定显式模板的任何节点。由于您只为CoverageInformation元素提供了模板,因此应用了模板的任何其他节点都将使用默认规则。

具有子节点(例如元素)的节点的默认规则只是<xsl:apply-templates/>到所有子节点。文本节点的默认规则是将文本打印到结果树。这些规则结合在一起,打印RiderInformation元素中包含的所有文本,但删除了周围的标记。

最简单的解决方法是添加

<xsl:template match="/">
  <xsl:apply-templates select="Coverage/CoverageInformation" />
</xsl:template>

将仅将模板应用于您感兴趣的元素,而不是将默认模板应用于文档的其余部分。