【问题标题】:Merge and update node values of two xmls using xslt使用 xslt 合并和更新两个 xml 的节点值
【发布时间】:2015-02-05 00:18:10
【问题描述】:

我是 XSLT 的新手。 但我相信使用 XSLT 可以实现以下要求 :) 现在我有一个要求,我需要将 2 个不同的 xml 合并为一个,并且应该检查 xslt 应该能够检查节点名称的位置,例如 input1/nodeName 是否与 input2/nodeName 匹配,然后需要从 input2 填充值。

例如:

输入1 xml:

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

输入 2 xml:

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

输出:应该是

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

我尝试在 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)。它应该是通用的。

如果需要任何进一步的信息,请告诉我。如果我的问题不清楚,也请告诉我。

【问题讨论】:

标签: xml xslt xslt-1.0 osb


【解决方案1】:

试试这个:

<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>

【讨论】:

  • Rudramuni - 感谢您的回复。我尝试了您建议的更新,但它没有更新有效负载,如果我应用上述 xslt。输出我得到的是相同的 input1 xml(作为输出)。
  • Uday,检查 XSLT 中给出的文件名是否与外部文件名有关。
  • 嗨 Rudramuni,在我说“我尝试了你所说的...”之前,我们是否可以在不提供输入 xml 在 xslt 代码中的物理位置的情况下执行 xslt 代码?实际上我必须动态地传递输入xml(不是从特定的物理位置获取)。所以 xslt 应该能够分别接收 param1 和 param2 中的输入(xmls)(它应该是这样的吗?)如果您需要更多信息,请告诉我......我尝试了很多方法,但没有找到运气.帮我解决这个...
  • @UdayShankar,我也尝试使用虚拟输入 XML,XSLT 正在从位于 XSLT 脚本路径中的其他两个 XML 中获取信息。请发布一个具有此要求的新问题,我们的 Stackoverflow 网站上有很多专家,相信他们会为您推荐正确的。这个 XSLT 对你有用吗?。
  • Rudramuni,是的,这对我有用。但通过一些代码修改,实际上我想在 OSB 中实现此代码。所以我必须动态地传递输入 xmls(而不是传递带有 xml 文件的物理位置的输入)。所以我必须介绍 java - 它将我的一个 xml 转换为 DOM 对象。所以这个 xslt 需要一些代码修改。基本上是说我从第一个 for-each 中获取了 XSLT 代码的一部分。万分感谢。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-04-11
相关资源
最近更新 更多