XML: How to load the contents of one xml file into another

你说的曾经没有我的故事 提交于 2019-12-06 11:54:08

Use an external (parsed) general entity to reference b.xml from a.xml.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE node [
<!ENTITY b SYSTEM "b.xml">
]>
<node>
    &b;
</node>

The XML parser will dynamically include the content of b.xml as it parsees a.xml and will produce the XML that you want.

If you load a.xml in IE, it will render correctly.

Note: Some browsers have very strict security policies that cause problems loading referenced XML files from the filesystem and expanding entity references, so it may not work in all browsers if you load a.xml from the filesystem, but may work in more browsers if you load from a URL.

When this XML document is opened in a browser:

<?xml-stylesheet type="text/xsl" href="stylesheet.xsl"?>
<node>
 -Include Contents of b.xml
</node>

With this XSLT stylesheet (other XML document) refered with stylesheet.xsl relative URI:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:template match="node()|@*">
        <xsl:copy>
            <xsl:apply-templates select="node()|@*"/>
        </xsl:copy>
    </xsl:template>
    <xsl:template match="node">
        <xsl:copy>
            <xsl:apply-templates select="@*"/>
            <xsl:copy-of select="document('B.xml')"/>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

It's rendered (without any style, or with browser default XML stylesheet) as:

<node>
    <anode>a</anode>
</node>

Note: The processing instruction. I have used xsl:copy-of instruction because I didn't want to confuse you with posible infinite recursion...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!