合并两个XML数据,并使用相同的XSLT输出
我有一个有两个XML变量,下面是样本数据是:合并两个XML数据,并使用相同的XSLT输出
$变量1:
<Group>
<A>Test</A>
<B>Test1</B>
.....
.....
.....
</Group>
$变量2:
<Data>
<ABC>Test</ABC>
<XYZ>Test1</XYZ>
.....
.....
.....
</Data>
现在我想在XSLT中合并这两个变量,并在同一个XSLT中使用输出,所以输出将是s合并后如下所示:
<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,并且只传递第二个文件的路径作为参数。
抱歉,延迟响应..
@ManojSingh没有像“XML字符串”这样的东西。一个字符串是一个字符串 - 并且使用XSLT处理字符串的功能不多。看到这个最近的问题:http://*.com/questions/27018244/apply-transforms-to-xml-attribute-containing-escaped-html/27019850#27019850 – 2014-11-20 08:37:13
是的,我知道没有这样的事情像XML字符串,我只是告诉file1和file2将有一个字符串值,它将在运行时生成。 – 2014-11-20 09:51:04
你的变量'Variable1'和'Variable2'从哪里来?它们应该在样式表中定义。 – 2014-10-28 17:46:02
它来自C#代码 – 2014-10-28 17:58:54
“*它来自C#代码*”这是不可能的。变量/参数必须在样式表本身中定义。您只能将**值**传递给预定义的参数。 – 2014-10-29 05:36:14