0

我已经阅读了我能找到的所有内容,但还没有找到让它工作的方法。我正在尝试根据节点或其父节点的值从特定节点查询总和。一些节点可能满足具有正确名称的基本要求,但它们的父节点可能不满足,所以我需要忽略这些。在 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)

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

4

1 回答 1

0

我对您的要求有点困惑,并且您的样本标签不匹配(<Source> </Bill>

这将选择您的样本中我认为符合您的条件的单个 ChargeData 元素

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

解释

  • Detail
    选择详细子元素
  • [ServiceType="electric"]
    仅选择具有值为“electric”的子 ServiceType 元素的 Detail 元素
  • [not(Charges/ChargeData/ChargeDescription[.="Leased Outdoor Lighting"])]
    排除提供了匹配排除字符串的后代 ChargeDescription 的 Detail 元素
  • /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
于 2013-03-14T15:49:54.433 回答