Read attribute of a parent/ancestor node using xpath axes
我正在尝试使用XPath和XSLT从"学生"节点读取"类"节点的"分区"属性。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <class division="myDiv"> <student rollno="700"> <firstname>Renjith</firstname> <lastname>R</lastname> <nickname>Renju</nickname> <marks>70</marks> </student> <student rollno="493"> <firstname>fname1</firstname> <lastname>lname1</lastname> <nickname>nick1</nickname> <marks>95</marks> </student> </class> |
我的条件是,如果名字是" Renjith",请读取" class"节点的" division"属性,我认为这是学生节点的父节点。
我使用以下xpath检查名字是否为" Renjith"
1 | //student[firstname/text() = 'Renjith'] |
我在名字为" Renjith"的学生节点上。现在我需要从作为学生节点父级的"类"节点获取除法属性的值。
我可以使用以下xpath表达式实现结果。
1 2 3 | 1)parent::class/@division or 2)ancestor::class/@division |
我在这里有两个问题。
1.黑白表达式1和2有什么区别?
2.有什么方法可以在不指定父节点名称"类"的情况下获得"分区"属性?
我知道'