0

我得到了预期的输出 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
4

1 回答 1

1

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

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

最简单的解决方法是添加

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

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

于 2013-09-23T13:39:52.283 回答