我有以下xml文件:
<courses>
<course>
<name>Course 1</name>
<code>00162</code>
<questions>2,2,1,1,2,1,1,1</questions>
</course>
</courses>
Run Code Online (Sandbox Code Playgroud)
我需要查询文件(我使用xpath)来拆分'questions'元素,检查每个数字出现的位置,并检查它是否为数字1或2.
基本上我需要在xpath中这样做:
Dim ints As String() = QuestionsString.ToString.Split(",")
Dim i As Integer
For i = 0 To UBound(ints)
If ints(i) = "2" Then
'do something
Else
'do something else
End If
Next
Run Code Online (Sandbox Code Playgroud)
从评论更新
嗨,谢谢你.我打算编辑这个问题,因为它不正确.我想要得到的,例如,所有的课程名称和代码,其"问题"元素(拆分后)有"2"中的第二位置,在1,2,2,1,1,1,2,1谢谢!
XPath 中有tokenize(string, pattern),所以你可以写
/courses/course/questions[(tokenize(text(), "[0-9]+")) ]
put something clever here -------^
Run Code Online (Sandbox Code Playgroud)
但我不明白你所说的“做某事”和“做其他事”是什么意思。XPath 用于选择节点/检索信息。您需要 XSLT 或 XQuery 来“做其他事情”。