使用XSLT1.0将外部xml注入主xml

使用XSLT1.0将外部xml注入主xml,xml,external,xslt-1.0,document,code-injection,Xml,External,Xslt 1.0,Document,Code Injection,我有我的xsl来转换xml文件1。 我将xml_文件_2包含在xsl的输出中 例如,输出文件的结构为: <A> <B> <!-- inject external xml here --> <C/> </B> </A> 如何做到这一点 我试图使用以下收据: <xsl:template match="/"> <xsl:copy-of select="do

我有我的xsl来转换xml文件1。 我将xml_文件_2包含在xsl的输出中

例如,输出文件的结构为:

<A>
    <B>
        <!-- inject external xml here -->
        <C/>
    </B>
</A>

如何做到这一点

我试图使用以下收据:

<xsl:template match="/">
  <xsl:copy-of select="document('external.xml')/*"/>
</xsl:template> 

但它只是用外部文件的内容替换输出文件。 我尝试了上述模板的不同变体,如将match=“/”指向需要插入的节点(match=“/A/B”),但没有结果


另外,在xsl中使用前,我将使用sed从外部文件中删除第一行。

我认为注入点应该由以下元素标记:

<?xml version="1.0" encoding="UTF-8"?>
<A>
    <B>
        <InjectionPoint />
        <C/>
    </B>
</A>

可以使用

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>

    <!-- Identity transform-->
    <xsl:template match="node() | @*">
        <xsl:copy>
            <xsl:apply-templates select="node() | @*" />
        </xsl:copy>
    </xsl:template>

    <!-- Replacing inject point element-->
    <xsl:template match="InjectionPoint">
        <xsl:copy-of select="document('external.xml')" />
    </xsl:template>

</xsl:stylesheet>

结果是

<?xml version="1.0" encoding="UTF-8"?>
<A>
    <B>
        <ExternaFile>
            <Content1/>
            <Content2/>
        </ExternaFile>
        <C/>
    </B>
</A>

顺便说一句,您不需要通过sed剥离xml序言

编辑:我想你不想使用特殊的元素来标记正确的位置,你可以使用模板

<!--or without "inject" element -->
<xsl:template match="B[parent::A]">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:copy-of select="document('external.xml')" />
        <xsl:apply-templates select="node()" />
    </xsl:copy>
</xsl:template>

但可能会出现一些问题(例如,当一个元素中有更多的B元素时,当有多个A元素包含B元素时,等等)

<!--or without "inject" element -->
<xsl:template match="B[parent::A]">
    <xsl:copy>
        <xsl:apply-templates select="@*" />
        <xsl:copy-of select="document('external.xml')" />
        <xsl:apply-templates select="node()" />
    </xsl:copy>
</xsl:template>