使用XSLT展平Amazon Response XML

时间:2016-10-22 04:34:28

标签: xml xslt ssis amazon-mws

我正在尝试使用XSLT来压缩或规范化我从亚马逊MWS获得的XML响应。我的计划是在以关系格式将数据导入SQL Server之前,使用SSIS中的XSLT预处理XML响应。

无论如何,我非常接近,但我遇到的问题似乎无法解决。当我使用XSLT转换XML时,我得到了值,但没有XML元素。我完成后需要一个有效的XML文件,所以这是一个问题......

这是我到目前为止所拥有的......

XML响应

            <ListFinancialEventsResponse xmlns="http://mws.amazonservices.com/Finances/2015-05-01">
            <ListFinancialEventsResult>
            <NextToken>2YgYW55IGNhcm5hbCBwbGVhcEXAMPLE</NextToken>
            <FinancialEvents>
            <ProductAdsPaymentEventList/>
            <RentalTransactionEventList/>
            <PayWithAmazonEventList/>
            <ServiceFeeEventList>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            </ServiceFeeEvent>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            <FeeList>
            <FeeComponent>
            <FeeType>FBACustomerReturnPerOrderFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-1.0</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            <FeeComponent>
            <FeeType>FBACustomerReturnPerUnitFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-1.46</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            <FeeComponent>
            <FeeType>FBACustomerReturnWeightBasedFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-0.96</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            </FeeList>
            </ServiceFeeEvent>
            </ServiceFeeEventList>
            </FinancialEvents>
            </ListFinancialEventsResult>
            <ResponseMetadata>
            <RequestId>612b2053-9e1f-469c-82a0-af9b19c671fa</RequestId>
            </ResponseMetadata>
            </ListFinancialEventsResponse>

XSL模板

            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >
            <xsl:output method="xml" indent="yes"/>
            <xsl:template match="/ListFinancialEventsResponse">
            <ListFinancialEventsResponse>
            <ListFinancialEventsResult>
            <NextToken></NextToken>
            <FinancialEvents>
            <ProductAdsPaymentEventList></ProductAdsPaymentEventList>
            <RentalTransactionEventList></RentalTransactionEventList>
            <PayWithAmazonEventList></PayWithAmazonEventList>
            <ServiceFeeEventList>
            <xsl:for-each select="ServiceFeeEventList/ServiceFeeEvent">
            <ServiceFeeEvent>
            <FeeDescription><xsl:value-of select="FeeDescription"/></FeeDescription>
            <SellerSKU><xsl:value-of select="SellerSKU"/></SellerSKU>
            <FeeType><xsl:value-of select="FeeList/FeeComponent/FeeType"/></FeeType>
            <CurrencyAmount><xsl:value-of select="FeeList/FeeComponent/FeeAmount/CurrencyAmount"/></CurrencyAmount>
            <CurrencyCode><xsl:value-of select="FeeList/FeeComponent/FeeAmount/CurrencyCode"/></CurrencyCode>
            </ServiceFeeEvent>
            </xsl:for-each>
            </ServiceFeeEventList>
            </FinancialEvents>
            </ListFinancialEventsResult>
            <ResponseMetadata>
            <RequestId></RequestId>
            </ResponseMetadata>
            </ListFinancialEventsResponse>
            </xsl:template>       
            </xsl:stylesheet>

输出

            <?xml version="1.0" encoding="utf-8"?>

            2YgYW55IGNhcm5hbCBwbGVhcEXAMPLE






            Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium
            L8399P_DCRL-2


            Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium
            L8399P_DCRL-2


            FBACustomerReturnPerOrderFee

            -1.0
            USD



            FBACustomerReturnPerUnitFee

            -1.46
            USD



            FBACustomerReturnWeightBasedFee

            -0.96
            USD








            612b2053-9e1f-469c-82a0-af9b19c671fa

提前非常感谢!

3 个答案:

答案 0 :(得分:0)

(1)source-xml的节点位于命名空间http://mws.amazonservices.com/Finances/2015-05-01中。您的xpath语句是在默认命名空间中选择的。所以有一个不匹配。您的所有模板都不会被执行,并且in xslt内置模板会生成您的&#34;字符串&#34;输出

(2)一种解决方案是将前缀映射到命名空间并为xpath语句添加前缀:

添加:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:f="http://mws.amazonservices.com/Finances/2015-05-01" exclude-result-prefixes="f">

所以你必须改为例如[你必须为所有人做到这一点!注意f:]:

... select="f:FeeList/f:FeeComponent/f:FeeType"...

(3)你的for-each错了。上下文节点是ListFinancialEventsResponse(处理模板的匹配节点)。所以你必须写:

<xsl:for-each select="f:ListFinancialEventsResult/f:FinancialEvents/f:ServiceFeeEventList/f:ServiceFeeEvent">

答案 1 :(得分:0)

确定。这是成品。效果很好。不得不通过一些嵌套等工作,但这里是:

XML响应:

            <ListFinancialEventsResponse xmlns="http://mws.amazonservices.com/Finances/2015-05-01">
            <ListFinancialEventsResult>
            <NextToken>2YgYW55IGNhcm5hbCBwbGVhcEXAMPLE</NextToken>
            <FinancialEvents>
            <ProductAdsPaymentEventList/>
            <RentalTransactionEventList/>
            <PayWithAmazonEventList/>
            <ServiceFeeEventList>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            </ServiceFeeEvent>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            <FeeList>
            <FeeComponent>
            <FeeType>FBACustomerReturnPerOrderFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-1.0</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            <FeeComponent>
            <FeeType>FBACustomerReturnPerUnitFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-1.46</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            <FeeComponent>
            <FeeType>FBACustomerReturnWeightBasedFee</FeeType>
            <FeeAmount>
            <CurrencyAmount>-0.96</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </FeeAmount>
            </FeeComponent>
            </FeeList>
            </ServiceFeeEvent>
            </ServiceFeeEventList>
            </FinancialEvents>
            </ListFinancialEventsResult>
            <ResponseMetadata>
            <RequestId>612b2053-9e1f-469c-82a0-af9b19c671fa</RequestId>
            </ResponseMetadata>
            </ListFinancialEventsResponse>

XSLT:

            <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:my="http://mws.amazonservices.com/Finances/2015-05-01">
            <xsl:output method="xml" indent="yes"/>
            <xsl:template match="/my:ListFinancialEventsResponse">
            <ListFinancialEventsResponse>
            <ListFinancialEventsResult>
            <NextToken><xsl:value-of select="my:ListFinancialEventsResult/my:NextToken"/></NextToken>
            <FinancialEvents>
            <ProductAdsPaymentEventList></ProductAdsPaymentEventList>
            <RentalTransactionEventList></RentalTransactionEventList>
            <PayWithAmazonEventList></PayWithAmazonEventList>
            <ServiceFeeEventList>
            <xsl:for-each select="my:ListFinancialEventsResult/my:FinancialEvents/my:ServiceFeeEventList/my:ServiceFeeEvent">
            <ServiceFeeEvent>
            <FeeDescription><xsl:value-of select="my:FeeDescription"/></FeeDescription>
            <SellerSKU><xsl:value-of select="my:SellerSKU"/></SellerSKU>
            <xsl:for-each select="my:FeeList/my:FeeComponent">
            <FeeType><xsl:value-of select="my:FeeType"/></FeeType>
            <CurrencyAmount><xsl:value-of select="my:FeeAmount/my:CurrencyAmount"/></CurrencyAmount>
            <CurrencyCode><xsl:value-of select="my:FeeAmount/my:CurrencyCode"/></CurrencyCode>
            </xsl:for-each>
            </ServiceFeeEvent>
            </xsl:for-each>
            </ServiceFeeEventList>
            </FinancialEvents>
            </ListFinancialEventsResult>
            <ResponseMetadata>
            <RequestId><xsl:value-of select="my:ResponseMetadata/my:RequestId"/></RequestId>
            </ResponseMetadata>
            </ListFinancialEventsResponse>
            </xsl:template>       
            </xsl:stylesheet>

输出:

            <?xml version="1.0" encoding="utf-8"?>
            <ListFinancialEventsResponse xmlns:my="http://mws.amazonservices.com/Finances/2015-05-01">
            <ListFinancialEventsResult>
            <NextToken>2YgYW55IGNhcm5hbCBwbGVhcEXAMPLE</NextToken>
            <FinancialEvents>
            <ProductAdsPaymentEventList/>
            <RentalTransactionEventList/>
            <PayWithAmazonEventList/>
            <ServiceFeeEventList>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            </ServiceFeeEvent>
            <ServiceFeeEvent>
            <FeeDescription>Love Tree Women's Linen Wide Leg Trouser Pant, Dark Coral, Medium</FeeDescription>
            <SellerSKU>L8399P_DCRL-2</SellerSKU>
            <FeeType>FBACustomerReturnPerOrderFee</FeeType>
            <CurrencyAmount>-1.0</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            <FeeType>FBACustomerReturnPerUnitFee</FeeType>
            <CurrencyAmount>-1.46</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            <FeeType>FBACustomerReturnWeightBasedFee</FeeType>
            <CurrencyAmount>-0.96</CurrencyAmount>
            <CurrencyCode>USD</CurrencyCode>
            </ServiceFeeEvent>
            </ServiceFeeEventList>
            </FinancialEvents>
            </ListFinancialEventsResult>
            <ResponseMetadata>
            <RequestId>612b2053-9e1f-469c-82a0-af9b19c671fa</RequestId>
            </ResponseMetadata>
            </ListFinancialEventsResponse>

我唯一真正需要处理的是:我在xmlns的末尾。我尝试使用exclude-result-prefixes =&#34; my&#34;,但它完全取消了。无论如何,希望这有助于其他人。感谢您的投入。

答案 2 :(得分:0)

AFAICT,预期结果只能生成:

XSLT 1.0

<xsl:stylesheet version="1.0" 
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:my="http://mws.amazonservices.com/Finances/2015-05-01"
exclude-result-prefixes="my">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:strip-space elements="*"/>

<!-- move all elements into no-namespace  -->
<xsl:template match="*">
    <xsl:element name="{local-name()}">
        <xsl:apply-templates/>
    </xsl:element>
</xsl:template>

<!-- remove these wrappers  -->
<xsl:template match="my:FeeList | my:FeeComponent | my:FeeAmount">
    <xsl:apply-templates/>
</xsl:template>

</xsl:stylesheet>