高级 XPath 查询
我有一个如下所示的 XML 文件:
<?xml version="1.0" encoding="utf-8" ?>
<PrivateSchool>
<Teacher id="teacher1">
<Name>
teacher1Name
</Name>
</Teacher>
<Teacher id="teacher2">
<Name>
teacher2Name
</Name>
</Teacher>
<Student id="student1">
<Name>
student1Name
</Name>
</Student>
<Student id="student2">
<Name>
student2Name
</Name>
</Student>
<Lesson student="student1" teacher="teacher1" />
<Lesson student="student2" teacher="teacher2" />
<Lesson student="student3" teacher="teacher3" />
<Lesson student="student1" teacher="teacher2" />
<Lesson student="student3" teacher="teacher3" />
<Lesson student="student1" teacher="teacher1" />
<Lesson student="student2" teacher="teacher4" />
<Lesson student="student1" teacher="teacher1" />
</PrivateSchool>
还有一个与此 XML 关联的 DTD,但我认为它与我的问题没有太大关系。我们假设所有需要的教师和学生都已明确定义。
返回教师姓名(至少有一名学生与他们一起上过 10 堂以上课程)的 XPath 查询是什么?
我查看了许多 XPath 站点/示例。对于这类问题,似乎没有什么足够先进的。
I have an XML file that looks like this:
<?xml version="1.0" encoding="utf-8" ?>
<PrivateSchool>
<Teacher id="teacher1">
<Name>
teacher1Name
</Name>
</Teacher>
<Teacher id="teacher2">
<Name>
teacher2Name
</Name>
</Teacher>
<Student id="student1">
<Name>
student1Name
</Name>
</Student>
<Student id="student2">
<Name>
student2Name
</Name>
</Student>
<Lesson student="student1" teacher="teacher1" />
<Lesson student="student2" teacher="teacher2" />
<Lesson student="student3" teacher="teacher3" />
<Lesson student="student1" teacher="teacher2" />
<Lesson student="student3" teacher="teacher3" />
<Lesson student="student1" teacher="teacher1" />
<Lesson student="student2" teacher="teacher4" />
<Lesson student="student1" teacher="teacher1" />
</PrivateSchool>
There's also a DTD associated with this XML, but I assume it's not much relevant to my question. Let's assume all needed teachers and students are well defined.
What is the XPath query that returns the teachers' NAMES, that have at least one student that took more than 10 lessons with them?
I was looking at many XPath sites/examples. Nothing seemed advanced enough for this kind of question.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在单个 XPath 中进行复杂的连接也许是可能的,但您却在碰壁。 XQuery 或 XSLT 更适合这种事情。它在 XQuery 中:
完成此操作后,如果您确实确定可以将其简化为 XPath 2.0:
未测试。
Doing a complex join in a single XPath may be possible, but you're banging your head against a brick wall. XQuery or XSLT are much more suited to this kind of thing. Here it is in XQuery:
Having done that, if you are really determined you can reduce it to XPath 2.0:
Not tested.
这是一个 XPath 2.0 解决方案:
This is an XPath 2.0 solution:
使用此 XPath 2.0 表达式:
此处我已将
$limit
设置为 2,以便根据提供的 XML 文档计算此 XPath 表达式时:它会产生正确的结果:
在您的真实表达式中,您将
$limit
设置为10
并且仅返回教师的结果名称:Use this XPath 2.0 expression:
here I have set
$limit
to 2, so that when this XPath expression is evaluated against the provided XML document:it produces the correct result:
In your real expression you'll have
$limit
set to10
and will only return the teachers' names:Michael Kay 为 xpath 2.0 发布的解决方案是正确的,但是是近似的。问题中发布的 xml 的确切解决方案是(没有绝对路径):(
我使用“gt 1”而不是“gt 10”以获得一些结果)
The solution posted by Michael Kay for xpath 2.0 is correct, but aproximate. An exact solution for the xml posted at the question would be (without absolute paths):
(I used "gt 1" instead of "gt 10" in order to get some result)