AL icon indicating copy to clipboard operation
AL copied to clipboard

SelectSingleNode allays return TRUE

Open dkorinets opened this issue 9 years ago • 14 comments

The "SelectSingleNode" always return TRUE even if 'NodeName' does not exist.

If ParentNode.SelectSingleNode('NodeName',ResultNode) then NodeValue := ResultNode.AsXmlText.Value;

ResultNode - is not initialed

dkorinets avatar Sep 13 '17 08:09 dkorinets

I am looking at a test for exactly this case and it returns false. Could you post an XML doc that repros this?

atoader avatar Sep 13 '17 12:09 atoader

<?xml version="1.0"?> <Request> <Operation>GetPriceInfo</Operation> <Params> <AccountId>20000</AccountId> <AccountType>Customer</AccountType> <CurrencyId/> <MultiCurrency>1</MultiCurrency> <LoadRelatedSkus>1</LoadRelatedSkus> <Product> <Id>1896-S</Id> <UnitOfMeasureId>PCS</UnitOfMeasureId> </Product> <ExtraFields> <field name="Product Group Code" /> </ExtraFields> <WebsiteId>nav2016</WebsiteId> </Params> </Request>

dkorinets avatar Sep 13 '17 12:09 dkorinets

Thanks! Do you also have a query that fails on this?

atoader avatar Sep 13 '17 12:09 atoader

Nope, just crashes doe to ResultNode - is not initialized

dkorinets avatar Sep 13 '17 12:09 dkorinets

If CurrentNodeGlobal.SelectNodes(QueryString,ResultNodeList) then ..

The almost same behavior for "SelectNodes" - return TRUE but QueryString does not exist in XML, the diffidence just that "ResultNodeList" stays initialized.

Check that please as well.

dkorinets avatar Sep 14 '17 11:09 dkorinets

In the case of SelectNodes, the operation will only return false or fail if the xpath is invalid or if another error happens. If the xpath does not match any nodes, the list will be empty.

In the case of SelectSingleNode, I tested it with the following snippet and it works as expected.

pageextension 50100 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        doc : XmlDocument;
        n : XmlNode;
    begin
        XmlDocument.ReadFrom('<root><book></book></root>', doc);

        if doc.SelectSingleNode('/something', n) then
            Message('Found ' + n.AsXmlElement.Name)
        else
            Message('Did not find something');

        if doc.SelectSingleNode('/root', n) then
            Message('Found ' + n.AsXmlElement.Name)        
        else
            Message('Did not find root');
    end;
}

I get "Dit not find something" and then "Found root"

atoader avatar Sep 19 '17 07:09 atoader

Yes it is, You have tried with XmlDoc, but in my case node is used. ParentNode.SelectSingleNode('NodeName',ResultNode) In the case it returns as TRUE and ResultNode - UNINITIALIZED

I'll double check it from my side as well

dkorinets avatar Sep 19 '17 08:09 dkorinets

Ok. I will check with an XmlNode. Maybe that is the problem. I will cast the doc to a node and try to see. Maybe there is a bug in the runtime.

atoader avatar Sep 19 '17 09:09 atoader

Second attempt below. I still get the expected behavior. If you can provide a full snippet with a small sample document that reproduces the error it would be of great help.

pageextension 50101 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    var
        doc : XmlDocument;
        root : XmlNode;
        book : XmlNode;
    begin
        XmlDocument.ReadFrom('<root><book></book></root>', doc);

        if doc.SelectSingleNode('/root', root) then begin
            Message('Found root');

            if root.SelectSingleNode('book', book) then
                Message('Found ' + book.AsXmlElement.Name)
            else
                Message('Did not find something');
        end
        else
            Message('Did not find root');
    end;
}

atoader avatar Sep 19 '17 11:09 atoader

CurrentNodeGlobal = '<Params><VisibleOnly>1</VisibleOnly><LoadRelatedSkus>1</LoadRelatedSkus><LoadVisibilityRules>1</LoadVisibilityRules><CalculatePrices>1</CalculatePrices><CalculateInventory>1</CalculateInventory><CalculateSkuPrices>1</CalculateSkuPrices><CalculateSkuInventory>1</CalculateSkuInventory><CheckStock>1</CheckStock><AccountId>10000</AccountId><AccountType>Customer</AccountType><MultiCurrency>0</MultiCurrency><PageIndex>0</PageIndex><PageSize>1</PageSize><SortField>Id</SortField><SortAscending>1</SortAscending><ExtraFields /><Filter><for field="ModifiedDate"><LessOrEqual>9/18/2017 6:10:36 AM</LessOrEqual></for></Filter><WebsiteId>NAV2016</WebsiteId></Params>'

QueryString = '//CurrencyId' - is not present in the xml;

Case true of
	CurrentNodeGlobal.IsXmlElement :
        begin
          CurrentXmlElement := CurrentNodeGlobal.AsXmlElement;
          IF CurrentXmlElement.SelectSingleNode(QueryString,ResultNode) then  //?? - ResultNode - becomes uninitialized here.
            case true of
              ResultNode.IsXmlAttribute :
                exit(ResultNode.AsXmlAttribute.Value);
              ResultNode.IsXmlElement : 
                exit(ResultNode.AsXmlElement.InnerXml);    
              ResultNode.IsXmlText :
                exit(ResultNode.AsXmlText.Value);  
            end;    
        end;  
 end;

dkorinets avatar Sep 20 '17 06:09 dkorinets

Still works as expected.

pageextension 50101 CustomerListExt extends "Customer List"
{
    trigger OnOpenPage();
    begin
        GetText();
    end;

    local procedure GetText() : Text;
     var
        doc : XmlDocument;
        currentXmlElementNode : XmlNode;
        currentXmlElement : XmlElement;
        ResultNode : XmlNode;
        book : XmlNode;
    begin
        XmlDocument.ReadFrom('<Params><VisibleOnly>1</VisibleOnly><LoadRelatedSkus>1</LoadRelatedSkus><LoadVisibilityRules>1</LoadVisibilityRules><CalculatePrices>1</CalculatePrices><CalculateInventory>1</CalculateInventory><CalculateSkuPrices>1</CalculateSkuPrices><CalculateSkuInventory>1</CalculateSkuInventory><CheckStock>1</CheckStock><AccountId>10000</AccountId><AccountType>Customer</AccountType><MultiCurrency>0</MultiCurrency><PageIndex>0</PageIndex><PageSize>1</PageSize><SortField>Id</SortField><SortAscending>1</SortAscending><ExtraFields /><Filter><for field="ModifiedDate"><LessOrEqual>9/18/2017 6:10:36 AM</LessOrEqual></for></Filter><WebsiteId>NAV2016</WebsiteId></Params>', doc);

        doc.SelectSingleNode('/Params/Filter', currentXmlElementNode);

        case true of 
            currentXmlElementNode.IsXmlElement :
            begin
                currentXmlElement := currentXmlElementNode.AsXmlElement();

                if currentXmlElement.SelectSingleNode( '//CurrencyId', ResultNode) then begin
                    case true of
                    ResultNode.IsXmlAttribute :
                        exit(ResultNode.AsXmlAttribute.Value);
                    ResultNode.IsXmlElement : 
                        exit(ResultNode.AsXmlElement.InnerXml);    
                    ResultNode.IsXmlText :
                        exit(ResultNode.AsXmlText.Value);  
                    end;
                end 
                else
                    Message('Did not find root');
            end;
        end;
    end;
}

atoader avatar Sep 20 '17 11:09 atoader

From my side Your example code does not work as well. Bellow You can find a screenshot. As You can see after IF statement ResultNode becomes "Uninitialized". Dev environment on Docker, image is "navdocker.azurecr.io/dynamics-nav:devpreview-finus", so the version of build should be the latest. AL Language 0.9.12465 September Update (LATEST) xmldoc_error

What might be wrong?

dkorinets avatar Sep 20 '17 14:09 dkorinets

In the latest version, it works as expected. As soon as the October update gets pushed out, let me know if this still happens.

atoader avatar Sep 22 '17 08:09 atoader

Could I know when October update will be available please?

dkorinets avatar Oct 09 '17 07:10 dkorinets