我想找到所有非必填问题的答案。这是xml代码:
<submittedPoll>
<pollHead>
<code>2</code>
</pollHead>
<pollBody>
<topic code="T1">
<topicHead>
<title>XML Section</title>
</topicHead>
<topicBody>
<question code="T1Q1" required="true" type="unique">Which encoding did you use?</question>
<answer code="T1Q1_1">ISO-8859-1</answer>
<question code="T1Q2" required="true" type="unique">How did you implement blabla?</question>
<answer code="T1Q2_1">blabla</answer>
<question code="T1Q3" type="unique">Did you blabla?</question>
<question code="T1Q4" type="unique">How many times did you blabla?</question>
</topicBody>
</topic>
<topic code="T4">
<topicHead>
<title>Other section</title>
</topicHead>
<topicBody>
<question code="T4Q1" type="unique">blabla question?</question>
<question code="T4Q2" type="unique">blabla other question?</question>
<question code="T4Q3" required="true" type="multiple">blabla third question?</question>
<answer code="T4Q3_1">blabla</answer>
<answer code="T4Q3_2">blablabla</answer>
<question code="T4Q4" type="unique">blablarly?</question>
<question code="T4Q5" type="unique">blablanotrly?</question>
<question code="T4Q6" type="unique">How did you do that?</question>
<answer code="T4Q6_2">BaseX</answer>
</topicBody>
</topic>
</pollBody>
</submittedPoll>
我写了这个 XPath 查询,但它并没有像我预期的那样工作:
/submittedPoll/pollBody/topic/topicBody/question[not(@required) or (@required="false")]/following-sibling::answer[starts-with(@code, current()/@code)]
这将返回 T4Q3_1、T4Q3_2 和 T4Q6_2。但是 T4Q3_1 和 T4Q3_2 是错误的,因为它们遵循一个必填问题。
我究竟做错了什么?也许我没有正确理解该current()
功能。
注意:所有答案“代码”属性都以相对问题“代码”属性开头(即问题T4Q3带有答案T4Q3 _1、T4Q3 _2 等)