Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用vbscript遍历XML节点_Xml_Xpath_Vbscript - Fatal编程技术网

使用vbscript遍历XML节点

使用vbscript遍历XML节点,xml,xpath,vbscript,Xml,Xpath,Vbscript,我有一个XML文件,我想遍历它并输出几个特定属性。这是我第一次直接使用XML数据,但我认为这应该很简单,但我失败了 XML的简化版本是这样的——我已经从这个示例中删除了额外的属性 我希望遍历读取这些节点/属性的XML,以便我的输出与我收到的相同。然而,目前我得到一个标题,然后在一个长长的列表中的所有日期。 我是否需要计算所有节点,然后计算并输出每个结果?对我来说,这个例子太复杂了 <?xml version="1.0" encoding="utf-8" standalone="no"?&g

我有一个XML文件,我想遍历它并输出几个特定属性。这是我第一次直接使用XML数据,但我认为这应该很简单,但我失败了

XML的简化版本是这样的——我已经从这个示例中删除了额外的属性

我希望遍历读取这些节点/属性的XML,以便我的输出与我收到的相同。然而,目前我得到一个标题,然后在一个长长的列表中的所有日期。 我是否需要计算所有节点,然后计算并输出每个结果?对我来说,这个例子太复杂了

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<contact_list >
  <contact >
    <enrolment description="Online Course April 14" >     
      <student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01"  />
      <student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02"  />
      <student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
      <student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
    </enrolment>
    <enrolment description="Online Course May 14" >     
      <student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01"  />
      <student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02"  />
      <student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
      <student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
    </enrolment>
    <enrolment description="Online Course June 14" >     
      <student_teaching_day teaching_date="16/04/14" teaching_days="1" session_from="9:01" session_to="10:01"  />
      <student_teaching_day teaching_date="24/04/14" teaching_days="1" session_from="9:02" session_to="10:02"  />
      <student_teaching_day teaching_date="01/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
      <student_teaching_day teaching_date="08/05/14" teaching_days="1" session_from="9:03" session_to="10:03"  />
    </enrolment>
  </contact>
</contact_list>

我的剧本

For Each Node In xmlDoc.documentelement.selectNodes("//enrolment")
    If Not Node is Nothing Then
    course_description = Node.getAttribute("description")
    table_teaching_dates_start = "<table><tr><th colspan='4'><strong>"+course_description+"</strong></th></tr>"

        For Each Day In Node.selectNodes("./student_teaching_day")
                student_teaching_date = "<td>"+Day.getAttribute("teaching_date")+"</td>"
                session_from = "<td>"+Day.getAttribute("session_from")+"</td>" + "<td> - </td>"
                session_to = "<td>"+Day.getAttribute("session_to")+"</td>"

            student_dates = student_dates + "<tr>" +student_teaching_date + session_from + session_to + "</tr>"

        Next
        table_teaching_dates_end ="</table>"
        End If
Next

total = table_teaching_dates_start + table_row_start + student_dates + table_row_end + table_teaching_dates_end
对于xmlDoc.documentelement.selectNodes(//注册)中的每个节点 如果不是,则节点为空 课程描述=Node.getAttribute(“描述”) 表\u教学日期\u开始=“”+课程描述+”“ 对于节点中的每一天。选择节点(“/学生教学日”) 学生教学日期=”“+天。getAttribute(“教学日期”)+“” session_from=”“+Day.getAttribute(“session_from”)+“”+“-” session_to=”“+Day.getAttribute(“session_to”)+“” 学生日期=学生日期+“”+学生教学日期+课时从+课时到+“” 下一个 表\u教学\u日期\u结束=“” 如果结束 下一个 总计=表格教学日期开始+表格行开始+学生日期+表格行结束+表格教学日期结束
您的主
For Each
循环不断覆盖您在构建表时使用的变量,但您不断附加的
学生日期除外。循环逻辑正常,HTML构建逻辑不正常

也许您应该改变方法,不断地附加到同一个输出变量。下面的代码可以做到这一点,看起来更令人愉快

Dim注册、日期、输出、html
设置输出=新字符串缓冲区
对于xmlDoc.selectNodes中的每个注册(//注册)
输出。追加“”
输出。追加“”
output.Append-HTMLEscape(inclusion.getAttribute(“description”))
输出。追加“”
对于注册的每一天。选择节点(“/学生教学日”)
输出。追加“”
输出。追加“”
output.Append HTMLEscape(day.getAttribute(“教学日期”))
输出。追加“”
输出。追加“”
output.Append HTMLEscape(day.getAttribute(“session\u from”))
输出。追加“”
输出。追加“”
output.Append HTMLEscape(day.getAttribute(“session_to”))
输出。追加“”
输出。追加“”
下一个
输出。追加“”
下一个
html=output.ToString
使用帮助器类
StringBuffer
,这有助于提高字符串构建性能。(在VBScript中,就像在许多其他语言中一样,字符串是不可变的。将它们大量连接会很慢。)

类字符串缓冲区
模糊词典
子类_初始化
Set dict=CreateObject(“Scripting.Dictionary”)
端接头
子附加(s)
添加命令计数,s
端接头
函数到字符串
ToString=Join(dict.Items,“”)
端函数
子类_终止
Set dict=无
端接头
末级
在从字符串构建HTML时,您确实应该始终使用强制的HTML转义函数

功能HTMLEscape(s)
HTMLEscape=替换(_
取代(_
取代(_
替换_
&、&_
),  "", ""   _
), """", """ _
)
端函数


PS:请注意,如果您的测试
节点不是空的,那么测试就没有必要了。
节点
在这一点上永远不可能是
空的。

我认为尝试通过字符串连接来构造XML或HTML不是一个好主意,有XSLT可以将XML转换为XML或HTML。就XPath而言,首先要做
//code>然后,
/student\u teaching\u day
内部看起来不错。你能发布一个你得到的输出片段,并展示给我们你想要的吗?谢谢你的详细回答!我知道我对“简单”xml遍历的方法很幼稚!但正如我所说,你的xml遍历是正确的。