如何比较XSLT中两个节点的值

如何比较XSLT中两个节点的值,xslt,xslt-2.0,Xslt,Xslt 2.0,我是XSLT新手。我需要比较XML中两个节点的值的帮助 我的示例XML: <?xml version="1.0" encoding="utf-8"?> <G1Export xmlns=""> <AgencyGroup xmlns=""> <Agency xmlns=""> <RecordType xmlns="">RecordType</RecordType>

我是XSLT新手。我需要比较XML中两个节点的值的帮助

我的示例XML:

<?xml version="1.0" encoding="utf-8"?>
<G1Export xmlns="">
    <AgencyGroup xmlns="">
        <Agency xmlns="">
            <RecordType xmlns="">RecordType</RecordType>
            <OrgId xmlns="">123</OrgId>
        </Agency>
    </AgencyGroup>
    <BranchGroup xmlns="">
        <BranchCode xmlns="">
            <OrgId xmlns="">123</OrgId>
        </BranchCode>
    </BranchGroup>
</G1Export>

记录类型
123
123
在上面的XML文件中,我需要将
节点下的
OrgId
节点的值与
节点下的值进行比较

我尝试使用
compare()
方法,但它给了我1的值。 实际结果必须为0(表示相等)。我正在使用XSLT2

//G1Export/compare(AgencyGroup//OrgId, BranchGroup//OrgId)
结果=0

编辑: xslt中有两个错误 1.对于brnchOrgId,您使用的是AgencyGroup而不是BranchGroup 2.对于compare(),应该是=0,而不是=0

已更正的xslt:

<xsl:template match="/">
        <xsl:element name="PICRESPONSE" namespace="fieldpoint.com/namespaces">
            <xsl:for-each select="//G1Export/AgencyGroup">
                <xsl:if test="count(.) &gt; 0">
                    <!--org_id variable-->
                    <xsl:variable name="orgId" select="string(/G1Export/AgencyGroup/Agency/OrgId)"/>
                    <xsl:element name="EXPORTRESPONSE" namespace="fieldpoint.com/namespaces">; <xsl:for-each select="//G1Export/BranchGroup">
                            <xsl:if test="count(.) &gt; 0">
                                <xsl:variable name="brnchOrgId" select="string(/G1Export/BranchGroup/BranchCode/OrgId)"/>                               
                                <!--Put the Branch information inside the current agency node only if branch belong to current Agency-->
                                <xsl:if test="compare($brnchOrgId,$orgId)=0">asda
                                    <xsl:value-of select="'orgid is same as branchogid'"/>
                                </xsl:if>
                            </xsl:if>
                        </xsl:for-each>
                    </xsl:element>
                </xsl:if>
            </xsl:for-each>
        </xsl:element>
    </xsl:template>

; 
阿斯达
输出:

<?xml version="1.0" encoding="UTF-8"?>
<PICRESPONSE xmlns="fieldpoint.com/namespaces">
    <EXPORTRESPONSE>; orgid is same as branchogid</EXPORTRESPONSE>
</PICRESPONSE>

; orgid与branchogid相同

希望这有帮助。

为什么不做
AgencyGroup/Agency/OrgId=BranchGroup/BranchCode/OrgId
? 对于额外的anal,
AgencyGroup/Agency/OrgId/text()=BranchGroup/BranchCode/OrgId/text()


如果你需要区别,考虑<代码> AgEngyGy/Actudi-BrutkGrave/BrangCult/OrdI/<代码>

< P>我不知道需要比较这些值的上下文,但是<代码>=运算符正是你正在寻找的。这将比较它们,但可能不是您需要的上下文:

<xsl:if 
  test="/G1Export/AgencyGroup/Agency/OrgId = /G1Export/BranchGroup/BranchCode/OrgId">


@Andrew Hare:哦,不!编辑重击器。老实说,我觉得我的编辑比你的好。你错过了很多。因为你显然还没有取得任何成功,我认为你应该在这个问题上添加你尝试的代码。然后人们可以帮你调试它。嗨,Rashmi,你的意思是说我应该使用comapare方法和节点的Xpath。compare()方法是否仅比较节点的值加上这些节点在xml文件中的位置?它使用默认排序规则比较两个字符串(在本例中仅比较值)。有关更多信息,请参阅此链接:我尝试匹配:这是false。在我的xml文件中,Agency/OrgId节点的值是123,BranchCode/OrgId也是123。这是因为您的XPath不正确。从单个位置不能使用“代理机构/组织ID”和“分支机构代码/组织ID”。尝试使用“AgencyGroup/Agency/OrgId”和“AgencyGroup/BranchCode/OrgId”。这取决于您当前的位置。如果您发布xslt示例或至少让我知道您在哪个模板中声明这些变量,这将有所帮助。在我的xml文件中,Agency/OrgId节点的值是123,BranchCode/OrgId也是123。它们都返回相同的值。输出:123输出:123根据我的示例XML文件。我尝试匹配:这是false。在我的xml文件中,Agency/OrgId节点的值为123,BranchCode/OrgId也为123。Agency/OrgId和BranchCode/OrgId在任何上下文中都不会返回实际值,因为在该级别上它们没有共同的父节点。使用我的示例中的完整路径。告诉我它是否有效。我试过这个,但仍然返回错误。