[英]XSLT merge multiple nodes
Recently I am trying to mess around with XSLT a little bid and am currently stuck with a problem, that is pretty specific I think. 最近,我试图对XSLT进行一点出价,并且目前遇到了一个问题,我认为这是非常具体的。
I want to combine multiple nodes until a node with a special property appears and then Merge the node text into a single new node. 我想合并多个节点,直到出现具有特殊属性的节点,然后将节点文本合并到单个新节点中。
<paragraph>
<row>
<text>Text1</text>
</row>
<row>
<properties>
<vertAlign val="superscript"/>
</properties>
<text>2</text>
</row>
<row>
<text>Text2</text>
</row>
<row>
<text>Text3</text>
</row>
<row>
<properties>
<vertAlign val="superscript"/>
</properties>
<text>1</text>
</row>
<row>
<text>Text4</text>
</row>
<row>
<text>Text5</text>
</row>
<row>
<text>Text6</text>
</row>
<row>
<properties>
<vertAlign val="superscript"/>
</properties>
<text>1</text>
</row>
<row>
<text>Text7</text>
</row>
<row>
<properties>
<vertAlign val="superscript"/>
</properties>
<text>1</text>
</row>
</paragraph>
In this example I want to collect all row/text until row/properties/vertAlign[@val="superscript"]. 在此示例中,我要收集所有行/文本,直到row / properties / vertAlign [@ val =“ superscript”]。 The output should be:
输出应为:
<root>
<node>Text1</node>
<node>Text2Text3</node>
<node>Text4Text5Text6</node>
<node>Text7</node>
</root>
Appreciate all of your Help, Asmo 感谢您的所有帮助,Asmo
Assuming XSLT 2.0 you can use 假设您可以使用XSLT 2.0
<xsl:template match="paragraph">
<root>
<xsl:for-each-group select="row" group-ending-with="row[properties/vertAlign/@val = 'superscript']">
<node>
<xsl:value-of select="current-group()[position() ne last()]/text" separator=""/>
</node>
</xsl:for-each-group>
</root>
</xsl:template>
If you are currently using XSLT 1.0 then you need to read up on a technique called Muenchian Grouping . 如果您当前正在使用XSLT 1.0,则需要阅读称为Muenchian Grouping的技术。 Assuming there will be always a "properties" row following a normal row at some point, you can "group" your row elements by the unique ID of the first following "properties" row.
假设在某个时刻在正常行之后总是有一个“属性”行,那么您可以通过在“属性”行之后的第一行的唯一ID对行元素进行“分组”。 This means defining a key like so:
这意味着要定义一个键,如下所示:
<xsl:key name="rows"
match="row[not(properties)]"
use="generate-id(following-sibling::row[properties][1])" />
You can then select the first row in each group like so.... 然后,您可以像这样选择每个组中的第一行。
<xsl:apply-templates
select="row[generate-id() = generate-id(key('rows', generate-id(following-sibling::row[properties][1]))[1])]" />
Then, in the template matching the row, you can get the text for each row in the group by again using the key: 然后,在与该行匹配的模板中,您可以再次使用键来获取组中每一行的文本:
<xsl:apply-templates select="key('rows', generate-id(following-sibling::row[properties][1]))/text" />
This will take advantage of XSLT's built-in templates which will iterate over (but not copy) elements, but output text nodes where it find them. 这将利用XSLT的内置模板,该模板将遍历(但不复制)元素,但是在找到它们的地方输出文本节点。
Try this XSLT 试试这个XSLT
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:output indent="yes"/>
<xsl:key name="rows" match="row[not(properties)]" use="generate-id(following-sibling::row[properties][1])" />
<xsl:template match="paragraph">
<root>
<xsl:apply-templates select="row[generate-id() = generate-id(key('rows', generate-id(following-sibling::row[properties][1]))[1])]" />
</root>
</xsl:template>
<xsl:template match="row">
<node>
<xsl:apply-templates select="key('rows', generate-id(following-sibling::row[properties][1]))/text" />
</node>
</xsl:template>
</xsl:stylesheet>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.