我怎样才能“包括"?一个 XML 文件转换为另一个 XML 文件? [英] How can I "include" one XML file into another XML file?
问题描述
我有一个 XML 模式,如:
I have a XML schema like:
<?xml version="1.0" encoding="utf-8"?>
<xs:schema id="MySchema"
targetNamespace="http://tempuri.org/MySchema.xsd"
elementFormDefault="qualified"
xmlns="http://tempuri.org/MySchema.xsd"
xmlns:mstns="http://tempuri.org/MySchema.xsd"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="MyDocument">
<xs:complexType>
<xs:all>
<xs:element name="TextHeader" type="xs:string" minOccurs="0" />
<xs:element name="TextBody" type="xs:string" minOccurs="0" />
</xs:all>
</xs:complexType>
</xs:element>
</xs:schema>
根据此架构的有效 XML 文档将是:
A valid XML document according to this schema would be:
<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">
<TextHeader>My header which is almost always the same...</TextHeader>
<TextBody>My text body which is always different...</TextBody>
</MyDocument>
问题 1:有没有办法包含"另一个文件中的 TextHeader 元素?
像这样:
文件Header.xml":
File "Header.xml":
<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">
<TextHeader>My Header which is almost always the same...</TextHeader>
</MyDocument>
文件CompleteDocument.xml":
File "CompleteDocument.xml":
<?xml version="1.0" encoding="utf-8" ?>
<MyDocument xmlns="http://tempuri.org/MySchema.xsd">
include "Header.xml" ???
<TextBody>My text body which is always different...</TextBody>
</MyDocument>
问题 2:如果可能的话,.NET XMLSerializer
是否能够解析和理解包含此类包含..."的文档?
Question 2: If it's possible at all, will the .NET XMLSerializer
be able to parse and understand the document containing such an "include..."?
提前感谢您的帮助!
推荐答案
对这两个问题都不是.您可以编写一些代码(例如 app.config 如何允许导入部分,或者 xslt 如何处理包含/导入),但这不是内置在任何 XML 规范中,并且与现有架构不匹配.基本上,您必须自己做所有事情.
No to both questions. You could code something (like how app.config allows sections to be imported, or how xslt handles includes/imports), but this is not inbuilt onto any XML spec and would not match the existing schema. You'd have to do everything yourself, basically.
如果您真的想走这条路,编写一个自定义 XmlReader 来识别特定元素(在特定 xmlns 中)并在此时静默合并 - 可能是最佳选择.
If you really wanted to go this route, writing a custom XmlReader that recognised a specific element (in a specific xmlns) and silently merging at that point - would perhaps be the beat choice.
这篇关于我怎样才能“包括"?一个 XML 文件转换为另一个 XML 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!