如何将两个元素合并为一个元素? [英] How combine two element in one element?
本文介绍了如何将两个元素合并为一个元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要合并相同的元素,例如:
I want merge elements that are same for example:
<Root>
<row>
<WID>10</WID>
<word>Bob</word>
<SID>2</SID>
<Ah>1</Ah>
</row>
<row>
<WID>5941</WID>
<word>Jany</word>
<SID>2</SID>
<Ah>1</Ah>
</row>
</Root>
结果是:
<span>Bob Jany</span>
我写了这个,但这是错误的:
I write this but it is wrong:
<xsl:choose>
<xsl:when test = "Ah[text()]=Ah[text()]">
<span>
<xsl:value-of select="./word"/>
</span>
</xsl:when>
</xsl:choose>
推荐答案
使用Muenchian分组方法的XSLT 1.0解决方案:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output omit-xml-declaration="yes" indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:key name="kRowByAh" match="row" use="Ah" />
<xsl:template match="row[generate-id()=generate-id(key('kRowByAh', Ah)[1])]">
<span>
<xsl:value-of select="word"/>
<xsl:for-each select="key('kRowByAh', Ah)[position() > 1]">
<xsl:value-of select="concat(' ', word)"/>
</xsl:for-each>
</span>
</xsl:template>
<xsl:template match="text()"/>
</xsl:stylesheet>
当此转换应用于提供的XML文档时:
<Root>
<row>
<WID>10</WID>
<word>Bob</word>
<SID>2</SID>
<Ah>1</Ah>
</row>
<row>
<WID>5941</WID>
<word>Jany</word>
<SID>2</SID>
<Ah>1</Ah>
</row>
</Root>
产生了所需的正确结果:
<span>Bob Jany</span>
这篇关于如何将两个元素合并为一个元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文