通过适当插入标记来格式化xquery的输出

时间:2014-02-23 05:04:47

标签: xml formatting xquery

我已经编写了xquery,但我没有得到正确的输出格式。

这是我的代码

let $doc:=(doc("hamlet.xml"))
for $y in distinct-values($doc/PLAY/ACT/SCENE/SPEECH/SPEAKER/text())
return 
<SPEAKER>
{$y}
<scene>{$doc/PLAY/ACT/SCENE/SPEECH[SPEAKER = $y]/../TITLE}</scene>
</SPEAKER>

它产生的输出格式为

<SPEAKER>BERNARDO<scene>
<TITLE>SCENE I.  Elsinore. A platform before the castle.</TITLE>
<TITLE>SCENE II.  A room of state in the castle.</TITLE>
</scene>
</SPEAKER>
<SPEAKER>FRANCISCO<scene>
<TITLE>SCENE I.  Elsinore. A platform before the castle.</TITLE>
</scene>
</SPEAKER>

我想要的输出应该是

形式
<SPEAKER>BERNARDO
<scene>SCENE I.  Elsinore. A platform before the castle.</scene>
<scene>SCENE II.  A room of state in the castle.</scene>

</SPEAKER>
<SPEAKER>FRANCISCO
<scene>SCENE I.  Elsinore. A platform before the castle.</scene>
</SPEAKER>

我尝试了很多以获得正确的输出,我该怎么做?请帮忙

1 个答案:

答案 0 :(得分:0)

尝试:

let $doc:=(doc("hamlet.xml"))
for $y in distinct-values($doc/PLAY/ACT/SCENE/SPEECH/SPEAKER/text())
return 
<SPEAKER>
{$y}
<scene>{$doc/PLAY/ACT/SCENE/SPEECH[SPEAKER = $y]/../TITLE/text()}</scene>
</SPEAKER>