使用 xslt 合并和更新两个 xml 的节点值
Merge and update node values of two xmls using xslt
我是 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)。它应该是通用的。
如果需要任何进一步的信息,请告诉我。如果我的问题不清楚,也请告诉我。
试试这个:
<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>
我是 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)。它应该是通用的。
如果需要任何进一步的信息,请告诉我。如果我的问题不清楚,也请告诉我。
试试这个:
<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>