我正在尝试使用带有谓词的表达式来显示每个父节点的子节点(练习看起来很愚蠢,因为这是测试数据:最终解决方案必须从其他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行有关。我尝试了两个for循环,只是将一个表达式放在学年标签内,如下所示
{doc(“test.xml”)/ Schools / [School = $ School] / AcademicYear / @ year}
抱歉 - 我确信这很简单。我是初学者!
答案 0 :(得分:0)
谓词前面没有子节点。
选择School
元素时,将其移出谓词之外作为从Schools
元素中选择的子节点,然后在谓词过滤器内部,引用当前School
元素与.
:
for $i in doc("test.xml")/Schools/School[. = $School]/AcademicYear
但是,如果您正在迭代同一文档中的元素,为什么不使用已经选择的$School
:
for $i in $School/AcademicYear