简体   繁体   English

将多个文件合并为单个XML文件的最佳方法?

[英]Best way to combine multiple files into a single XML file?

I have a collection of files that I'd like to be able to combine into a single XML, according to a specific schema. 我有一个文件集合,可以根据特定的模式将其合并为一个XML。

I'm thinking about something like using XSLT with a custom XmlResolver (C# / .NET), although I'm not sure if that would give me enough control over how the XPath expressions in the XSLT are parsed to be able to direct certain expressions to certain files. 我正在考虑将XSLT与自定义XmlResolver (C#/ .NET)结合使用的方法,尽管我不确定这是否能使我充分控制XSLT中的XPath表达式如何解析以能够定向某些表达式某些文件。

The files to be combined will be XML fragments. 要合并的文件将是XML片段。 In general, the entire fragment will be incorporated into the XML destination, though it would be nice to have the option of just incorporating certain fields. 通常,整个片段都将合并到XML目标中,尽管最好只合并某些字段。

Example: 例:

Starting with three fragment files: 从三个片段文件开始:

one.txt: contains <One>Number One</One> [or maybe just "Number One"]
two.txt: contains <Two>Number Two</Two> [or "Number Two"]
three.txt: contains <Three>Number Three</Three> [or "Number Three"]

I want to combine one.txt and two.txt into the following, while ignoring three.txt: 我想将one.txt和two.txt合并为以下内容,而忽略three.txt:

<Numbers>
  <One>Number One</One>
  <Two>Number Two</Two>
</Numbers>

I'm looking for a descriptive approach, not custom code, since there can be many different fragments and many different combinations. 我正在寻找一种描述性的方法,而不是自定义代码,因为可以有许多不同的片段和许多不同的组合。

I know the code below won't work, but something like this: 我知道下面的代码将无法正常工作,但类似这样:

<Numbers>
    <xsl:value-of select="mine:one.txt/One" />
    <xsl:value-of select="mine:two.txt/Two" />
</Numbers>

The XML fragments can be complex, so a simple file-merge isn't enough. XML片段可能很复杂,因此简单的文件合并是不够的。 I will probably want some form of XPath / XQuery to extract the relevant pieces. 我可能希望某种形式的XPath / XQuery提取相关的部分。

Any ideas? 有任何想法吗?

What's wrong with 怎么了

<Numbers>
    <xsl:copy-of select="doc('mine:one.txt')/One" />
    <xsl:copy-of select="doc('mine:two.txt')/Two" />
</Numbers>

I don't think you've explained why you think you might need a custom XmlResolver, but if you do, then it's no problem 我不认为您已经解释了为什么您认为可能需要自定义XmlResolver的原因,但是如果您这样做了,那就没问题了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM