Get distinct attributes for XML nodes

末鹿安然 提交于 2019-12-08 09:45:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!