Is a FOR EACH loop possible? In normal VBScript you can do something like this...
‘strQuery = “/ProfilesInfo/Profiles/ProfileInfo/Profile/Fields/Field [Name='ProfileUri' or Name= 'Age'] /(Name|Value)”
Set colNodes = xmlDoc.selectNodes( strQuery )
If colNodes.length>0 Then
For Each objNode in colNodes
Print objNode.nodeName & “: ” & objNode.text
is it possible to do something like this with XML? Maybe something like
For each node in myDocument.XMLElements.Item(1)
If there is a better way, and I am sure there is, I'll take an example of that instead.
I am sure there is probably a way to do it with XPath, but I haven't been able to find any examples of using XPath with VBScript.
If anyone has an example of how to use XPath with VBScript I would greatly appreciate it.
You can do this using XML rules. Check out a chapter of the same name in the InDesign CS5 Scripting Guide (VB version). There you will find many examples.
You can indicate which nodes you want to process with XPath. For example, this will match on every XML element in the XML structure:
Public Property Get xpath xpath = "//*" End Property
And this matches all following sibling XML elements of the first "item" XML element:
Public Property Get xpath xpath = "/xmlElement/item/following-sibling::*" End Property
Kasyan, thank you for the reply. I looked at that part in the book and I have tried using the example code, but I am having difficulty in understanding how it is being used, maybe you, or someone can help to explain it.
So in an example like this:
Public Property Get name
name = "ProcessChildrenRule"
Public Property Get xpath
xpath = "//XMLElement"
Public Function apply(myXMLElement, myRuleProcessor)
.XMLAttributes.Item(1).Value & vbCr
apply = false
There are 5 things that I am not understanding.
1. How is "xpath" being used? I don't see another occurance of it other than when the variable xpath is set.
2. How is name being used? I don't see another occurance of it other than when the variable name is set.
3. Why is "Public Function apply" created in each class? In the examples in the book, that function exists in each Class. Why not just create the function outside of the class and call it from inside the class?
4. Where is the "apply" function being called from? It doesn't get called from inside or outside the class on the example pages anywhere?
5. Where are the values of myXMLElement and myRuleProcessor being set before they are used in the function?
Even in the examples you gave, once I set the xpath value, what do I do with it after that? How do I write out the value of what I am searching for with the xpath?
So going back to my original question, if I set xpath = "/devices/device", how do I loop through each "device" node and write them out? That is where it is breaking down for me. I haven't seen any examples in the book or in the downloaded example code that show how to do that.