xsl-list
[Top] [All Lists]

Re: [xsl] FW: I need help with converting XML document different formed XML document.

2009-06-30 12:58:32
Johnson, Mike wrote:

Example XML Document:
<?xml  version="1.0" encoding="utf-8"?>

<fxf version="1.0" data="hold">
     <report records="1583" lines="1583" >
         <column_desc>
            <col colnum="c0" fieldname="test"></col>
         </column_desc>
         <table>
             <tr linetype="data" linenum="1">
                 <td colnum="c0">01</td>
                 <td colnum="c1">E1c06</td>
                 <td colnum="c2">40797115201</td>
             </tr>
             <tr linetype="data" linenum="2">
                 <td colnum="c0">02</td>
                 <td colnum="c1">E2c06</td>
                 <td colnum="c2">50797115201</td>
             </tr>

         </table>
      </report>
</fxf>

Needed Format:
<?xml  version="1.0" encoding="utf-8"?>

<fxf version="1.0" data="hold">
    <report records="1583" lines="1583" >
       <column_desc>
           <col colnum="c0" fieldname="test"></col>
       </column_desc>
       <table>
           <item linetype="data" linenum="1" c0="01" c1=" E1c06" c2="40797115201" 
></item>
           <item linetype="data" linenum="2" c0="02" c1=" E2c06" c2="50797115201" 
></item>
       </table>
    </report>
</fxf>

The following is an identity transformation plus a template for tr elements ensuring that transforms them into item elements wher only the attributes and the td elements are processed plus a template that transforms td elements into attributes:


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tr">
    <item>
      <xsl:apply-templates select="@* | td"/>
    </item>
  </xsl:template>

  <xsl:template match="td">
    <xsl:attribute name="{(_at_)colnum}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

That should do what you described, with the exception of those spaces in front of e.g. c1=" E1c06". If you really want those then you need


<xsl:stylesheet
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform";
  version="1.0">

  <xsl:template match="@* | node()">
    <xsl:copy>
      <xsl:apply-templates select="@* | node()"/>
    </xsl:copy>
  </xsl:template>

  <xsl:template match="tr">
    <item>
      <xsl:apply-templates select="@* | td"/>
    </item>
  </xsl:template>

  <xsl:template match="td">
    <xsl:attribute name="{(_at_)colnum}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

  <xsl:template match="td[(_at_)colnum = 'c1']">
    <xsl:attribute name="{(_at_)colnum}">
      <xsl:value-of select="concat(' ', .)"/>
    </xsl:attribute>
  </xsl:template>

</xsl:stylesheet>

--

        Martin Honnen
        http://msmvps.com/blogs/martin_honnen/

--~------------------------------------------------------------------
XSL-List info and archive:  http://www.mulberrytech.com/xsl/xsl-list
To unsubscribe, go to: http://lists.mulberrytech.com/xsl-list/
or e-mail: <mailto:xsl-list-unsubscribe(_at_)lists(_dot_)mulberrytech(_dot_)com>
--~--

<Prev in Thread] Current Thread [Next in Thread>