How to convert XML for one XSD to another XML format, which is very similar but has a different XSD file?

How to convert XML for one XSD to another XML format, which is very similar but has a different XSD file? 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 successfully validate one of the XSD files. I would like to convert one of the XML files to another so that I can use only one class for further operations.

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

It was just a nightmare using AutoMapper to convert one XML class to another. When I looked at XML, it was so similar, so I thought there might be an easier way ...

+4
source share
2 answers

I would definitely use XSLT. If XML is very similar, it should not be too complicated. Start by transforming your identity, and then redefine it when something needs to change.

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

<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

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

+2
source

I used Altova MapForce to create an XSLT file. The interface is easy to use by downloading the source and target XSD / DTD files and then matching the corresponding elements. Then use .NET to convert the source XML to the target XML using an XSLT file.

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

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

+1
source

Source: https://habr.com/ru/post/1398838/


All Articles