Reputation: 188
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.
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"]. The output should be:
<root>
<node>Text1</node>
<node>Text2Text3</node>
<node>Text4Text5Text6</node>
<node>Text7</node>
</root>
Appreciate all of your Help, Asmo
Upvotes: 0
Views: 945
Reputation: 70598
If you are currently using XSLT 1.0 then you need to read up on a technique called 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. 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.
Try this 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>
Upvotes: 1
Reputation: 167436
Assuming XSLT 2.0 you can use
<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>
Upvotes: 1