合并后使用XSLT连接XML? file1.xml file2.xml output.xml
这是这里的一个后续问题: 我在file1.XML中的每个XML文件action标记和file2.XML中的parent标记中都有2个不同的标记,我需要它们在遍历公共标记后显示在输出文件中合并后使用XSLT连接XML? file1.xml file2.xml output.xml,xml,xslt,merge,concatenation,Xml,Xslt,Merge,Concatenation,这是这里的一个后续问题: 我在file1.XML中的每个XML文件action标记和file2.XML中的parent标记中都有2个不同的标记,我需要它们在遍历公共标记后显示在输出文件中 请帮助我编写XSLT,以确保这两个标记都反映在输出中。查看JLRishie的精确答案,给出了以下复制匹配根元素的模板: <xsl:template match="root"> <xsl:copy> <xsl:apply-templates select="@* | n
请帮助我编写XSLT,以确保这两个标记都反映在输出中。查看JLRishie的精确答案,给出了以下复制匹配根元素的模板:
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:copy-of
select="document('file2.xml')
/config/state[@version = current()/../@version]
/root[@value = current()/@value and
@group = current()/@group]/*" />
</xsl:copy>
</xsl:template>
要扩展此功能以从file2.xml获取非根元素,可以添加新模板以匹配file1.xml中的state元素,然后从file2.xml复制非根元素
<xsl:template match="state">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:copy-of
select="document('file2.xml')
/config/state[@version = current()/@version]/*[not(self::root)]" />
</xsl:copy>
</xsl:template>
这是完整的XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="state">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="document('file2.xml')
/config/state[@version = current()/@version]
/*[not(self::root)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="document('file2.xml')
/config/state[@version = current()/../@version]
/root[@value = current()/@value and
@group = current()/@group]/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
当应用于两个XML文件时,将输出以下内容
<config>
<state version="10">
<root value="100" group="5">
<leaf number="2"/>
<leaf number="6"/>
</root>
<action value="2" step="4">
<get score="5"/>
</action>
<parent>
<child node="yes"/>
</parent>
</state>
</config>
<xsl:template match="state">
<xsl:copy>
<xsl:apply-templates select="@* | node()" />
<xsl:copy-of
select="document('file2.xml')
/config/state[@version = current()/@version]/*[not(self::root)]" />
</xsl:copy>
</xsl:template>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output indent="yes" omit-xml-declaration="yes"/>
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="state">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="document('file2.xml')
/config/state[@version = current()/@version]
/*[not(self::root)]"/>
</xsl:copy>
</xsl:template>
<xsl:template match="root">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
<xsl:copy-of select="document('file2.xml')
/config/state[@version = current()/../@version]
/root[@value = current()/@value and
@group = current()/@group]/*"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
<config>
<state version="10">
<root value="100" group="5">
<leaf number="2"/>
<leaf number="6"/>
</root>
<action value="2" step="4">
<get score="5"/>
</action>
<parent>
<child node="yes"/>
</parent>
</state>
</config>