cancel
Showing results for 
Search instead for 
Did you mean: 

Flatten a nested XML - HCI

Balaji_Ponnam
Active Contributor
0 Kudos

Hi Everyone,

Looking for some expert advice on converting a nested XML into a flat XML structure using HCI. Below are the input and output xml templates.

Input

<User>
    <User>
      <matrixManager>
        <User>
          <userId>12345678</userId>
        </User>
        <User>
          <userId>22345678</userId>
        </User>
        <User>
          <userId>32345678</userId>
        </User>
      </matrixManager>
      <userId>user_01</userId>
    </User>
  </User>

Output

<User>
    <User>
      <matrixManager>
          <userId>12345678</userId>
          <userId>22345678</userId>
          <userId>32345678</userId>
      </matrixManager>
      <userId>user_01</userId>
    </User>
  </User>

Regards,

Balaji

Accepted Solutions (1)

Accepted Solutions (1)

Hi Balaji,

You could use below XSLT to convert this.

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
	<xsl:output method="xml" indent="yes" omit-xml-declaration="yes" />
	<xsl:template match="User"> 
		<User>
			<User>
				<matrixManager>
					<xsl:for-each select="User/matrixManager/User">
						<userId><xsl:value-of select="userId" /></userId>
					</xsl:for-each>	
				</matrixManager>
				<userId><xsl:value-of select="User/userId" /></userId>
			</User>
		</User>
	</xsl:template>
</xsl:stylesheet>

Hope this helps !

thanks and regards,

Praveen T

Balaji_Ponnam
Active Contributor
0 Kudos

Thank you Praveen. It worked.

Regards,

Balaji Ponnam

Answers (1)

Answers (1)

MortenWittrock
Active Contributor

Hi Balaji

You can use the method I described here: Thinking in XSLT: Filtering XML elements

I.e. identity transform + add the templates that should behave differently.

Here's the resulting stylesheet:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="3.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:mode on-no-match="shallow-copy"/>
    <xsl:template match="matrixManager/User">
        <xsl:copy-of select="userId"/>
    </xsl:template>
</xsl:stylesheet>

Regards,

Morten

Balaji_Ponnam
Active Contributor

Thank you Morten. Solution worked. It does not allow me to accept both the solutions.

Regards,

Balaji Ponnam