Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

XML Conversion using XSLT

Status
Not open for further replies.

dave1167

Programmer
May 4, 2006
2
GB
Hi

| am a newbie with XML but from a Asp.net background. I need to convert an XML file to different format.
The file that needs to be converted looks like this
<products>
<product>
<program_id><![CDATA[106]]></program_id>
<program_name><![CDATA[Widets Inc]]></program_name>
<product_id><![CDATA[106OFB]]></product_id>
<product_name><![CDATA[Large Widget]]</product_name>
<description><![CDATA[Large Widget with brown wotsits.]]></description>
<price><![CDATA[99.95]]></price>
<last_updated><![CDATA[2006-04-11 10:12:23]]></last_updated>
</program_id>
</product>
</products>
and I need it to be converted into an XML file below

<products>
<product>
<ProductID><![CDATA[5613]]></ProductID>
<ProductName><![CDATA[Widget 1]]></ProductName>
<ProductPrice><![CDATA[9.99]]></ProductPrice>
<ProductDescription><![CDATA[A Black widget with lots of wotsits attached?]]></ProductDescription>
<ProductUpdatedDate><![CDATA[04-04-2005 12:40]]></ProductUpdatedDate>
</product>
</product>

As can be seen I dont need all the fields(elements?)(ie. program name) but I do need the field names changing to the correct case and description. I think I need to produce an XSLT sheet but after sweating for the last two days without much success. All I seem to get is one long product node with attributes without CDATA Can anyone suggest what the XSLT file should look like?
 
Take the XSLT tutorial at and see if you can make a first attempt at the XSLT.

If you expect the XSLT to massage the actual description text (as opposed to the element tag names), that would be an unusual (though not impossible) use of XSLT.

Tom Morrison
 
Are you choosing these new tag names yourself? Whats the point in prefixing everything with "Product". Thes nodes are inherently associated with product anyway. Much better to have
Code:
<products>
  <product>
    <ID><![CDATA[5613]]></ID>
    <Name><![CDATA[Widget 1]]></Name>
    <Price><![CDATA[9.99]]></Price>
    <Description><![CDATA[A Black widget with lots of wotsits attached?]]></Description>
    <UpdatedDate><![CDATA[04-04-2005 12:40]]></UpdatedDate>
  </product>
</product>
You cannot keep the CDATA sections, these get transformed to their syntatic equivalent before the transform and are not part of the XML tree. To rename the nodes and discard some, the most efficient and extensible solution is using the identity transform:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="[URL unfurl="true"]http://www.w3.org/1999/XSL/Transform">[/URL]
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>
  <xsl:template match="program_id"/>
  <xsl:template match="program_name"/>
  <xsl:template match="product_id">
    <ProductID>
      <xsl:apply-templates select="@*|node()"/>
    </ProductID>
  </xsl:template>
  <xsl:template match="product_name">
    <ProductName>
      <xsl:apply-templates select="@*|node()"/>
    </ProductName>
  </xsl:template>
  <xsl:template match="price">
    <ProductPrice>
      <xsl:apply-templates select="@*|node()"/>
    </ProductPrice>
  </xsl:template>
  <xsl:template match="description">
    <ProductDescription>
      <xsl:apply-templates select="@*|node()"/>
    </ProductDescription>
  </xsl:template>
  <xsl:template match="last_updated">
    <ProductUpdatedDate>
      <xsl:apply-templates select="@*|node()"/>
    </ProductUpdatedDate>
  </xsl:template>
</xsl:stylesheet>

Jon

"I don't regret this, but I both rue and lament it.
 
Jon

Wow that's amazing it worked first time, the tag names where not my choice they were names that I have inherited from previous programmer.

Many thanks for your help. I relaise now the Cdata section is not important :-D

Dave
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top