-1

Trying to pull out all the product names from the tag <NameProduct>, but the problem is that you need to pull out all the names of the products, which lie only in the interval <Product>...<PostAll>.

Below is a sample XML:

<?xml version="1.0" encoding="UTF-8"?>
<Products>
    <Product>
        <NameProduct>PRODUCT NAME 1</NameProduct>
        <CodeProduct>PRODUCT CODE 1</CodeProduct>
        <CodePlant>1111111111111111</CodePlant>
        <NameDesc>DESCRIPTION POS 1</NameDesc>
        <PostAll>
            <Post>
                <NameProduct>M1</NameProduct>
                <CodeProduct>M1</CodeProduct>
                <CodePlant>1</CodePlant>
                <NameDesc>??-1</NameDesc>
            </Post>
            <Post>
                <NameProduct>M2</NameProduct>
                <CodeProduct>M2</CodeProduct>
                <CodePlant>2</CodePlant>
                <NameDesc>??-2</NameDesc>
            </Post>
        </PostAll>
    </Product>
    <Product>
        <NameProduct>PRODUCT NAME 2</NameProduct>
        <CodeProduct>PRODUCT CODE 2</CodeProduct>
        <CodePlant>2222222222222222</CodePlant>
        <NameDesc>DESCRIPTION POS 2</NameDesc>
        <PostAll>
            <Post>
                <NameProduct>M3</NameProduct>
                <CodeProduct>M3</CodeProduct>
                <CodePlant>3</CodePlant>
                <NameDesc>??-3</NameDesc>
            </Post>
            <Post>
                <NameProduct>M4</NameProduct>
                <CodeProduct>M4</CodeProduct>
                <CodePlant>4</CodePlant>
                <NameDesc>??-4</NameDesc>
            </Post>
        </PostAll>
    </Product>
</Products>

Below is a sample code:

Dim node 
Dim nodes
Dim query
Dim xml
Dim i 
Dim attrs
Dim ConfigFile

Set xml = CreateObject("Msxml2.DOMDocument.3.0")
ConfigFile = "C:\ProgramData\xmlfile.xml" ' Configuration file path
Dim ok
ok = xml.Load(ConfigFile)

If (Not ok) Then
    MsgBox("File configuration failed!")
    Exit Sub
Else
    xml.SetProperty "SelectionLanguage", "XPath"
    query = "NameProduct"
    Set nodes = xml.SelectNodes(query)         

The method "XPath" does not work for me, apparently due to the fact that XML is missing attribute names.

Tell me how to return all the matches in a given interval XML?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • 1
    @Lankymart I agree with Ekkehard that the alleged duplicate is not an actual duplicate, or at least not a very good one, and considered re-opening this question myself. Both of our answers to the other questions aren't really that helpful for the problem described in this question. Please do not close questions for the sake of closing them. The solution(s) provided to a duplicate question should be applicable to the question that is being closed. – Ansgar Wiechers Apr 03 '19 at 16:27
  • @AnsgarWiechers ofc you do, there were three duplicates listed. You seriously suggesting it didn’t fit any of them? Look if the dup target doesn’t have a good enough answer then provide one rather than answering duplicate questions. – user692942 Apr 03 '19 at 16:32
  • Possible duplicate of [reading xml file with vbscript](https://stackoverflow.com/q/8254600), [Reading XML file nodes in VBSCript](https://stackoverflow.com/q/19249033), [Navigating XML nodes in VBScript, for a Dummy](https://stackoverflow.com/q/9723903), [Query XML Data using VBScript](https://stackoverflow.com/q/20569096), [Looping through XML file elements](https://stackoverflow.com/q/17482853), [xml parse with vbscript](https://stackoverflow.com/q/26144567), [using VBscript to extract data from XML from nodes and children](https://stackoverflow.com/q/19571565), – user692942 Apr 03 '19 at 16:49
  • Possible duplicate of [parsing xml with vbscript](https://stackoverflow.com/q/17014567/692942) – user692942 Apr 03 '19 at 17:08
  • 3
    The edit history clearly shows that there was only one duplicate. There may have been other mentions in comments, but if there were they had already been deleted without being added to the linked questions. BTW, you do realize that about a third of those can't be used as a duplicate, b/c they don't have an upvoted or accepted answer, and another third doesn't apply here at all, since this question isn't about attributes in the first place. One or two of them might actually be valid duplicates, and had the question been closed as a duplicate of them it might actually have remained closed. – Ansgar Wiechers Apr 03 '19 at 17:59
  • @AnsgarWiechers I do, I’m just making the point. As a question “Parse XML in VBScript” is ambiguous to start with. There is enough questions / answers there to point the OP in the right direction. – user692942 Apr 03 '19 at 18:36
  • 4
    The concept of "duplicate" (defined as: This question has been asked before and already has an answer.") is ok for questions like "What is sum of 4 and 5?" and answers like "4+5 = 9". Typical questions here need more thought and interpretation just to identify the/a (possible) nub of the problem; and then a non-arrogant flaggelant couldn't rule out a new answer. But the worst con of "duplicate" is that it allows people who can't provide creative solutions or decent code to generate noise and harassment. – Ekkehard.Horner Apr 03 '19 at 19:22
  • @Ekkehard.Horner the only *”noise”* here is members who actively work against the concept of a canonical library for programmers by programmers in favour of yahoo answers! – user692942 Apr 03 '19 at 20:01
  • @Ekkehard.Horner there are at least three or four questions there that detail how to load XML and parse it using the DOM, using XPath and iterate it using VBScript. Why do we need another? – user692942 Apr 03 '19 at 20:03
  • For full disclosure, this question has prompted a somewhat broader Meta discussion: https://meta.stackoverflow.com/questions/382402/ – TylerH Apr 03 '19 at 22:03
  • 1
    @Lankymart FWIW I don't think any of the dupe targets serve as a good 'canonical' post; they all seem like specific debugging questions rather than a broad/vanilla issue with broadly applicable solutions, despite all being potentially the same issue. Perhaps this is a good opportunity for you to create a solid self-answered/canonical Q&A on the subject. – TylerH Apr 03 '19 at 22:06

1 Answers1

3

Your XPath expression doesn't work because it doesn't match what you think it matches. A query NameProduct will not find <NameProduct> nodes anywhere in the XML structure, but only under the current node (which in your case is the document root node). Since there are no <NameProduct> nodes directly under <Products> you're getting an empty result set. Missing attributes have nothing to do with it.

What you want is an XPath expression //Product/NameProduct (find <NameProduct> nodes that are immediate child nodes of <Product> nodes anywhere in the document):

query = "//Product/NameProduct"
Set nodes = xml.selectNodes(query)         

The double slash essentially means "anywhere below".

Please read up on how XPath works.


As a side note: Msxml2.DOMDocument.3.0 is buggy and should not be used anymore. Use Msxml2.DOMDocument.6.0 instead. Also, you may want to disable asynchronous loading of your XML file to make sure the document is actually loaded completely before you start processing it:

Set xml = CreateObject("Msxml2.DOMDocument.6.0")
xml.Async = False
ok = xml.Load("C:\ProgramData\xmlfile.xml")
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328