Trying to create a transformation that will map a flat xml with parent/child ids into a hierarchical structure. I have included a simple request and response examples below. Any help would be really appreciated! Thanks!
Input xml:
<?xml version="1.0" encoding="UTF-8"?>
<Rowset>
<Row>
<id>5</id>
<header>grouptile1</header>
<parentid>NULL</parentid>
</Row>
<Row>
<id>1</id>
<header>Grp1 HeaderTile 1-1</header>
<subheader>Grp1 HeaderTile 1-1</subheader>
<parentid>5</parentid>
</Row>
<Row>
<id>2</id>
<header>Grp1 HeaderTile 2-1</header>
<subheader>Grp1 HeaderTile 2-1</subheader>
<parentid>5</parentid>
</Row>
<Row>
<id>6</id>
<header>grouptile2</header>
<parentid>NULL</parentid>
</Row>
<Row>
<id>3</id>
<header>Grp1 HeaderTile 2-1</header>
<subheader>Grp1 HeaderTile 2-1</subheader>
<parentid>6</parentid>
</Row>
<Row>
<id>4</id>
<header>Grp1 HeaderTile 2-2</header>
<subheader>Grp1 HeaderTile 2-2</subheader>
<parentid>6</parentid>
</Row>
</Rowset>
Convert to the Output xml:
<?xml version="1.0" encoding="UTF-8"?>
<tiles>
<grouptile>
<id>5</id>
<header>grouptile1</header>
<tile>
<id>1</id>
<header>Grp1 HeaderTile 1-1</header>
<subheader>Grp1 HeaderTile 1-1</subheader>
</tile>
<tile>
<id>2</id>
<header>Grp1 HeaderTile 2-1</header>
<subheader>Grp1 HeaderTile 2-1</subheader>
</tile>
</grouptile>
<grouptile>
<id>6</id>
<header>grouptile2</header>
<tile>
<id>3</id>
<header>Grp1 HeaderTile 2-1</header>
<subheader>Grp1 HeaderTile 2-1</subheader>
</tile>
<tile>
<id>4</id>
<header>Grp1 HeaderTile 2-2</header>
<subheader>Grp1 HeaderTile 2-2</subheader>
</tile>
</grouptile>
</tiles>
What would be the xslt?
If all you have is parent and child, you could do simply:
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="children" match="Row" use="parentid"/>
<xsl:template match="/Rowset">
<tiles>
<xsl:for-each select="Row[parentid='NULL']">
<grouptile>
<xsl:copy-of select="id | header"/>
<xsl:for-each select="key('children', id)">
<tile>
<xsl:copy-of select="id | header | subheader"/>
</tile>
</xsl:for-each>
</grouptile>
</xsl:for-each>
</tiles>
</xsl:template>
</xsl:stylesheet>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.