在 XML/XSL 文件中包含 XML 文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4703312/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Including an XML file in an XML/XSL file
提问by Jookia
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-> XSLT-> (HTML5/CSS3) 工作。现在我有一个 menu.xml 文件,我想将它包含在 XSL 文件或 XML 页面中。我已经做了很多搜索,但我无法找到一个直接的答案。
So, how do I include an XML file in to another XML file or in to a XSL file?
那么,如何将 XML 文件包含到另一个 XML 文件或 XSL 文件中呢?
Edit: By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.
编辑:通过包含,我的意思是从另一个文件引用/加载它,而不是复制和粘贴它或简单地嵌入它。
回答by Dimitre Novatchev
I. Here is how any XML document or fragment can be embedded in an XSLT stylesheet and used during the transformation:
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>
When this transformation is applied on any XML document (not used in this example), the wanted result (just copying the XML) is produced:
当此转换应用于任何 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>
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 都可以嵌入到 XSLT 样式表中,前提是它被包装到命名空间元素(命名空间而不是 XSLT 命名空间)中,并且该包装元素处于全局级别(<xsl:stylesheet>(顶部)元素的子元素)。
II. Accessing the XML menu file that resides in a separate XML file:
二、访问驻留在单独 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>
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 resultas the previous:
如果菜单 XML 文件在该'menu.XML'文件中(与 XSLT 样式表文件在同一目录中,则此转换产生与之前完全相同的结果:
<menu>
<choice>A</choice>
<choice>B</choice>
<choice>C</choice>
</menu>
Do note: In both cases we are using the standard XSLT function document()
请注意:在这两种情况下,我们都使用标准的 XSLT 函数document()
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.
通常,定义一个全局级别的变量,其值是调用document()函数的结果。然后在转换期间通过 XPath 表达式访问此变量及其内容。
回答by Mads Hansen
So, how do I include an XML file in to another XML file or in to a XSL file?
那么,如何将 XML 文件包含到另一个 XML 文件或 XSL 文件中呢?
You can use an external entityto reference the menu.xmlfile and include the content into either an XML file or the XSLT (or both).
您可以使用外部实体来引用menu.xml文件并将内容包含到 XML 文件或 XSLT(或两者)中。
By include, I mean referencing/loading it from another file, not copy and pasting it or simply embedding it.
包含,我的意思是从另一个文件引用/加载它,而不是复制和粘贴它或简单地嵌入它。
By using external entities, you can reference/load the menu.xmlcontent from external files and do not have to duplicate the XML content.
通过使用外部实体,您可以从外部文件引用/加载menu.xml内容,而不必复制 XML 内容。
For instance, if you wanted the menu.xml content included in your XSLT, you would declare the external entity in your XSLT like this:
例如,如果您希望将 menu.xml 内容包含在您的 XSLT 中,您可以像这样在 XSLT 中声明外部实体:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE stylesheet [
<!ENTITY menu SYSTEM "./menu.xml">
]>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
and could then reference it like you would any other entity:
然后可以像引用任何其他实体一样引用它:
&menu;
When the XSLT parsed, the entity reference will be expanded and the XML content of the menu.xml will be included as part of the XSLT document as if you had copy/pasted into the spot where the entity reference was.
解析 XSLT 时,实体引用将被扩展,menu.xml 的 XML 内容将作为 XSLT 文档的一部分包含在内,就像您已复制/粘贴到实体引用所在的位置一样。
回答by PaulMurrayCbr
I have a blog poston this. You use document() to get the other XML and pass around the content using parameters.
回答by Cody
I was unable to get the code to work that people provided. However I did get a solution after trying several things. Name an xml file clGroup.xml (first two letters don't matter, but I am finding "Group" is required. This xml file will reference the xml you want to parse.
我无法让人们提供的代码工作。但是,在尝试了几件事后,我确实得到了解决方案。命名一个 xml 文件 clGroup.xml (前两个字母无关紧要,但我发现“组”是必需的。这个 xml 文件将引用您要解析的 xml。
clGroup xml:
clGroup xml:
<?xml version='1.0' encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="calendarGroup.xslt" ?>
<groups>
<groupRef>caGroup.xml</groupRef>
</groups>
Now name a file caGroup.xml and put in the following code:
现在命名一个文件 caGroup.xml 并输入以下代码:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<topics xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<topic>
<topicstopic>Calendar</topicstopic>
<topicsubmitter>Calendar</topicsubmitter>
<creatoremail>[email protected]</creatoremail>
</topic>
<topic>
<name>Week1</name>
<sunday/>
<monday/>
<tuesday>Test01 Young Men Weekly</tuesday>
<wednesday>02 Elders Home Teaching Message Birthday</wednesday>
<thursday>03 </thursday>
<friday>04 </friday>
<saturday>05 #Young Men Weekly</saturday>
</topic>
<topic>
<name>Week2</name>
<sunday>06 Fast Sunday</sunday>
<monday>07 FHE</monday>
<tuesday>08 Young Men Tonight #Young Men Weekly going to Bishops storehouse 6PM to 7PM Young Men Weekly</tuesday>
<wednesday>09 </wednesday>
<thursday>10 Scout Round Table at 7 PM </thursday>
<friday>11 </friday>
<saturday>12 </saturday>
</topic>
</topics>
Now here is my XSLT file calendarGroup.xslt
现在这是我的 XSLT 文件 calendarGroup.xslt
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="html"
version="1.0"
encoding="UTF-8"
indent="yes"/>
<xsl:template match="groups/groupRef">
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Table</title>
<meta charset="utf-8" />
</head>
<body>
<table border="1">
<tr>
<th>Sunday</th>
<th>Monday</th>
<th>Tuesday</th>
<th>Wednesday</th>
<th>Thursday</th>
<th>Friday</th>
<th>Saturday</th>
</tr>
<xsl:for-each select="document(.)//topic">
<xsl:if test="string(name) != ''">
<tr>
<td>
<xsl:value-of select="sunday"/>
</td>
<td>
<xsl:value-of select="monday"/>
</td>
<td>
<xsl:value-of select="tuesday"/>
</td>
<td>
<xsl:value-of select="wednesday"/>
</td>
<td>
<xsl:value-of select="thursday"/>
</td>
<td>
<xsl:value-of select="friday"/>
</td>
<td>
<xsl:value-of select="saturday"/>
</td>
</tr>
</xsl:if>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template>
</xsl:stylesheet>
The benefit of setting up my code this way is I can easily create the xml from an excel file and not half to put in a declaration statement in the newly created file. You do need to have the format "xxGroup" for this to work together. You can add more groups to the clGroup file and they will parse in the XSLT allowing you to include more xml in the same XSLT parser. Make sure they are all in the same directory.
以这种方式设置我的代码的好处是我可以轻松地从 excel 文件创建 xml,而不是在新创建的文件中放入声明语句。您确实需要具有“xxGroup”格式才能使其协同工作。您可以向 clGroup 文件添加更多组,它们将在 XSLT 中解析,从而允许您在同一个 XSLT 解析器中包含更多 xml。确保它们都在同一目录中。
回答by Yzmir Ramirez
If simply embedding the file inside the XML file is not a solution then adding a url field that the interpreting program reads should be added- you're basically asking for the equivalent of an include(menu.xml) or require(xml), but in an XML file.
如果简单地将文件嵌入到 XML 文件中不是解决方案,那么添加解释程序读取的 url 字段应该添加 - 您基本上要求等效于 include(menu.xml) 或 require(xml),但是在一个 XML 文件中。
So since you are writing the program that is intepretting the file you can add an <externalMenuTagThatYouDecideToAddToTheBaseXmlFile>tag or whatever you want to call it that you will read and insert menu.xml's root note the place of the <externalMenuTagThatYouDecideToAddToTheBaseXmlFile>.
因此,由于您正在编写解释文件的程序,因此您可以添加一个<externalMenuTagThatYouDecideToAddToTheBaseXmlFile>标签或任何您想要调用的名称,您将读取并插入 menu.xml 的根注释 .xml 文件的位置<externalMenuTagThatYouDecideToAddToTheBaseXmlFile>。
Good luck and may your programs always compile.
祝你好运,愿你的程序总是编译通过。

