Reputation: 17
I have a bit of XML which I need to add a parent node to i.e. node1, node2, node3 etc. This I then need to also group with other nodes:
Original XML:
<parentnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.3">
</childnode>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</parentnode>
Desired XML:
<parentnode>
<row0>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</row0>
<row1>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
<childnode attribute="option.a.b.3">
</childnode>
</row1>
<row2>
<childnode attribute="option.a.b.1">
</childnode>
<childnode attribute="option.a.b.2">
</childnode>
</row2>
</parentnode>
option.a.b.* the * could be any number I just need it to start a new row every time option.a.b.1 appears. I'm not even sure if this is this possible in XSLT?
Upvotes: 1
Views: 229
Reputation: 117073
I just need it to start a new row every time option.a.b.1 appears. I'm not even sure if this is this possible in XSLT?
XSLT - even XSLT 1.0 - is a Turing-complete language, so yes, it is possible. If you are using XSLT 1.0, try:
XSLT 1.0
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
<xsl:key name="k" match="childnode[not(@attribute='option.a.b.1')]" use="generate-id(preceding-sibling::childnode[@attribute='option.a.b.1'][1])" />
<xsl:template match="/parentnode">
<xsl:copy>
<xsl:for-each select="childnode[@attribute='option.a.b.1']">
<xsl:element name="row{position()-1}">
<xsl:copy-of select=". | key('k', generate-id())"/>
</xsl:element>
</xsl:for-each>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Note: you could have figured this out by adapting the answer I gave you here: https://stackoverflow.com/a/26397156/3016153
Upvotes: 1
Reputation: 167696
Assuming an XSLT 2.0 processor like Saxon 9 or XmlPrime or AltovaXML you can use
<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="2.0">
<xsl:output indent="yes"/>
<xsl:strip-space elements="*"/>
<xsl:template match="parentnode">
<xsl:copy>
<xsl:for-each-group select="childnode" group-starting-with="childnode[@attribute = 'option.a.b.1']">
<row>
<xsl:copy-of select="current-group()"/>
</row>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
</xsl:transform>
I have intentionally not number the row elements as it results in a poor format in my view, if you really need that then use
<xsl:template match="parentnode">
<xsl:copy>
<xsl:for-each-group select="childnode" group-starting-with="childnode[@attribute = 'option.a.b.1']">
<xsl:element name="row{position() - 1}">
<xsl:copy-of select="current-group()"/>
</xsl:element>
</xsl:for-each-group>
</xsl:copy>
</xsl:template>
Upvotes: 1