XSL:组合分组和调用模板
我饶有兴趣地阅读了网络上可用的技术,这些技术可使用 XSL 从包含重复项的 XML 文件中提取唯一的项目列表。
这些范围分为两类: 1) Muenchian 方法(示例:http://www.jenitennison.com/xslt/grouping/< /a>) 2) 或上一个兄弟姐妹查找 它们都依赖于 XPath 表达式来选择要分组的数据。
但是,在我尝试计算的 XML 文件中,数据并不“本地”存在于 XML 文件中。 我正在使用 xsl:template 来计算元素中的一些聚合数据。 我想根据汇总数据进行分组。
例如,我有:
<filmsreview>
<record><data name='movie'>Star Wars</data><data name='ratings'>John:Good, Mary:Good</data></record>
<record><data name='movie'>Indiana Jones</data><data name='ratings'>John:Good, Mary:Bad, Helen:Average</data></record>
<record><data name='movie'>Titanic</data><data name='ratings'>John:Bad, Helen:Good</data></record>
</filmsreview>
我知道数据的结构并不完美,通过创建子元素我可以做一些更容易的事情,但我无法轻松更改数据源,所以让我们将此视为一个挑战。
我想回顾一下我对约翰的独特评价: 约翰的评价: 好的 不好
我有一个 xsl:template 可以采用记录元素并返回约翰对此记录的评分: 示例:
<xsl:template name="get_rating">
<xsl:param name="reviewer" />
<!-- I use some string manipulation, and xsl:value-of to return the review for John-->
</xsl:template>
我可以在 xsl:for-each 下调用它来获取 John 评论的详尽列表。 但我无法将此调用与任何方法结合起来以获得唯一值。
我是否必须使用中间 XSL 将 XML 文件转换为更结构化的方式? 或者我可以一步完成吗?
非常感谢 杰拉德
I've read with interest the techniques available on the web to extract a unique list of items from a XML file containing duplicates using XSL.
These range into 2 categories:
1) The Muenchian method (example: http://www.jenitennison.com/xslt/grouping/)
2) Or the previous-sibling look-up
These both rely on an XPath expression to select the data to group by.
However, in the XML file that I'm trying to work out, the data is not present "natively" in the XML file. I am using a xsl:template to compute some aggregated data from my elements. And I would like to group based on the aggregated data.
For example I have:
<filmsreview>
<record><data name='movie'>Star Wars</data><data name='ratings'>John:Good, Mary:Good</data></record>
<record><data name='movie'>Indiana Jones</data><data name='ratings'>John:Good, Mary:Bad, Helen:Average</data></record>
<record><data name='movie'>Titanic</data><data name='ratings'>John:Bad, Helen:Good</data></record>
</filmsreview>
I know that the structuration of data is not perfect and that by creating sub-elements I could do something easier, but I cannot change the data source easily, so let's take this as a challenge.
And I would like to build a recap where I have John's distinct ratings:
John's ratings:
Good
Bad
I have a xsl:template that can take a record element and return John's rating for this record:
Example:
<xsl:template name="get_rating">
<xsl:param name="reviewer" />
<!-- I use some string manipulation, and xsl:value-of to return the review for John-->
</xsl:template>
I can just call it under a xsl:for-each to get the exhaustive list of John's review. But I cannot combine this call with any of the methods to get unique values.
Do I have to use an intermediary XSL to convert my XML file to a more structured way? Or can I do in a single step?
Many thanks
Gerard
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯...这应该可以使用 xslt 变量和nodeset 方法,也许是这样的:
此时,应该可以使用标准 XPath 查询来查询 $johnsRatings 变量,并且您可以使用您想要的两种方法中的任何一种。上面提到从中检索唯一值...
希望有帮助
编辑:
我不知道您使用的是什么 XSLT 引擎,我假设您可以访问 msxsl:node-set() 函数。 但是,大多数 XSLT 处理器都有类似的方法,因此您可能需要在处理器中搜索等效方法
Hmm... This should be possible using xslt variables and the nodeset method, perhaps something like this:
At this point, it should be possible to query the $johnsRatings variable using standard XPath queries, and you can use either of the two methods you mentioned above to retrieve unique values from it...
Hope that helps
EDIT:
I don't know what XSLT engine you are using, I assumed you have access to the msxsl:node-set() function. However, most XSLT processors have similar methods, so you might have to search around for an equivalent method in your processor