在 XML/XSL 文件中包含 XML 文件 [英] Including an XML file in an XML/XSL file
问题描述
所以目前我正在做一些 XML-> XSLT-> (HTML5/CSS3) 工作.现在我有一个 menu.xml 文件,我想将它包含在 XSL 文件或 XML 页面中.我已经做了很多搜索,但我无法找到一个直接的答案.
So currently I'm doing some XML-> XSLT-> (HTML5/CSS3) work. Right now I have a menu.xml file, and I'd like to include it in either the XSL file or the XML page. I've done lots of searching, but I'm unable to find a straightforward answer.
那么,如何将 XML 文件包含到另一个 XML 文件或 XSL 文件中?
So, how do I include an XML file in to another XML file or in to a XSL file?
通过包含,我的意思是从另一个文件引用/加载它,而不是复制和粘贴它或简单地嵌入它.
By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.
推荐答案
I.以下是如何将任何 XML 文档或片段嵌入 XSLT 样式表并在转换期间使用:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="my:my">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<my:menu>
<menu>
<choice>A</choice>
<choice>B</choice>
<choice>C</choice>
</menu>
</my:menu>
<xsl:template match="/">
<xsl:copy-of select="document('')/*/my:menu/*"/>
</xsl:template>
</xsl:stylesheet>
当此转换应用于任何 XML 文档(本示例中未使用)时,会产生想要的结果(只是复制 XML):
<menu xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="my:my">
<choice>A</choice>
<choice>B</choice>
<choice>C</choice>
</menu>
记住:任何 XML 都可以嵌入到 XSLT 样式表中,前提是它被包装到命名空间元素(命名空间而不是 XSLT 命名空间)中,并且这个包装元素处于全局级别(子
(顶部)元素).
Remember: Any XML can be embedded into an XSLT stylesheet, provided it is wrapped into a namespaced element (the namespace not the XSLT namespace) and this wrapping element is at the global level (a child of the <xsl:stylesheet>
(top) element).
二.访问驻留在单独 XML 文件中的 XML 菜单文件:
要做到这一点,我们只需要稍微改变前面的例子:
To do this we have to change only slightly the previous example:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="/">
<xsl:copy-of select="document('menu.XML')/*"/>
</xsl:template>
</xsl:stylesheet>
如果菜单 XML 文件在 'menu.XML'
文件中(与 XSLT 样式表文件在同一目录中,则此转换产生完全相同的结果和以前一样:
If the menu XML file is in the 'menu.XML'
file (in the same directory as the XSLT stylesheet file, then this transformation produces exactly the same result as the previous:
<menu>
<choice>A</choice>
<choice>B</choice>
<choice>C</choice>
</menu>
请注意:在这两种情况下,我们都使用标准的 XSLT 函数document()
Do note: In both cases we are using the standard XSLT function document()
通常,定义一个全局级别的变量,其值是调用document()
函数的结果.然后在转换过程中通过 XPath 表达式访问该变量及其内容.
Typically, one defines a global-level variable, whose value is the result of calling the document()
function. Then this variable and its contents is accessed via XPath expressions during the transformation.
这篇关于在 XML/XSL 文件中包含 XML 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!