XSLT:将相邻元素与同类分组
我是XSLT的新手,我陷入了“相邻元素”问题上。 我的XML输入遵循架构:
<body>
<aside class="1">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
<p>...</p>
<img .../>
<aside class="2">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
</body>
我需要的(也无法实现)是一个输出,其中相邻 旁边
将同一类分组在一起。当之无愧的输出就是这样:
<body>
<tag1 class="1">everything contained in the grouped tags</tag1>
<tag2 class="2">everything contained in the grouped tags</tag2>
<p>...</p>
<img.../>
<tag2 class="2">everything contained in the grouped tags</tag2>
</body>
可以使用变量来管理新标签,这很好。但是如何获得此输出?到目前为止,我已经获得了最好的结果:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:for-each-group select="aside" group-adjacent="@class">
<xsl:variable name="grouping_tag">
<xsl:choose>
<xsl:when test="@class = '1'">tag1</xsl:when>
<xsl:when test="@class = '2'">tag2</xsl:when>
<xsl:otherwise>tagX</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$grouping_tag}" >
<xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
<xsl:for-each select="current-group()/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
此解决方案确实确实将相邻的与同一类分组,但是在输出中,我丢失了其中未包含的所有内容(例如,<代码> p
,img
)。我需要在其他所有位置上进行其他一切。
有人可以帮我吗?
I'm new to XSLT and I'm stuck on the "grouping adjacent elements" problem.
My XML input follows the schema:
<body>
<aside class="1">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
<p>...</p>
<img .../>
<aside class="2">
some tags + text
</aside>
<aside class="2">
some tags + text
</aside>
</body>
What I need (and can't achieve) is an output in which the adjacent aside
with the same class are grouped together. The desidered output would be like this:
<body>
<tag1 class="1">everything contained in the grouped tags</tag1>
<tag2 class="2">everything contained in the grouped tags</tag2>
<p>...</p>
<img.../>
<tag2 class="2">everything contained in the grouped tags</tag2>
</body>
The new tags can be managed using variables, and that's fine. But how do I obtain this output? So far, I've obtained the best results with this:
<xsl:template match="@*|node()">
<xsl:copy>
<xsl:apply-templates select="@*|node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="body">
<xsl:for-each-group select="aside" group-adjacent="@class">
<xsl:variable name="grouping_tag">
<xsl:choose>
<xsl:when test="@class = '1'">tag1</xsl:when>
<xsl:when test="@class = '2'">tag2</xsl:when>
<xsl:otherwise>tagX</xsl:otherwise>
</xsl:choose>
</xsl:variable>
<xsl:element name="{$grouping_tag}" >
<xsl:attribute name="class"><xsl:value-of select="@class"/></xsl:attribute>
<xsl:for-each select="current-group()/*">
<xsl:copy>
<xsl:apply-templates select="@*"/>
<xsl:apply-templates/>
</xsl:copy>
</xsl:for-each>
</xsl:element>
</xsl:for-each-group>
</xsl:template>
This solution does indeed group adjacent aside
with the same class, but in the output I lost everything that is not contained in them (for example, p
, img
). And I need to mantain everything else, and in the same position.
Can someone help me?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将使用
select =“*” group-adjacent =“。 1]
要检查组的旁边元素并包裹它们,否则将组通过申请表推动。I would use
select="*" group-adjacent=". instance of element(aside), @class" composite="yes"
and then you can testcurrent-group()[1]
to check the group hasaside
elements and wrap them, otherwise push the group through apply-templates.也许是这样?
Something like this, perhaps?
我想您可以做类似:
ps的事情:如果
一边,那么您可以将其缩短为:
I guess you could do something like:
P.S. If
aside
elements are the only child elements ofbody
that have a (non-empty)class
attribute, then you could shorten this to: