重用其他文件中的 XML 元素 [英] Reuse XML element from other file

查看:29
本文介绍了重用其他文件中的 XML 元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在另一个 XML 中使用来自其他文件的 XML 元素?
例如,而不是:

Is it possible to use a XML element from other file in another XML?
For instance, instead of having:

<document>
   <a><!-- huge content --></a>
   <b/>
</document>

我想要:

<document>
    <a ref="aDef"/>
    <b/>
</document>

Where 在它自己的 XML 中定义并在需要的地方重用.我希望这由解析器完成并且对应用程序透明,应用程序将无法知道该元素是引用还是副本.
我怎样才能完成这项工作?

Where is defined in its own XML and reused where needed. I would like this to be done by the parser and transparent to the application, the app would not be able to know if the element is a reference or a copy.
How can i get this done?

推荐答案

这就是 xinclude W3C 标准适用于.类似于外部实体方法(如上面的答案),您可以将要包含的内容编码在一个单独的文件中,例如(frag.xml):

This is what the xinclude W3C standard is for. Similar to the external entities approach (as in above answer), you can encode the content-to-be-included in a separate file, like e.g. (frag.xml):

<a><!-- huge content --></a>

在主 XML 文件中,一个 xinclude 指令引用了这个外部内容:

In the main XML file, an xinclude instruction refers to this external content:

<document>
  <xi:include href="frag.xml" xmlns:xi="http://www.w3.org/2001/XInclude"/>
  <b/>
</document>

当使用(xinclude-capable)XML 处理器(例如 Xerces http://xerces.apache.org/)处理时,解析器将使用它指向的内容扩展此 xinclude 指令.@href 属性中的包含目标被解释为 URI,因此您可以同样使用片段标识符(例如 href="frag.xml#fragment1)指向片段.

When processed with an (xinclude-capable) XML processor (like e.g. Xerces http://xerces.apache.org/), the parser will expand this xinclude instruction with the contents it points at. The inclusion target in the @href attribute is interpreted as a URI, so you can equally point to fragments using fragment identifiers (e.g. href="frag.xml#fragment1).

除了@href 中的简单 URI 之外,xinclude 标准还支持非常细粒度的词汇表,用于在 @xpointer 属性中表达包含目标.但是,对复杂 XPointer 表达式的支持取决于处理器的 XPointer 合规性,这通常未被充分利用.但是,还有一个(最小的)XSLT 实现:XIPr (http://dret.net/projects/xipr/).

Besides simple URIs in @href, the xinclude standard supports a very fine-grained vocabulary for expressing the inclusion target in a @xpointer attribute. However, support for complex XPointer expressions depends on the processor's XPointer compliance, which is generally underused. However, there's a (minimal) XSLT implementation as well: XIPr (http://dret.net/projects/xipr/).

这篇关于重用其他文件中的 XML 元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆