[XSL-LIST Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

Re: FW: I need help with converting XML document diff

Subject: Re: FW: I need help with converting XML document different formed XML document.
From: Martin Honnen <Martin.Honnen@xxxxxx>
Date: Tue, 30 Jun 2009 18:57:59 +0200
Re:  FW: I need help with converting XML document  diff
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="{@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="{@colnum}">
      <xsl:value-of select="."/>
    </xsl:attribute>
  </xsl:template>

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

</xsl:stylesheet>

--

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

Current Thread

PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.