使用嵌套的 For Each 和 If return 来自不同子节点的节点值

Using nested For Each and If to return a node value from a different child node

当 'CreditCard' 是其中一种付款方式时,我正在尝试提取邮政编码。通常有多种付款方式,即信用卡和现金;信用卡和礼品卡;等。信用卡不一定是第一笔付款,但绝不会出现超过一张信用卡被允许的情况。我想遍历所有支付类型,当支付类型值为 'CreditCard' 时,然后检索邮政编码。如果没有使用信用卡,则该值为空。

根据我的初步研究,我似乎需要一个嵌套的 For-Each,但不一定需要一个 IF。我找到了很多解释嵌套 for-each 语句的文章和显示简单 if 语句的文章。我没有找到任何明确的文章显示使用这些组合语句从不同节点级别进行检索。这是我对这一行的伪代码:

IF... purchases/purchase/payments/payment/type = 'CreditCard'
THEN value of... purchases/purchase/payments/payment/address/zipcode

我试过的各种版本在SSIS包中都没有失败但是没有检索到结果。

XML样本:

<?xml version="1.0" encoding="utf-8"?>
<exportbatch xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<purchases>
    <purchase>
        <posRef>987654321</posRef>
        <locationRef>1234</locationRef>
        <totalamount>99.9900</totalamount>
        <payments>
            <payment>
                <amount>50.0000</amount>
                <description>Gift Card x-8765</description>
                <type>GiftCard</type>
            </payment>
            <payment>
                <amount>19.5200</amount>
                <description>some credit card brand x-8765</description>
                <brand>some credit card brand</brand>
                <type>CreditCard</type>
                <address>
                    <zipcode>65432</zipcode>
                </address>
            </payment>
        </payments>
    </purchase>
</purchases>
</exportbatch>  

XSLT 示例:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
  <xsl:output method="xml" indent="yes"/>
  <xsl:template match="exportbatch">
    <purchases>
    <xsl:for-each select="purchases/purchase">
        <purchase>
            <posRef><xsl:value-of select="posRef"/></posRef>
            <LocationId><xsl:value-of select="locationRef"/></LocationId>
            <TotalAmount><xsl:value-of select="totalamount"/></TotalAmount>
            <xsl:for-each select="payments/payment/type">
                <xsl:if test="'CreditCard'">
                    <PaymentZipCode><xsl:value-of select="address/zipcode"/></PaymentZipCode>
                </xsl:if>
            </xsl:for-each>
        </purchase>
    </xsl:for-each>
    </purchases>
</xsl:template>
</xsl:stylesheet>

备用 XSLT 示例:

    <xsl:for-each select="payments/payment">
        <xsl:if test="@type='CreditCard'">
            <PaymentZipCode><xsl:value-of select="address/zipcode"/></PaymentZipCode>
        </xsl:if>
    </xsl:for-each>

我认为你可以做到:

<xsl:template match="exportbatch">
    <purchases>
        <xsl:for-each select="purchases/purchase">
            <purchase>
                <posRef><xsl:value-of select="posRef"/></posRef>
                <LocationId><xsl:value-of select="locationRef"/></LocationId>
                <TotalAmount><xsl:value-of select="totalamount"/></TotalAmount>
                <xsl:variable name="cc" select="payments/payment[type='CreditCard']" />
                <xsl:if test="$cc">
                    <PaymentZipCode><xsl:value-of select="$cc/address/zipcode"/></PaymentZipCode>
                </xsl:if>
            </purchase>
        </xsl:for-each>
    </purchases>
</xsl:template>

这将测试类型为 CreditCardpayment 是否存在 - 如果存在,它会从那里检索邮政编码。

或者,您可以这样做:

<xsl:template match="exportbatch">
    <purchases>
        <xsl:for-each select="purchases/purchase">
            <purchase>
                <posRef><xsl:value-of select="posRef"/></posRef>
                <LocationId><xsl:value-of select="locationRef"/></LocationId>
                <TotalAmount><xsl:value-of select="totalamount"/></TotalAmount>
                <xsl:if test="payments/payment[type='CreditCard']">
                    <PaymentZipCode><xsl:value-of select="payments/payment/address/zipcode"/></PaymentZipCode>
                </xsl:if>
            </purchase>
        </xsl:for-each>
    </purchases>
</xsl:template>

这会进行相同的测试,如果通过,它会检索它能找到的第一个邮政编码。