5

How to transform XML for one XSD into another XML format that is very similar but has a different XSD file? The XSD is quite large and has many complex types, but the actual XML looks very similar.

I have two XSD files and two XML files - they both validate successfuly to one of the XSD files. I would like to transform one of the XML files into the other so that I can use only one class for further operations.

How do I do this in .NET 4.0 and c# 4.0 ? Do I have to use XSLT or something? If I do have to use XSLT, how do I do this? I'm not sure I'm looking forward to creating an XSLT document.

It was kind of a nightmare using AutoMapper to convert one XML class into the other. When I looked at the XML it was so similar so I thought there may be an easier way...

user610064
  • 481
  • 2
  • 11
  • 25
  • Even if you don't like it: The usual way to do this is by Xslt - but if your schemas are very similar this should be easy. Maybe show us parts of your Xml docs that differ so we could tell you more. – Filburt Feb 28 '12 at 15:44
  • So, why not give an example of the source XML document and the exact wanted transformed XML document? Please, edit the question and provide these -- then many people will be able to give you the wanted transformation. We may even not need the XSD's. – Dimitre Novatchev Feb 28 '12 at 15:44

2 Answers2

2

I would definitely use XSLT. If the XML is very similar, it shouldn't be too difficult. Start with an identity transform and then override it when something needs to change.

The following example only changes "foo" elements to "bar" elements.:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output indent="yes"/>
  <xsl:strip-space elements="*"/>

  <!--Identity Template. This will copy everything as-is.-->
  <xsl:template match="@*|node()">
    <xsl:copy>
      <xsl:apply-templates select="@*|node()"/>
    </xsl:copy>
  </xsl:template>

  <!--Change "foo" element to "bar" element.-->
  <xsl:template match="foo">
    <bar>
      <xsl:apply-templates select="@*|node()"/>
    </bar>
  </xsl:template>

</xsl:stylesheet>

Resources:

http://www.w3.org/TR/xslt

http://www.jenitennison.com/xslt/

http://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf

http://www.mulberrytech.com/xsl/xsl-list/index.html#archive

Also, a huge part of XSLT is XPath. If you don't have a development tool (my favorite is oXygen, @DimitreNovatchev has a great tool called the XPath Visualizer.

Daniel Haley
  • 51,389
  • 6
  • 69
  • 95
  • Thanks for your reply and example. It does sound fairly simple. Are there good resource(s) links that are especially helpful for what I want?(complex types) – user610064 Feb 28 '12 at 15:59
  • @user610064 - You're very welcome. I will add a few links for resources that I like. There's also w3schools, but I don't use it. stackoverflow.com is my favorite resource. – Daniel Haley Feb 28 '12 at 16:05
  • Using a tool to generate XSLT is fine but that does not help automate the transformation. What if I have a requirement of automate the process of transforming a bulk of input XML files with certain schema, regularly placed in some directory, to another schema and place in another directory? – Tanmoy Sengupta Aug 17 '21 at 17:09
1

I used Altova MapForce to generate a XSLT file. The interface is easy to use by loading the source and destination XSD/DTD files and then mapping the matching elements. Then use .NET to transform the source XML to destination XML using the XSLT file.

http://www.altova.com/mapforce.html

http://msdn.microsoft.com/en-us/library/14689742.aspx

user2601995
  • 6,463
  • 8
  • 37
  • 41