什么是获取另一个元素中元素的第1次,第2次和第3次的xpath语法

时间:2012-11-05 09:23:46

标签: xml xpath asp-classic jscript

我有以下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 - Price - Amount

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - Component(type = Principle) - Charge - Amount

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - Component(type = Shipping) - Charge - Amount

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - Component(type = PrincipalPromo) - Charge - Amount

  • NewOrderNotification - ProcessedOrder - ProcessedOrderItems - ProcessedOrderItem - ItemCharges - Component(type = ShippingPromo) - Charge - Amount

请注意根元素中的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;

3 个答案:

答案 0 :(得分: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"

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

答案 1 :(得分:0)

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

'/NewOrderNotification/ProcessedOrder/ProcessedOrderItems/ProcessedOrderItem/ItemCharges/Component[Type="Shipping"]/Charge/Amount/text()'

答案 2 :(得分:0)

我将快速而脏的xpath放在以下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>