XPath,基于相关节点的过滤器

时间:2013-03-14 15:28:02

标签: xpath

我已经阅读了我能找到的所有内容,并且没有找到办法让它发挥作用。我正在尝试根据节点或其父节点的值查询特定节点的总和。一些节点可能满足具有正确名称的基本要求,但其父节点可能不是,因此我需要忽略它们。在SQL术语中,我将使用EXISTS或LIKE子句来处理此问题。

我想做的是

   SUM    ServiceAmounts 
   FROM   ChargeData elements 
   WHERE  ChargeDescription = 'subTotal-DistrubutionCharges' 
          AND Detail.ServiceType = 'electric' 
          AND NOT 
          (
              ChargeData has a sibling with ChargeDescription = 'Leased Outdoor Lighting' - the entire detail node should be ignored that contains the ChargeData element with 'Leased Outdoor Lighting'. 
          ) 

如果有价值,我的XSL版本是1.0。

源XML:

    <Source>
      <Detail>
        <ServiceType>electric</ServiceType>
        <Charges>
           <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>19.8</ServiceAmount>
            <ChargeDescription>7% Sales Tax</ChargeDescription>
            <ChargeID>sales_tax</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>282.8</ServiceAmount>
            <ChargeDescription>E-2 Demand</ChargeDescription>
            <ChargeID>usage_charge</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>165.91</ServiceAmount>
            <ChargeDescription>7% Sales Tax</ChargeDescription>
            <ChargeID>sales_tax</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>2370.15</ServiceAmount>
            <ChargeDescription>E-2 Commercial</ChargeDescription>
            <ChargeID>usage_charge</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>2838.66</ServiceAmount>
            <Quantity>0</Quantity>
            <ChargeDescription>subTotal-DistributionCharges</ChargeDescription>
          </ChargeData>
        </Charges>
      </Detail>
      <Detail>
        <ServiceType>electric</ServiceType>
        <Charges>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>2.57</ServiceAmount>
            <ChargeDescription>7% Sales Tax</ChargeDescription>
            <ChargeID>sales_tax</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>36.8</ServiceAmount>
            <ChargeDescription>Leased Outdoor Lighting</ChargeDescription>
            <ChargeID>usage_charge</ChargeID>
          </ChargeData>
          <ChargeData>
            <AllowanceChargeInd>C</AllowanceChargeInd>
            <ServiceAmount>39.37</ServiceAmount>
            <Quantity>0</Quantity>
            <ChargeDescription>subTotal-DistributionCharges</ChargeDescription>
          </ChargeData>
        </Charges>
      </Detail>
    </Source>

我的xsl,好吧,我已经尝试了多次,并且一直处于死胡同状态:

    sum(//ChargeData[not(contains(ChargeDescription,'Leased Outdoor Lighting')) 
    and not(contains(Detail/Charges/ChargeData/ChargeDescription, 'subTotal'))  
    and ancestor::Detail/ServiceType='electric']/ServiceAmount) 

    sum(//ChargeData[contains(ChargeDescription, 'subTotal-DistributionCharges') 
    and ancestor::Detail/ServiceType='electric']/ServiceAmount)
    -
    sum(//ChargeData[contains(ChargeDescription, 'Leased Outdoor Lighting') 
    and ancestor::Detail/ServiceType='electric']/ServiceAmount)

也许这是不可能的。我不能做的一件事是改变架构/数据结构。

1 个答案:

答案 0 :(得分:0)

我对您的要求感到有些困惑,您的样本标签不匹配(<Source> </Bill>

这会选择样本中我认为符合您标准的单个ChargeData元素

Detail
 [ServiceType="electric"]
 [not(Charges/ChargeData/ChargeDescription[.="Leased Outdoor Lighting"])]
 /Charges/ChargeData[ChargeDescription="subTotal-DistributionCharges"]

<强>解释

  • Detail
    选择明细子元素
  • [ServiceType="electric"]
    仅选择具有子ServiceType元素的Detail元素,其值为“electric”
  • [not(Charges/ChargeData/ChargeDescription[.="Leased Outdoor Lighting"])]
    排除有一个后代ChargeDescription匹配排除字符串的详细信息元素
  • /Charges/ChargeData[ChargeDescription="subTotal-DistributionCharges"]
    在过滤的Detail元素下方,选择ChargeData,其中有一个子提供的ChargeDescription匹配字符串

所以

sum(
 Detail
 [ServiceType="electric"]
 [not(Charges/ChargeData/ChargeDescription[.="Leased Outdoor Lighting"])]
 /Charges/ChargeData[ChargeDescription="subTotal-DistributionCharges"]/ServiceAmount
)

产量

2838.6599999999999