C# - 将 RDF 文件转换为特定的 XML 文件
我们有一个需求,需要将给定的rdf文件转换为特定的xml文件。所以我们有三个输入:RDFS 文件、RDF 文件和 XSD 文件。有了这些,我们需要创建包含 RDF 文件数据的 XML 文件(应符合 XSD)。 RDF 模式包含简单、复杂、关系实体。 RDF 中定义的关系与 XSD 中定义的关系不同。
我们看到了实现相同功能的几种方法:
- 创建一个 XSLT,定义将 RDF 文件转换为 XML 文件的逻辑。
- 构建自定义 C# 应用程序。做同样的事情(使用 RDF 库,如 LinqToRDF 等),
请指导哪个选项更好,以及任何相同的指针。
问候,
We have a requirement in which we need to convert a given rdf file into a specific xml file. So we have three inputs RDFS file, RDF file and XSD file. With these we need to create XML file (which should conform to the XSD) having the data of the RDF file. The RDF schema contains simple, complex, relation entities. The relations defined in the RDF are different from the relations defined in the XSD.
We see couple of ways of implementing the same:
- Create an XSLT defining the logic for converting the RDF file to XML file.
- Build a custom C# app. for doing the same (using RDF libraries like LinqToRDF etc.)
Please guide regarding which option would be better and any pointers for the same.
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先:这不是一个“非此即彼”的决定。您可以轻松地将 XSLT 转换与自定义 C# 代码结合起来。也就是说:
根据我的经验,如果转换不包含太多逻辑,XSLT 是一个很好的选择。使用 XSLT 重命名标签、展平或重新排列层次结构等都很容易,但如果您需要“if-then-else”逻辑(例如“创建标签 A if value of ... 但仅当 ”),则可能会很麻烦。 ..否则创建标签 B”。
这句话
听起来像是一个复杂的转换,其中涉及相当多的“if-then-else”和“查找”逻辑。在这种情况下,我可能会选择更以 C# 为中心的解决方案。
First of all: It's not a "either ... or" decision. You can easily combine an XSLT transformation with custom C# code. That said:
In my experience XSLT is a great choice, if the transformation does not contain too much logic. It's easy to rename tags, to flatten or rearrange hierarchies, ... using XSLT, but it can be come nasty if you need "if-then-else" logic like "create tag A if value of ... but only if ... else create tag B".
The sentence
sounds like a complex transformation, which involves quite some "if-then-else" and "look up" logic. In that case I would probably go for a more C# centric solution.
将 RDF/XML 转换为其他格式时,XSLT 通常不是一个好主意,因为相同的 RDF 数据可以以不同的方式编码为 RDF/XML。
因此,除非您的 RDF/XML 输入受到严格控制,否则纯 XSLT 转换通常非常困难且难以维护。
对于 RDF 的 C# 操作,您可能需要尝试 dotNetRDF (免责声明 - 我开发该库 )它为您提供了一个在 Triple 级别上使用 RDF 的 API。如果您的 RDF 中有规则的重复结构,那么提取该数据的最简单方法是使用 SPARQL 查询从 RDF 中获取与您相关的数据。一旦获得结果(其结构类似于 DataTable,如果您更容易使用,可以将其转换为 DataTable),您可以根据需要生成 XML 文件。
如果这看起来对您来说可能是一个可行的选择,那么如果您需要任何帮助/建议,请随时在项目邮件列表上给我发一封电子邮件。
XSLT is generally a bad idea for converting RDF/XML into other formats because the same RDF data can be encoded in RDF/XML in different ways.
So unless your RDF/XML input is tightly controlled a pure XSLT transformation is usually quite difficult and hard to maintain.
For C# manipulation of RDF you might want to try dotNetRDF (disclaimer - I develop the library) which gives you an API for working with RDF at the Triple level. If your RDF has regular repeating structures in it then the easiest way to extract that data will be to use SPARQL queries to get the data of relevance to you out of the RDF. Once you have the results (which are similar to a DataTable in structure and can be cast to a DataTable if that is easier for you to work with) you can generate your XML file as desired.
If this looks like it might be a viable option for you then feel free to drop me an email on the project mailing lists if you want any help/advice.