2

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

在上面的 XML 文件中,我需要将OrgId节点下的<AgencyGroup>节点值与节点下的值进行比较<BranchGroup>

我尝试使用该compare()方法,但结果为 1。实际结果必须为 0(等于)。我正在使用 XSLT 2。

4

3 回答 3

3

我不知道您需要比较这些值的上下文,但=操作符就是您要查找的内容。这将比较它们,但可能不是您需要的上下文:

<xsl:if 
  test="/G1Export/AgencyGroup/Agency/OrgId = /G1Export/BranchGroup/BranchCode/OrgId">
于 2009-05-14T12:00:36.920 回答
1

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

如果您需要差异,请考虑AgencyGroup/Agency/OrgId - BranchGroup/BranchCode/OrgId

于 2009-05-14T11:59:13.413 回答
0
//G1Export/compare(AgencyGroup//OrgId, BranchGroup//OrgId)

结果 = 0

已编辑:xslt 1 中有 2 个错误。对于 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>

希望这可以帮助。

于 2009-05-14T11:59:08.890 回答