XSLT - 使用子节点重复父节点

时间:2013-11-25 09:55:40

标签: xml xslt

我有一个具有以下结构的xml:

<FlightDetails>
                        <CouponNumber>1</CouponNumber>
                        <ServiceClass>Y</ServiceClass>
</FlightDetails>
<FlightDetails>
                        <CouponNumber>2</CouponNumber>
<ServiceClass>Y</ServiceClass>
</FlightDetails>
<FlightDetails>
                        <CouponNumber>3</CouponNumber>
                        <ServiceClass>Y</ServiceClass>
</FlightDetails>

使用的xslt是:

<FlightDetails>
          <xsl:for-each select="FlightDetails/CouponNumber">
          <CouponNumber>
          <xsl:value-of select="." />
          </CouponNumber>
          </xsl:for-each>
          <xsl:for-each select="FlightDetails/ServiceClass">
          <ServiceClass>
          <xsl:value-of select="." />
          </ServiceClass>
          </xsl:for-each>
</FlightDetails>

使用xslt将xml转换为输出xml显示:

<FlightDetails>
<CouponNumber>1</CouponNumber>
<CouponNumber>2</CouponNumber>
<CouponNumber>3</CouponNumber>
<ServiceClass>Y</ServiceClass>
<ServiceClass>Y</ServiceClass>
<ServiceClass>Y</ServiceClass>
</FlightDetails>

但是,所需的输出xml应如下所示:

<FlightDetails>
    <CouponNumber>1</CouponNumber>
    <ServiceClass>Y</ServiceClass>
</FlightDetails>
<FlightDetails>
    <CouponNumber>2</CouponNumber>
    <ServiceClass>Y</ServiceClass>
</FlightDetails>
<FlightDetails>
    <CouponNumber>3</CouponNumber>
    <ServiceClass>Y</ServiceClass>
</FlightDetails>

如何修改XSLT以实现上述输出xml结构?

1 个答案:

答案 0 :(得分:0)

你可以试试,

<强> XSL:

 <?xml version="1.0" encoding="UTF-8"?>
    <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
       <xsl:output indent="yes" />

    <xsl:template match="/">
         <xsl:copy-of select="//FlightDetails"/>
    </xsl:template>
    </xsl:stylesheet>

<强>输出:

<?xml version="1.0" encoding="UTF-8"?>
<FlightDetails>
      <CouponNumber>1</CouponNumber>
      <ServiceClass>Y</ServiceClass>
   </FlightDetails>
<FlightDetails>
      <CouponNumber>2</CouponNumber>
      <ServiceClass>Y</ServiceClass>
   </FlightDetails>
<FlightDetails>
      <CouponNumber>3</CouponNumber>
      <ServiceClass>Y</ServiceClass>
   </FlightDetails>