Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用xslt合并和更新两个XML的节点值_Xml_Xslt_Xslt 1.0_Osb - Fatal编程技术网

使用xslt合并和更新两个XML的节点值

使用xslt合并和更新两个XML的节点值,xml,xslt,xslt-1.0,osb,Xml,Xslt,Xslt 1.0,Osb,我是XSLT的新手。 但我相信使用XSLT可以实现以下要求:) 现在我需要将两个不同的XML合并到一个XML中,并且应该检查xslt是否能够检查节点名,比如如果input1/nodeName与input2/nodeName匹配,则需要从input2填充值 例如: Input1 xml: <Parent> <C1>123</C1> <C2>Incorrect data</C2> <C3>789</

我是XSLT的新手。 但我相信使用XSLT可以实现以下要求:) 现在我需要将两个不同的XML合并到一个XML中,并且应该检查xslt是否能够检查节点名,比如如果input1/nodeName与input2/nodeName匹配,则需要从input2填充值

例如:

Input1 xml:

<Parent>
    <C1>123</C1>
    <C2>Incorrect data</C2>
    <C3>789</C3>
</Parent>

123
错误数据
789
Input2 xml:

<NewParent>
  <C2>CorrectData</C2>
</NewParent>

校正数据
输出:应该是

<Parent>
    <C1>123</C1>
    <C2>CorrectData</C2>
    <C3>789</C3>
</Parent>

123
校正数据
789
我尝试在XSLT下面合并这两者,但没有找到解决方案

<xsl:stylesheet version="1.0"
    xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" indent="yes" />
    <xsl:param name="input1" select="input1.xml" />
    <xsl:param name="input2" select="input2.xml" />
    <xsl:template match="@* | node()">
        <xsl:copy>
            <xsl:apply-templates select="@* | node()" />
        </xsl:copy>
    </xsl:template>
    <xsl:template match="/">
        <xsl:copy>
            <xsl:for-each select="/*">
                <xsl:choose>
                    <xsl:when
                        test="$input1//node() = $input2//node()">
                        <xsl:value-of select="$input2/node()" />
                    </xsl:when>
                    <xsl:otherwise>
                        <xsl:apply-templates select="@* | node()" />
                    </xsl:otherwise>
                </xsl:choose>
            </xsl:for-each>
        </xsl:copy>
    </xsl:template>
</xsl:stylesheet>

样式表终止时出现节点集错误

注意:为了得到结果,我们不应该在XSLT代码中指定任何节点名(如c1、c2)。它应该是通用的

如果需要更多信息,请告诉我。如果我的问题不清楚,请告诉我。

试试这个:

<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" indent="yes" />
<xsl:variable name="input1" select="document('input1.xml')" />
<xsl:variable name="input2" select="document('input2.xml')" />
<xsl:template match="@* | node()">
    <xsl:copy>
        <xsl:apply-templates select="@* | node()" />
    </xsl:copy>
</xsl:template>
<xsl:template match="/">
    <xsl:copy>
        <xsl:for-each select="$input1/*">
            <xsl:copy>
                <xsl:for-each select="*">
                    <xsl:choose>
                        <xsl:when test="name() = $input2//name()">
                            <xsl:copy-of select="$input2/*/node()" />
                        </xsl:when>
                        <xsl:otherwise>
                            <xsl:copy-of select="." />
                        </xsl:otherwise>
                    </xsl:choose>
                </xsl:for-each>
            </xsl:copy>
        </xsl:for-each>
    </xsl:copy>
</xsl:template>
</xsl:stylesheet>

输出:

<Parent>
 <C1>123</C1>
 <C2>CorrectData</C2>
 <C3>789</C3>
</Parent>

123
校正数据
789

关于接下来的问题,请查看格式帮助:。在实现自己的合并算法之前,请尝试Oliver Becker的合并算法:。Rudramuni-感谢您的回复。我尝试了您建议的更新,但如果我应用上述xslt,它不会更新有效负载。输出iam获取的是相同的input1 xml(作为输出)。Uday,检查XSLT中给出的文件名和外部文件名。嗨,Rudramuni,在我说“我尝试了你说的…”之前,我们是否可以执行XSLT代码而不给出输入xml在XSLT代码中的物理位置?实际上,我必须动态地传递输入xml(而不是从特定的物理位置获取)。所以xslt to应该能够分别接收param1和param2中的输入(xmls)(是否应该是这样的?)如果需要更多信息,请告诉我。。。我尝试了很多方法,但没有找到运气。在这个…@UdayShankar中,我还尝试了伪输入XML,XSLT正在从位于XSLT脚本路径中的另外两个XML获取信息。请发布一个新的问题与此要求,所以许多专家在我们的Stackoverflow网站,肯定他们会建议你正确的一个。这个XSLT对你有用吗?.Rudramuni,是的,这个对我有用。但是通过一些代码修改,实际上我想在OSB中实现这段代码。因此,我必须动态地传递输入xml(而不是通过xml文件的物理位置传递输入)。所以我必须介绍java,它将把我的一个xml转换成DOM对象。因此,这个xslt需要一些代码修改。基本上是说,我从1开始为每种类型提取了部分XSLT代码。非常感谢。:)