Reputation: 327
I need to concatenate t-Elements within a p-Element. I tried it with a xsl:for-each-group and store the text from the t-elements in a variable. But i cant use this variable outside of the xsl:template.
Source:
<document>
<body>
<p>
<pPr>
<spacing line="286" lineRule="auto"/>
</pPr>
<r>
<t>First:</t>
</r>
</p>
<p>
<pPr>
<numPr>
<ilvl val="0"/>
<numId val="17"/>
</numPr>
<spacing line="286" lineRule="auto"/>
</pPr>
<r>
<t>Second</t>
</r>
<r>
<t space="preserve"> third</t>
</r>
<r>
<t space="preserve"> </t>
</r>
<r>
<t>last.</t>
</r>
</p>
</body>
</document>
Desired outcome:
<document>
<body>
<p>
<pPr>
<spacing line="286" lineRule="auto"/>
</pPr>
<r>
<t>First:</t>
</r>
</p>
<p>
<pPr>
<numPr>
<ilvl val="0"/>
<numId val="17"/>
</numPr>
<spacing line="286" lineRule="auto"/>
</pPr>
<r>
<t>Second third last.</t>
</r>
</p>
</body>
</document>
My try so far:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:mf="http://example.com/mf"
xmlns:w="www.wnamespace.com"
version="2.0"
exclude-result-prefixes="xs mf w">
<xsl:strip-space elements="*"/>
<xsl:preserve-space elements="t"/>
<xsl:output method="xml" encoding="UTF-8" indent="yes" omit-xml-declaration="no"/>
<xsl:template match="@* | node()">
<xsl:copy>
<xsl:apply-templates select="@*, node()"/>
</xsl:copy>
</xsl:template>
<xsl:template match="p">
<xsl:for-each-group select="node()" group-adjacent="boolean(self::r)">
<xsl:variable name="concat-t">
<xsl:sequence select="current-group()[self::r]"></xsl:sequence>
</xsl:variable>
<xsl:choose>
<xsl:when test="self::r">
</xsl:when>
<xsl:otherwise>
<p><xsl:copy-of select="current-group()"/></p>
</xsl:otherwise>
</xsl:choose>
<t><xsl:value-of select="$concat-t"/></t>
</xsl:for-each-group>
</xsl:template>
</xsl:stylesheet>
I am new to XSLT 2.0 and a little overwhelmed. Any help appriciated. XsltFiddle: http://xsltfiddle.liberty-development.net/pNEj9dj
Upvotes: 0
Views: 38
Reputation: 167716
It seems the identity transformation plus two templates
<xsl:template match="p/r[1]">
<p>
<t>
<xsl:value-of select="../r/t"/>
</t>
</p>
</xsl:template>
<xsl:template match="p/r[position() gt 1]"/>
should do the job.
Upvotes: 1