Reputation: 8484
I want to write an XSLT that can transform an XML like this:
<Companies>
<Company>
<CompanyId>1</CompanyId>
<CompanyType>1</CompanyType>
</Company>
<Company>
<CompanyId>2</CompanyId>
<CompanyType>2</CompanyType>
<ParentCompanyId>1</ParentCompanyId>
</Company>
</Companies>
Into an XML like this:
<Companies>
<Company>
<CompanyId>1</CompanyId>
<CompanyType>1</CompanyType>
<Company>
<CompanyId>2</CompanyId>
<CompanyType>2</CompanyType>
</Company>
</Company>
</Companies>
How can I do such thing?
(The reason I am including the company type is that I can directly derive the depth of the node from it, meaning all the Company
nodes of depth X will be of type Y)
Upvotes: 0
Views: 45
Reputation: 117100
This is easy to do using a key to select the children of the current Company. This works recursively for any depth of nesting. The CompanyType
is not required.
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="child-by-parent" match="Company" use="ParentCompanyId" />
<xsl:template match="/Companies">
<xsl:copy>
<!-- select progenitors -->
<xsl:apply-templates select="Company[not(ParentCompanyId)]" />
</xsl:copy>
</xsl:template>
<xsl:template match="Company">
<xsl:copy>
<xsl:copy-of select="CompanyId | CompanyType"/>
<!-- select children -->
<xsl:apply-templates select="key('child-by-parent', CompanyId)"/>
</xsl:copy>
</xsl:template>
</xsl:stylesheet>
Upvotes: 2