Page 1 of 1

Loop XML with namespace is not working

Posted: Tue May 04, 2021 12:48 pm
by automation
I have this XML with namespace and I want to get e.g all the numbers in the Number tag. But the loop is not working with the namespace. I need to use a loop like this because I'am doing other stuff as well in the loop.

Code: Select all

<xx.yy xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xx.se/schemas/xx.yy/3.0/integration IntegrationDocumentSchema.xsd" xmlns="http://xx.se/schemas/xx.yy/3.0/integration">
    <Envelope>
        <Client>Cusomer</Client>
    </Envelope>
    <Data>
        <Item>
            <Number>11111</Number>
            <Balance>2</Balance>
        </Item>
        <Item>
            <Number>2222</Number>
            <Balance>4</Balance>
        </Item>
    </Data>
</xx.yy>
I have this code. I have comment out some code when I try to parse the XML with the namespace but it's not working. I don't need to use the namespace I just need to parse/loop the XML.

Code: Select all

function jobArrived( s : Switch, job : Job )
{
	var path = s.getSpecialFolderPath("PluginResources");
	var companyXML = File.toNativeSeparators(path + "/myXML.xml");
	var selectedNode;
	
	var company = new Document(companyXML);
	//var companyns = 	company.createDefaultMap();
	

	var companyItems = company.evalToNodes("/xx.yy/Data/Item", null);
	//var companyItems = company.evalToNodes("/xx.yy/Data/Item", companyns);

	for (var i = 0; i < companyItems.length; i++) {
		
		s.log(1, "Loop working");	

	}
}
If I remove the namespace my code works fine. But I will get the XML with namespace.

Code: Select all

<xx>
    <Envelope>
        <Client>Cusomer</Client>
    </Envelope>
    <Data>
        <Item>
            <Number>11111</Number>
            <Balance>2</Balance>
        </Item>
        <Item>
            <Number>2222</Number>
            <Balance>4</Balance>
        </Item>
    </Data>
</xx>
I can I change my code to make it work with the first XML with namespace?

Re: Loop XML with namespace is not working

Posted: Tue May 04, 2021 1:32 pm
by freddyp
The commented lines have to go back in. The problem is the fact that you have not taken into account nodes that use the default namespace. Remember these two principles when using XPaths:
  • when an XML uses namespaces the XPaths must use namespaces for ALL nodes
  • the default namespace in an XPath IS NOT EMPTY. The string that is used for the default namespace depends on the environment; for Switch it is dn.
Without having tested it I would say that this line:

Code: Select all

	var companyItems = company.evalToNodes("/xx.yy/Data/Item", companyns);
should become

Code: Select all

	var companyItems = company.evalToNodes("/xx.yy/dn:Data/dn:Item", companyns);

Re: Loop XML with namespace is not working

Posted: Tue May 04, 2021 1:54 pm
by automation
Thanks it worked!

Re: Loop XML with namespace is not working

Posted: Tue Feb 21, 2023 4:46 am
by celinedion
I had this problem too, glad to hear the support info here.