我正在尝试使用带有谓词的表达式来显示每个父节点的子节点(练习看起来很愚蠢,因为这是测试数据:最终解决方案必须绘制数据来自其他XML文件)。
数据样本如下:
<Schools>
<School>
<name>St John's</name>
<AcademicYear year = "2017"></AcademicYear>
<Subject>English</Subject>
<Subject>Geography</Subject>
<Subject>Maths</Subject>
<AcademicYear year = "2016"></AcademicYear>
<Subject>Maths</Subject>
<Subject>Textiles</Subject>
<Subject>English</Subject>
<AcademicYear year = "2015"></AcademicYear>
<Subject>History</Subject>
<Subject>French</Subject>
<Subject>Spanish</Subject>
</School>
<School>
<name>Marsh Academy</name>
<AcademicYear year = "2017"></AcademicYear>
<Subject>Science</Subject>
<Subject>Geography</Subject>
<Subject>Computer science</Subject>
<AcademicYear year = "2016"></AcademicYear>
<Subject>English</Subject>
<Subject>History</Subject>
<Subject>Maths</Subject>
<AcademicYear year = "2015"></AcademicYear>
<Subject>French</Subject>
<Subject>Geography</Subject>
<Subject>Art</Subject>
</School>
</Schools>
查询如下:
<Schools>
{for $School in doc("test.xml")/Schools/School
return
<School>
<Name>
{$School/name}
</Name>
{for $i in doc("test.xml")/Schools/[School = $School]/AcademicYear
return
<AcademicYear>
{$i/@year}
<subject><teacher></teacher><scale></scale></subject>
</AcademicYear>}
</School>}
</Schools>
出现的错误显示了与第8行有关的"节点预期,发现的阵列"。
态
对不起 - 我确定它很简单。我是初学者!
谓词之前没有子节点。
选择School
元素时,将其移到谓词外部,作为从Schools
元素中选择的子节点,然后在谓词过滤器中,请参阅带有.
的当前School
元素:
for $i in doc("test.xml")/Schools/School[. = $School]/AcademicYear
但是,如果您要迭代同一文档中的元素,为什么不使用已经选择的$School
:
for $i in $School/AcademicYear