Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Xpath quandry..

Status
Not open for further replies.

Mrbaseball34

Programmer
Apr 17, 2002
24
US
Below is a snippet of one of my NAXML file.

I am using XPath to parse the nodes, however, I am having
difficulty getting this where it doesn't duplicate values.
I need to prepend the XPAth query for FuelGradeID and
FGMPositionSummary to get the correct nodes when iterating
the FGMDetail nodes. See Delphi, using MXSML, code below.

Can anyone help out with this XPath quandry?
Code:
<?xml version="1.0"?>
  <NAXML-MovementReport version="3.2">
    <TransmissionHeader>
      <StoreLocationID>6059</StoreLocationID>
      <VendorName>Gilbarco-VeederRoot</VendorName>
      <VendorModelVersion>5.00.27.01A  Shell Network</VendorModelVersion>
    </TransmissionHeader>
    <FuelGradeMovement>
      <MovementHeader>
        <ReportSequenceNumber>1</ReportSequenceNumber>
        <PrimaryReportPeriod>2</PrimaryReportPeriod>
        <SecondaryReportPeriod>0</SecondaryReportPeriod>
        <BusinessDate>2006-08-25</BusinessDate>
        <BeginDate>2006-08-25</BeginDate>
        <BeginTime>03:38:45</BeginTime>
        <EndDate>2006-08-26</EndDate>
        <EndTime>03:08:19</EndTime>
      </MovementHeader>
      <FGMDetail>
        <FuelGradeID>1</FuelGradeID>
        <FuelProductID/>
        <FGMNonResettableTotal>
          <FuelGradeNonResettableTotalVolume>2496833.95</FuelGradeNonResettableTotalVolume>
          <FuelGradeNonResettableTotalAmount>3632711.18</FuelGradeNonResettableTotalAmount>
        </FGMNonResettableTotal>
        <FGMPositionSummary>
          <FuelPositionID>1</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>600.87</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>1779.35</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
        <FGMPositionSummary>
          <FuelPositionID>2</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>757.001</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>2241.9</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
        <FGMPositionSummary>
          <FuelPositionID>3</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>689.973</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>2043.84</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
      </FGMDetail>
      <FGMDetail>
        <FuelGradeID>2</FuelGradeID>
        <FuelProductID/>
        <FGMNonResettableTotal>
          <FuelGradeNonResettableTotalVolume>430398.23</FuelGradeNonResettableTotalVolume>
          <FuelGradeNonResettableTotalAmount>784497.06</FuelGradeNonResettableTotalAmount>
        </FGMNonResettableTotal>
        <FGMPositionSummary>
          <FuelPositionID>1</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>16.671</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>51</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
        <FGMPositionSummary>
          <FuelPositionID>2</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>56.832</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>174.11</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
        <FGMPositionSummary>
          <FuelPositionID>3</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>20.132</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>61.82</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
        <FGMPositionSummary>
          <FuelPositionID>4</FuelPositionID>
          <FGMSalesTotals>
            <FuelGradeSalesVolume>12.096</FuelGradeSalesVolume>
            <FuelGradeSalesAmount>37</FuelGradeSalesAmount>
            <PumpTestAmount>0</PumpTestAmount>
            <PumpTestVolume>0</PumpTestVolume>
            <TaxExemptSalesVolume/>
            <DiscountAmount/>
            <DiscountCount/>
            <DispenserDiscountAmount/>
            <DispenserDiscountCount/>
          </FGMSalesTotals>
        </FGMPositionSummary>
      </FGMDetail>
    </FuelGradeMovement>
  </NAXML-MovementReport>
Here is the Delphi code to parse this file:
Code:
procedure LoadFGMFile;
const
  _FGMSalesTotals = '//FGMSalesTotals';
var
  i_fgm, i_fgmd, i_fgmps: integer;
  FuelGradeMovements, FGMDetails, FGMPositionSummaries: IXMLDOMNodeList;
  FuelProduct: String;
  FuelPosition: Integer;
begin
  FuelGradeMovements := GetNodeList(xml.documentElement, '//FuelGradeMovement');
  for i_fgm := 0 to FuelGradeMovements.length - 1 do
  begin
    // Get list of details
   {******  GetNodeList and GetNodeValue functions defined below  ******}
    FGMDetails := GetNodeList(FuelGradeMovements.item[i_fgm], '//FGMDetail');
    // For each detail
    for i_fgmd := 0 to FGMDetails.length - 1 do
    begin
      FuelProduct := GetNodeValue(FGMDetails.item[i_fgmd], '//FuelGradeID');
      FGMPositionSummaries := GetNodeList(FGMDetails.item[i_fgmd], '//FGMPositionSummary');
      for i_fgmps := 0 to FGMPositionSummaries.length - 1 do
      try
        FuelPosition := StringToInt(GetNodeValue(FGMPositionSummaries.item[i_fgmps], 
                                                 '//FuelPositionID'));
        // Post: Fuel Sale
        ImportData.PostDispenser(
          FuelPosition,
          FuelProduct,
          0,
          0,
          StringToFloat(GetNodeValue(FGMPositionSummaries.item[i_fgmps], 
                                     _FGMSalesTotals + '/PumpTestAmount')),
          StringToFloat(GetNodeValue(FGMPositionSummaries.item[i_fgmps], 
                                     _FGMSalesTotals + '/PumpTestVolume')),
          StringToFloat(GetNodeValue(FGMPositionSummaries.item[i_fgmps], 
                                     _FGMSalesTotals + '/FuelGradeSalesAmount')),
          StringToFloat(GetNodeValue(FGMPositionSummaries.item[i_fgmps], 
                                     _FGMSalesTotals + '/FuelGradeSalesVolume')),
          1);
      except
        on E: Exception do if E is EAbort then raise
        else if E is EConvertError then 
          LogInformation(peInvalidImportData, CodeSection, lkUserError)
        else 
          LogInformation(peSystemError, CodeSection, lkSystemError);
      end;
    end;
  end;
end;

function GetNodeValue(Node: IXMLDOMNode; XPath: String): String; overload;
begin
  try
    Result := Node.selectSingleNode(XPath).text;
  except
    Result := ''; // Just return blank if Exception occurs
  end;
end;

function GetNodeList(Node: IXMLDOMNode; XPath: String): IXMLDOMNodeList; overload;
begin
  Result := Node.selectNodes(XPath);
end;

function GetNodeList(Elem: IXMLDOMElement; XPath: String): IXMLDOMNodeList; overload;
begin
  Result := Elem.selectNodes(XPath);
end;
 
First, an off the topic question: Are you located in Central Texas, by any chance? I did gasoline in a former life...

I haven't spent a huge amount of time looking at this (not any kind of Delphi expert), but one thing does seem rather unusual. You are using XPath expressions that begin with '//' which says: find all the elements, anywhere in the document, that have the following name. I note that you are using this construct inside a loop, and this is almost always an error. You probably should be using XPath expressions that are relative to the node you are processing in the loop iteration.

Tom Morrison
 
Yes, Round Rock, to be exact. How would I change the XPath to get the ones relative to the one being passed to the GetNodeList function?
 
Try simply removing the '//' from the beginning of all your XPath expressions except //FuelGradeMovement. For example, if my context is a FuelPositionSummary element, then the XPath expression to create a nodeset (or node list) of all the FuelPositionID elements subordinate to that FuelPositionSummary element is simply 'FuelPositionID'. If there can be more than one subordinate (which does not appear to be the case here) then the XPath expression for the first FuelPositionID element is 'FuelPositionID[position()=1]' which can be abbreviated to the more familiar looking 'FuelPositionID[1]'.

(Please keep in mind that I am neither a Delphi or XML DOM expert.)

Tom Morrison
 
Thanks, I figured it out. there were other sections that needed the mods, too.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top