M
Martin Danielson
I have a problem that I can't work out. I have a script in ASP using VBScript that act quite funny when editing and xml file. Here is my explenation
I have the following xml-fil
<root><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element><element type="type2">element 4</element><element type="type2">element 5</element></root
I wanted to order them by type, by creating a new node for each type in the root element and then inserting the corresponding elements into the type node. The output should be looking like this
<root><type1><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element></type1><type2><element type="type2">element 4</element><element type="type2">element 5</element></type2></root
Now, when doing this I first got all the element nodes into an IXMLDOMNodeList, I used the method .getElementsByTagName( "element" ). No problem there. Since I don't like to use "For X = 0 To Y" I used "For Each X In Y" instead, where Y was the IXMLDOMNodeList
Then, within the loop i took the current Nodes parent and appended a child to it with the type as the nodeName, if the type was different from the last nodes type. Then I kept adding current node to the nodes parents last child, which resulted in me moving the elements into the correct type node
When doing this the first time i printed the length of the IXMLDOMNodeList at the beginning of the file, but when i removed it everything went crazy. It seems that when I dodn't check the length (either by responsing it or just by setting a "dumb" variable to it) the loop went on forever. I read that the IXMLDOMNodeList was live and that got me thinking that for each loop the referense to .getElementsByTagName was run and it would keep finding new ones. But that still dowesn't explain why it worked when geting the length of it first hand. I later on tried the "For X = 0 To IXMLDOMNodeList.length -1" approach and that worked fine since i called upon the length property
Then I got back to the "For Each X In Y" code and added a exact copy of the enumeration before the orgininal one, without doing anything, and that seemed to "lock" the length and worked fine. This got me really confused
The last thing I tried was to use the selectNodes method where i specifically got the "/root/element" nodes and that turned out great. When using "//element" as the XPath the script acted as when I use the .getElementsByTagName approach. I stuck with this just to get moving but I am still not up to speed what was going on. Is this a bug or is it supposed to re-run the search every iteration when using "For Each X In Y"
Please, put me at ease and explain this to me. I used DOMDocument.3.0
Many thanks in advance
Martin Danielson
I have the following xml-fil
<root><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element><element type="type2">element 4</element><element type="type2">element 5</element></root
I wanted to order them by type, by creating a new node for each type in the root element and then inserting the corresponding elements into the type node. The output should be looking like this
<root><type1><element type="type1">element 1</element><element type="type1">element 2</element><element type="type1">element 3</element></type1><type2><element type="type2">element 4</element><element type="type2">element 5</element></type2></root
Now, when doing this I first got all the element nodes into an IXMLDOMNodeList, I used the method .getElementsByTagName( "element" ). No problem there. Since I don't like to use "For X = 0 To Y" I used "For Each X In Y" instead, where Y was the IXMLDOMNodeList
Then, within the loop i took the current Nodes parent and appended a child to it with the type as the nodeName, if the type was different from the last nodes type. Then I kept adding current node to the nodes parents last child, which resulted in me moving the elements into the correct type node
When doing this the first time i printed the length of the IXMLDOMNodeList at the beginning of the file, but when i removed it everything went crazy. It seems that when I dodn't check the length (either by responsing it or just by setting a "dumb" variable to it) the loop went on forever. I read that the IXMLDOMNodeList was live and that got me thinking that for each loop the referense to .getElementsByTagName was run and it would keep finding new ones. But that still dowesn't explain why it worked when geting the length of it first hand. I later on tried the "For X = 0 To IXMLDOMNodeList.length -1" approach and that worked fine since i called upon the length property
Then I got back to the "For Each X In Y" code and added a exact copy of the enumeration before the orgininal one, without doing anything, and that seemed to "lock" the length and worked fine. This got me really confused
The last thing I tried was to use the selectNodes method where i specifically got the "/root/element" nodes and that turned out great. When using "//element" as the XPath the script acted as when I use the .getElementsByTagName approach. I stuck with this just to get moving but I am still not up to speed what was going on. Is this a bug or is it supposed to re-run the search every iteration when using "For Each X In Y"
Please, put me at ease and explain this to me. I used DOMDocument.3.0
Many thanks in advance
Martin Danielson