我认为注射点应像一些元素标记:
<?xml version="1.0" encoding="UTF-8"?>
<A>
<B>
<InjectionPoint />
<C/>
</B>
</A>
让external.xml是
<?xml version="1.0" encoding="UTF-8"?>
<ExternaFile>
<Content1 />
<Content2 />
</ExternaFile>
然后稍微修改identity transform可以用来
<?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元素等)。