我有一個XML文件,我想遍歷並輸出一些特定的屬性。這是我第一次直接使用XML數據,但我認爲這應該是直接的,但我被擊敗了。使用vbscript遍歷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中,首先執行'//註冊'然後''/ student_teaching_day'內部看起來很好。你可以發佈一個你得到的輸出片段,並告訴我們你想要的嗎? – 2014-10-07 17:40:24