选择其名称包含在另一个节点中的元素?

时间:2016-02-02 14:19:41

标签: xml xpath

鉴于此XML

<DateInfo date="2016/02/02 13:11:12">
  <Year>2016</Year>
  <Month number="2">February</Month>
  <Day ofMonth="2" ofYear="33">Tuesday</Day>
  <Hour am="false" pm="true">13</Hour>
  <Minute>11</Minute>
  <Second>12</Second>
  <Monday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <Tuesday is="true" numInMonth="1" firstInMonth="true" lastInMonth="false" />
  <Wednesday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <Thursday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <Friday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <Saturday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <Sunday is="false" numInMonth="0" firstInMonth="false" lastInMonth="false" />
  <WeekDay is="true" isFirstWeekDay="false" isLastWeekDay="false" />
  <Weekend is="false" />  
  <DueDay>Tuesday</DueDay>
</DateInfo>

如何获取名称为firstInMonth元素值的元素的<DueDay>属性的值?

例如,DueDayTuesday,因此我想获取元素firstInMonth的{​​{1}}属性的值,即'true'。

我在现有框架中工作,无法访问XPath 2.

1 个答案:

答案 0 :(得分:2)

要选择名称由@firstInMonth子项提供的DateInfo子项的DueDay属性,请使用此XPath 1.0表达式:

/DateInfo/*[name() = ../DueDay]/@firstInMonth

对于您的XML,这将根据要求选择true

相关问题