简体   繁体   English

XSL:组合分组和调用模板

[英]XSL: Combining grouping and call-template

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. 我感兴趣地阅读了Web上可用的技术,这些技术可以使用XSL从包含重复项的XML文件中提取项目的唯一列表。

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. 它们分为2类:1)Muenchian方法(例如: http : //www.jenitennison.com/xslt/grouping/ )2)或上一兄弟查找这两个都依赖XPath表达式来选择数据分组。

However, in the XML file that I'm trying to work out, the data is not present "natively" in the XML file. 但是,在我要尝试的XML文件中,XML文件中没有“本机”显示数据。 I am using a xsl:template to compute some aggregated data from my elements. 我正在使用xsl:template从我的元素计算一些聚合的数据。 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,可以带一个记录元素并返回此记录的John评分:示例:

<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. 我可以在xsl:for-each下调用它,以获得约翰评论的详尽列表。 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? 我是否必须使用中介XSL将我的XML文件转换为更结构化的方式? Or can I do in a single step? 还是可以一步一步完成?

Many thanks Gerard 非常感谢Gerard

Hmm... This should be possible using xslt variables and the nodeset method, perhaps something like this: 嗯...使用xslt变量和nodeset方法应该可行,也许是这样的:

<xsl:variable name="_johnsRatings">
   <xsl:apply-templates select="/" mode="johnsRatings" />
</xsl:variable>
<xsl:variable name="johnsRatings" select="msxsl:node-set($_johnsRatings)" />

<xsl:template match="/" mode="johnsRatings">
    <Ratings>
         <xsl:for-each select="/filmsReview/record/data[@name='ratings']">
              <Rating><xsl:call-template name="get_rating" /></Rating>
         </xsl:for-each>
    </Ratings>
</xsl:template>

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... 此时,应该可以使用标准XPath查询来查询$ johnsRatings变量,并且可以使用上面提到的两种方法中的任何一种来从中检索唯一值...

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. 编辑:我不知道您使用的XSLT引擎,我假设您有权访问msxsl:node-set()函数。 However, most XSLT processors have similar methods, so you might have to search around for an equivalent method in your processor 但是,大多数XSLT处理器都有类似的方法,因此您可能必须在处理器中寻找等效的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM