2013-03-14 57 views
0

我已閱讀過有關我能找到的所有內容,但尚未找到使其正常工作的方法。我試圖根據節點或其父節點的值從特定節點查詢總和。有些節點可能符合具有正確名稱的基本要求,但其父母可能不會,所以我需要忽略這些。在SQL術語中,我會用EXISTS或LIKE子句來解決這個問題。XPath,基於相關節點的過濾器

我試圖做的是

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) 

也許這ISN」可能。我不能做的一件事是改變模式/數據結構。

回答

0

我對你的要求有點糊塗了,你的樣品有不匹配的標籤(<Source></Bill>

此選擇你的樣品中的單一元素ChargeData,我認爲您的條件匹配

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

說明

  • Detail
    個 選擇詳細的子元素
  • [ServiceType="electric"]
    僅選擇的詳細元素與子服務類型元素與「電」
  • [not(Charges/ChargeData/ChargeDescription[.="Leased Outdoor Lighting"])]
    排除詳細元素的值,其中,提供了一個派生ChargeDescription匹配排阻串
  • /Charges/ChargeData[ChargeDescription="subTotal-DistributionCharges"]
    在已過濾的細節元素下方,選擇ChargeData,其中有一個孩子提供ChargeDescription匹配字符串

所以

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

息率

2838.6599999999999 
+0

感謝您的答覆馬特。我修復了XML,對於結束標記感到抱歉。 我試過你在XML Spy中的解決方案,它不喜歡它。試過調整它,並不能到達那裏,但在VS2010中它是一個賓果遊戲。有一天我可能會對此語法有所瞭解。非常感謝! – Rick 2013-03-14 17:06:19