Loop XML with namespace is not working

Post Reply
automation
Member
Posts: 40
Joined: Tue Jan 15, 2019 10:19 pm

Loop XML with namespace is not working

Post 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?
freddyp
Advanced member
Posts: 1008
Joined: Thu Feb 09, 2012 3:53 pm

Re: Loop XML with namespace is not working

Post 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);
automation
Member
Posts: 40
Joined: Tue Jan 15, 2019 10:19 pm

Re: Loop XML with namespace is not working

Post by automation »

Thanks it worked!
celinedion
Newbie
Posts: 1
Joined: Mon Feb 20, 2023 9:47 am

Re: Loop XML with namespace is not working

Post by celinedion »

I had this problem too, glad to hear the support info here.
Post Reply