问题描述
如何将一个 XSD 的 XML 转换为另一种非常相似但具有不同 XSD 文件的 XML 格式?XSD 非常大并且有很多复杂的类型,但实际的 XML 看起来非常相似.
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.
我有两个 XSD 文件和两个 XML 文件 - 它们都成功验证到其中一个 XSD 文件.我想将一个 XML 文件转换为另一个,以便我只能使用一个类进行进一步的操作.
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.
如何在 .NET 4.0 和 c# 4.0 中执行此操作?我必须使用 XSLT 还是什么?如果我必须使用 XSLT,我该怎么做?我不确定我是否期待创建 XSLT 文档.
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.
使用 AutoMapper 将一个 XML 类转换为另一个是一场噩梦.当我查看 XML 时,它是如此相似,所以我认为可能有更简单的方法...
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...
推荐答案
我肯定会使用 XSLT.如果 XML 非常相似,应该不会太难.从身份转换开始,然后在需要更改时覆盖它.
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.
以下示例仅将foo"元素更改为bar"元素.:
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>
资源:
http://www.jenitennison.com/xslt/
http://www.mulberrytech.com/quickref/XSLT_1quickref-v2.pdf
http://www.mulberrytech.com/xsl/xsl-list/index.html#archive
此外,XSLT 的很大一部分是 XPath.如果您没有开发工具(我最喜欢的是 oXygen,@DimitreNovatchev 有一个很棒的工具,称为 oXygena href="http://www.huttar.net/dimitre/XPV/TopXML-XPV.html" rel="nofollow">XPath 可视化工具.
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.
这篇关于如何将一个 XSD 的 XML 转换为另一种非常相似但具有不同 XSD 文件的 XML 格式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!