0

我有以下 xml 文档(我已经清除了不相关的数据):

<?xml version="1.0"?>
<NewOrderNotification xmlns="http://payments.amazon.com/checkout/2008-11-30/">
  <ProcessedOrder>
    <ShippingServiceLevel>blah</ShippingServiceLevel>
    <ProcessedOrderItems>
      <ProcessedOrderItem>
        <AmazonOrderItemCode>blah</AmazonOrderItemCode>
        <Price>
          <Amount>0.2</Amount>
          <CurrencyCode>GBP</CurrencyCode>
        </Price>
        <ItemCharges>
          <Component>
            <Type>Principal</Type>
            <Charge>
              <Amount>0.2</Amount>
              <CurrencyCode>GBP</CurrencyCode>
            </Charge>
          </Component>
          <Component>
            <Type>Shipping</Type>
            <Charge>
              <Amount>0.95</Amount>
              <CurrencyCode>GBP</CurrencyCode>
            </Charge>
          </Component>
          <Component>
            <Type>PrincipalPromo</Type>
            <Charge>
              <Amount>0.0</Amount>
              <CurrencyCode>GBP</CurrencyCode>
            </Charge>
          </Component>
          <Component>
            <Type>ShippingPromo</Type>
            <Charge>
              <Amount>0.0</Amount>
              <CurrencyCode>GBP</CurrencyCode>
            </Charge>
          </Component>
        </ItemCharges>
        <ShippingCustomData>null</ShippingCustomData>
      </ProcessedOrderItem>
    </ProcessedOrderItems>
  </ProcessedOrder>
</NewOrderNotification>

我想知道用于获取 5 个不同<Amount>元素中的每个值的 xpath 语法:

即我如何提取以下各项:

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - 价格 - 金额

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - 组件(类型=原则) - 费用 - 金额

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - 组件(类型=Shipping) - 费用 - 金额

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - 组件(类型=PrincipalPromo) - 费用 - 金额

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - 组件(类型=ShippingPromo) - 费用 - 金额

请注意根元素中的 xmlns。

这是我用来提取元素的 JScript 代码:

var xmlDoc = Server.CreateObject("Msxml2.DOMDocument.6.0");
xmlDoc.setProperty("SelectionNamespaces", "xmlns:a='http://payments.amazon.com/checkout/2008-11-30/'");
xmlDoc.loadXML(xml);
var node = xmlDoc.documentElement.selectSingleNode("XPATH_IN_HERE");
return node.text;
4

3 回答 3

1

您可以获得的第一笔金额:

//Price/Amount/text()
#=> "0.2"

对于其他四个,您可以使用前面的兄弟Type元素的文本来获取它们的值:

//Charge[preceding-sibling::Type="Principal"]/Amount/text()
#=> "0.2"

//Charge[preceding-sibling::Type="Shipping"]/Amount/text()
#=> "0.95"

//Charge[preceding-sibling::Type="PrincipalPromo"]/Amount/text()
#=> "0.0"

//Charge[preceding-sibling::Type="ShippingPromo"]/Amount/text()
#=> "0.0"

我认为这样做。(如果我误解了你的问题,请告诉我。)

于 2012-11-05T09:33:05.033 回答
0

或者,如果您喜欢缩写的绝对路径

'/NewOrderNotification/ProcessedOrder/ProcessedOrderItems/ProcessedOrderItem/ItemCharges/Component[Type="Shipping"]/Charge/Amount/text()'
于 2012-11-05T09:43:00.783 回答
0

我将我快速而肮脏的 xpaths 放在下面的 xslt 脚本中。

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" 
    xmlns:test="http://payments.amazon.com/checkout/2008-11-30/">
    <xsl:output indent="yes"/>

    <!-- Standard if you can define namespaces -->
    <xsl:variable name="amount1" select="//test:Price/test:Amount"/>
    <!-- for cases with namespace problems -->
    <xsl:variable name="amount2" select="//*[local-name() = 'Component'][1]/*[local-name() = 'Charge']/*[local-name() = 'Amount']"/>
    <!-- you can add the other in the same way -->


    <xsl:template match="/">
        <test>
        <variable1><xsl:value-of select="$amount1"/></variable1>    
        <variable2><xsl:value-of select="$amount2"/></variable2>
        </test> 
    </xsl:template>
</xsl:stylesheet>
于 2012-11-05T09:44:14.953 回答