[英]Merging two xml data and using the output in same XSLT
我有两个XML变量,下面是示例数据:
$ Variable1:
<Group>
<A>Test</A>
<B>Test1</B>
.....
.....
.....
</Group>
$ Variable2:
<Data>
<ABC>Test</ABC>
<XYZ>Test1</XYZ>
.....
.....
.....
</Data>
现在,我想在XSLT中合并这两个变量,并在同一XSLT中使用输出,因此合并后的输出如下所示:
<Group>
<A>Test</A>
<B>Test1</B>
.....
.....
.....
<ABC>Test</ABC>
<XYZ>Test1</XYZ>
.....
.....
.....
</Group>
以上输出将在同一XSLT中传递以进行进一步处理。
以下是我尝试过的xslt示例:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes"/>
<xsl:param name="var1" select="document($Variable1)" />
<xsl:param name="var2" select="document($Variable2)" />
//Here I want to merge above to inputs and later will be used in XSLT below
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@* | node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="Group">
-------
-------
-------
-------
</xsl:template>
</xsl:stylesheet>
如果采用以下样式表:
XSLT 1.0
<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"/>
<xsl:strip-space elements="*"/>
<xsl:param name="file1"/>
<xsl:param name="file2"/>
<xsl:template match="/">
<xsl:apply-templates select="document($file1)/*"/>
</xsl:template>
<xsl:template match="/*">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
<xsl:apply-templates select="document($file2)/*/*"/>
</xsl:copy>
</xsl:template>
<!-- identity transform -->
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
并将其传递到两个文件的路径 (作为字符串),将返回以下结果 :
<?xml version="1.0" encoding="UTF-8"?>
<Group>
<A>Test</A>
<B>Test1</B>
<ABC>Test</ABC>
<XYZ>Test1</XYZ>
</Group>
当然,您还需要在其上运行转换的第三个(虚拟)XML文件。 一个更聪明的实现将使用第一个输入文件作为源XML,而仅传递第二个文件的路径作为参数。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.