XUL 文档模板中的目标 XML 子元素
如果我有一个简单的 XML 文档
<albums>
<album name="Napoleon Bonaparte" artist="male" year="1993" genre="rock">
<song title="test" track="1"/>
<song title="test4" track="2"/>
<song title="test6" track="3"/>
</album>
<album name="Cleopatra" artist="female" year="1993" genre="jazz">
<song title="test1" track="1"/>
<song title="test2" track="2"/>
<song title="test3" track="3"/>
</album>
</albums>
和一个模板,即
<box id="albumList" orient="vertical" datasources="list.xml" ref="*" querytype="xml">
<template>
<query expr="*" />
<action>
<box uri="?" align="left" class="album" orient="vertical">
<box flex="1">
<description class="albumName" value="?name"/>
<label class="albumArtist" value="?artist"/>
</box>
<listbox class="songlist" align="left" collapsed="true">
<listitem uri="?" label="?title"/>
</listbox>
</box>
</action>
</template>
</box>
如何获取
元素以正确显示专辑名称下方每首歌曲的标题?专辑名称/艺术家显示正常,但我不知道如何构建 XML 查询来定位专辑的 song
子项。
If I have a simple XML document of
<albums>
<album name="Napoleon Bonaparte" artist="male" year="1993" genre="rock">
<song title="test" track="1"/>
<song title="test4" track="2"/>
<song title="test6" track="3"/>
</album>
<album name="Cleopatra" artist="female" year="1993" genre="jazz">
<song title="test1" track="1"/>
<song title="test2" track="2"/>
<song title="test3" track="3"/>
</album>
</albums>
And a template that is
<box id="albumList" orient="vertical" datasources="list.xml" ref="*" querytype="xml">
<template>
<query expr="*" />
<action>
<box uri="?" align="left" class="album" orient="vertical">
<box flex="1">
<description class="albumName" value="?name"/>
<label class="albumArtist" value="?artist"/>
</box>
<listbox class="songlist" align="left" collapsed="true">
<listitem uri="?" label="?title"/>
</listbox>
</box>
</action>
</template>
</box>
How can I get the <listbox>
element to properly display the title of each song beneath the album name? The album name/artist display fine, but I can't figure out how to structure the XML query to target the song
children of album.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个非常棘手的问题,谢谢您的提问。 使用递归模板文档提供了一些线索,但似乎缺少重要部分。最终的效果如下:
根据文档的建议,这会将 XML 标记名称分配给
?type
变量,并使用它为album
和 < code>song 标签(通过
和
)。请注意
标记上的uri="?"
,它将该标记指定为需要插入子标记生成的内容的点。但模板引擎还会将子标签的内容直接插入到父标签的内容中 - 这是通过对歌曲规则的parent="listbox"
限制来防止的。我将查询表达式更改为album|song
以确保其他标签立即被忽略,但这并不是绝对必要的。旁注:通常不使用
标签。首选
和
标记,因为它们不需要您单独指定内容方向。That's a pretty tricky question, thank you for asking. Using recursive templates documentation provides some clues but seems to be missing an important part. Here is what works in the end:
As suggested by the documentation, this assigns the XML tag name to the
?type
variable and uses it to generate different content foralbum
andsong
tags (via<rule>
and<where>
). Noteuri="?"
on the<listbox>
tag, it designates that tag as the point where the generated content for the child tags need to be inserted. But the template engine will additionally insert content for the child tags directly into the content of the parent tag - this is prevented by theparent="listbox"
restriction on the rule for songs. I changed the query expression toalbum|song
to make sure that other tags are immediately ignored but this is not strictly necessary.A side-note: normally the
<box>
tag isn't used. The<hbox>
and<vbox>
tags are preferred since they don't require you to specify content orientation separately.