如何将一个XML文件“包含”到另一个XML文件中?

时间:2022-04-20 20:56:32

I have a XML schema like:

我有一个XML模式:

<?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>

A valid XML document according to this schema would be:

根据这个模式,有效的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>
  <TextBody>My text body which is always different...</TextBody>
</MyDocument>

Question 1: Is there a way to "include" the TextHeader element from another file?

问题1:是否有一种方法可以“包含”来自另一个文件的TextHeader元素?

Like so:

像这样:

File "Header.xml":

文件“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>

File "CompleteDocument.xml":

文件“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>

Question 2: If it's possible at all, will the .NET XMLSerializer be able to parse and understand the document containing such an "include..."?

问题2:如果可能的话,.NET XMLSerializer是否能够解析和理解包含“include…”的文档?

Thank you for help in advance!

提前感谢您的帮助!

3 个解决方案

#1


3  

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.

没有对这两个问题。您可以编写一些东西(比如app.config如何允许导入部分,或者xslt如何处理包含/导入),但是这不是嵌入到任何XML规范中,也不匹配现有的模式。基本上,你得自己动手。

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.

如果您真的想走这条路,那么编写一个自定义XmlReader来识别一个特定的元素(在特定的xmlns中),然后在那个时候悄悄地合并——这可能是最佳选择。

#2


0  

If you can modify your schema, you might be able to use XML entities.

如果您可以修改您的模式,您可能可以使用XML实体。

#3


0  

The answer to the first question is Yes. You can do an xInclude to get the content included in the master file (http://www.w3.org/TR/xinclude/)

第一个问题的答案是肯定的。您可以使用xInclude来获取主文件中包含的内容(http://www.w3.org/TR/xinclude/)

#1


3  

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.

没有对这两个问题。您可以编写一些东西(比如app.config如何允许导入部分,或者xslt如何处理包含/导入),但是这不是嵌入到任何XML规范中,也不匹配现有的模式。基本上,你得自己动手。

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.

如果您真的想走这条路,那么编写一个自定义XmlReader来识别一个特定的元素(在特定的xmlns中),然后在那个时候悄悄地合并——这可能是最佳选择。

#2


0  

If you can modify your schema, you might be able to use XML entities.

如果您可以修改您的模式,您可能可以使用XML实体。

#3


0  

The answer to the first question is Yes. You can do an xInclude to get the content included in the master file (http://www.w3.org/TR/xinclude/)

第一个问题的答案是肯定的。您可以使用xInclude来获取主文件中包含的内容(http://www.w3.org/TR/xinclude/)