简体   繁体   中英

XSLT to transform XML based on tag content value

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)

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>

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM