如何使用xslt比较相同输入xml数据中的两个字段?
谁能帮我解决这个问题。 事实上,当你们和“ID”比较时,若“ID”等于,它返回“bb”或者我们在输入xml中有什么输入。如果它不相等,一些东西将返回。我将在截图中提供所有输入和xslt编码 谢谢你提前通知 Myxslt:如何使用xslt比较相同输入xml数据中的两个字段?,xslt,Xslt,谁能帮我解决这个问题。 事实上,当你们和“ID”比较时,若“ID”等于,它返回“bb”或者我们在输入xml中有什么输入。如果它不相等,一些东西将返回。我将在截图中提供所有输入和xslt编码 谢谢你提前通知 Myxslt: 所需输出: bb您正在为每个构造定义Result1和Result2变量。这意味着它们在该块的范围内是局部的,在该块之外是不可访问的。此外,当您确实需要第二个Emp元素下的Id元素时,使用Id[2]查找当前Emp元素下的第二个Id元素 因此,用以下内容替换xsl:for
所需输出:
bb
您正在为每个构造定义Result1
和Result2
变量。这意味着它们在该块的范围内是局部的,在该块之外是不可访问的。此外,当您确实需要第二个Emp
元素下的Id
元素时,使用Id[2]
查找当前Emp
元素下的第二个Id
元素
因此,用以下内容替换xsl:for each
<xsl:variable name="Result1" select="Emps/Emp[1]/Id" />
<xsl:variable name="Result2" select="Emps/Emp[2]/Id" />
试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="Result1" select="Emps/Emp[1]/Id" />
<xsl:variable name="Result2" select="Emps/Emp[2]/Id" />
<xsl:choose>
<xsl:when test="$Result1 = $Result2">
<xsl:value-of select="$Result1/following-sibling::Name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Result2/following-sibling::Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
注意,这里不需要变量。也试试这个XSLT
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:choose>
<xsl:when test="Emps/Emp[1]/Id = Emps/Emp[2]/Id">
<xsl:value-of select="Emps/Emp[1]/Name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Emps/Emp[2]/Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
当然,这两种方法都假设XML中只有两个
Emp
元素。我不禁想,也许您正试图从XML中消除重复ID。如果是这样的话,你应该仔细阅读一种叫做的技术(如果你使用的是XSLT 1.0,也就是说,如果你使用的是XSLT 2.0,你可以对每个组使用xsl:foreach group
)。这里我的输入:1 aa 1 bb你能编辑你的问题以包含输入吗?谢谢。感谢您的快速响应。@。我当然会尝试这个xslt。再次感谢您的帮助,它工作正常。
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:variable name="Result1" select="Emps/Emp[1]/Id" />
<xsl:variable name="Result2" select="Emps/Emp[2]/Id" />
<xsl:choose>
<xsl:when test="$Result1 = $Result2">
<xsl:value-of select="$Result1/following-sibling::Name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$Result2/following-sibling::Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:choose>
<xsl:when test="Emps/Emp[1]/Id = Emps/Emp[2]/Id">
<xsl:value-of select="Emps/Emp[1]/Name"/>
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="Emps/Emp[2]/Name"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>