XSLT 2.0 中的子字符串和分组问题
我有以下 XML:
<InterSection criteria="Microsoft-ASP">value</Intersection>
<InterSection criteria="Microsoft-MVC">value</Intersection>
<InterSection criteria="HP-MVC">value</Intersection>
我需要在 HTML 页面顶部创建 TOC,因此我需要按条件分组(“-”之前的第一部分)。
这样的不同值
- 我需要像Microsoft
- HP
我的 XSLT 有以下代码
<a>
<xsl:attribute name="href">
<xsl:text>#trigger</xsl:text>
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
<xsl:value-of select="substring-before(@Criteria, ' - ')"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</a>
但不知何故我得到了
- Microsoft
- Microsoft
- HP
而不是 1.微软 2. HP
我在 XSLT 中写了以下内容
<xsl:template match="InterSection" mode="TOC">
<li>
<a>
<xsl:attribute name="href">
<xsl:text>#trigger</xsl:text>
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
<xsl:value-of select="current-grouping-key()"/>
<xsl:for-each select="current-group()"/>
</xsl:for-each-group>
</a>
</li>
I have the following XML:
<InterSection criteria="Microsoft-ASP">value</Intersection>
<InterSection criteria="Microsoft-MVC">value</Intersection>
<InterSection criteria="HP-MVC">value</Intersection>
I need to create TOC at top of my HTML page and so I need to group by criteria (First part before '-').
I need distinct values like
- Microsoft
- HP
My XSLT have following code
<a>
<xsl:attribute name="href">
<xsl:text>#trigger</xsl:text>
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
<xsl:value-of select="substring-before(@Criteria, ' - ')"/>
<xsl:text>
</xsl:text>
</xsl:for-each-group>
</a>
But somehow i am getting
- Microsoft
- Microsoft
- HP
instead of
1. Microsoft
2. HP
I have following written in my XSLT
<xsl:template match="InterSection" mode="TOC">
<li>
<a>
<xsl:attribute name="href">
<xsl:text>#trigger</xsl:text>
<xsl:value-of select="position()"/>
</xsl:attribute>
<xsl:for-each-group select="." group-by="substring-before(@Criteria, ' - ')">
<xsl:value-of select="current-grouping-key()"/>
<xsl:for-each select="current-group()"/>
</xsl:for-each-group>
</a>
</li>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最后要总结一下我将在您的工作中解决的问题,以便使其正常工作(删除了以前的编辑):
criteria使用错误的大小写,
substring
表达式在第二个参数-
周围包含太多空格,current-grouping-key()
来获取当前匹配组的值,InterSection
的父级(Root
)匹配,然后您必须像您一样迭代InterSection
可以使用xsl:for-each
I'm finally going to summarize the problems I would fix in your work in order to make it work (deleted previous edits):
criteria
using the wrong casesubstring
expression contains too much spaces around the second argument-
current-grouping-key()
to get the value of the current matching groupInterSection
(Root
) and then you have to iterate onInterSection
like you would do withxsl:for-each
这毫无意义。
select
属性中的表达式选择单个项目。当选择了多个项目并且我们希望使用特定标准对它们进行分组时,分组就有意义。当单个项目被“分组”时,“结果”始终是同一个项目——这正是您得到的结果。
解决办法:正确选择一组要分组的项目。或者,使用此替代解决方案:
当将此转换应用于以下 XML 文档时(通过更正其严重格式错误而从提供的片段派生):
生成所需的正确结果 >:
This is meaningless. The expression in the
select
attribute selects a single item. Grouping has meaning when there are more than one items selected and we want to group them using certain criteria.When a single item is "grouped" the "result" is always that same item -- this is exactly what you get.
Solution: Select correctly a group of items to be grouped. Or, use this alternative solution:
when this transformation is applied on the following XML document (derived from the provided fragment by corrected its severe malformedness):
the wanted, correct result is produced: