2013-03-14 58 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