【发布时间】:2014-10-07 16:54:03
【问题描述】:
我有一个 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
【问题讨论】:
-
我不认为尝试构造 XML 或 HTML 进行字符串连接是一个好主意,有 XSLT 将 XML 转换为 XML 或 HTML。就 XPath 而言,先做
//enrolment然后再做./student_teaching_day里面看起来不错。您可以发布您获得的输出的 sn-p 并向我们展示您想要的输出吗?