问题
I`ve been trying to get distinct attribute values from the XML: dim i,xmlOx,arr(100) "xmlOx" has following xml structure
<root>
<a x="Animal" y="Bird"/>
<a x="Animal" y="Bird"/>
<a x="Country" y="Bird"/>
<a x="Animal" y="Bird"/>
</root>
asp:
for i=0 xmlOx.selectNodes("a").length-1
arr(i)=xmlOx(i).selectNodes("a").getAttribute("x")
next
Like,here ive to get "x" attribute values but I dont need duplicates.Then I need to add the values to an array in vbscript.
Please someone tell me how we do it?
回答1:
The theory is here.
Code:
Dim sXml : sXml = Join(Array( _
"<root>" _
, "<a x=""Animal"" y=""Bird""/>" _
, "<a x=""Animal"" y=""Bird""/>" _
, "<a x=""Country"" y=""Bird""/>" _
, "<a x=""Animal"" y=""Bird""/>" _
, "</root>" _
))
Dim oXDoc : Set oXDoc = CreateObject("Msxml2.DOMDocument")
oXDoc.loadXml sXml
WScript.Echo oXDoc.xml
Dim xObjXml : Set xObjXml = oXDoc.documentElement.childNodes
Dim dicAttrs : Set dicAttrs = CreateObject("Scripting.Dictionary")
Dim i
For i = 0 To xObjXml.length - 1
Dim a : a = xObjXml(i).getAttribute("x")
dicAttrs(a) = dicAttrs(a) + 1
Next
Dim aAttrs : aAttrs = dicAttrs.Keys()
WScript.Echo Join(aAttrs)
Output:
<root>
<a x="Animal" y="Bird"/>
<a x="Animal" y="Bird"/>
<a x="Country" y="Bird"/>
<a x="Animal" y="Bird"/>
</root>
Animal Country
来源:https://stackoverflow.com/questions/13514795/get-distinct-attributes-for-xml-nodes