Flatten Nested XML/hierarchy using XSLT Transformation -


i have nested hierarchial xml structure flattened using xsl transformation. following scenario.

<company>   <managers>     <manager>        <name>matt</name>        <id>1</id>        <manager>           <name>joe</name>           <id>11</id>           <manager>           <name>dave</name>           <id>111</id>        </manager>        </manager>     </manager>     <manager>         <name>mike</name>           <id>2</id>>     </manager>   </managers> </company> 

result:

matt 1 joe 11 dave 111 mike 2 

a better alternative via @mathias mueller,

<?xml version="1.0" encoding="utf-8" ?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">     <xsl:output method="text" encoding="utf-8"/>      <xsl:template match="manager">         <xsl:value-of select="name" />         <xsl:text>: </xsl:text>         <xsl:value-of select="id" />         <xsl:text> </xsl:text>         <xsl:apply-templates/>     </xsl:template>         <xsl:template match="text()"/>  </xsl:transform> 

why better: more control text output (assuming want output plaintext format). more xslt template oriented, tends more extensible , maintainable having for-each loops hanging around.

using descendant-or-self axis:

<?xml version="1.0" encoding="utf-8" ?> <xsl:transform xmlns:xsl="http://www.w3.org/1999/xsl/transform" version="1.0">     <xsl:output method="text" doctype-public="xslt-compat" omit-xml-declaration="yes" encoding="utf-8" indent="yes" />      <xsl:template match="//managers">        <xsl:for-each select="descendant-or-self::manager">         <xsl:value-of select="name" />: <xsl:value-of select="id" /><xsl:text> </xsl:text>       </xsl:for-each>     </xsl:template>      </xsl:transform> 

output:

  matt: 1 joe: 11 dave: 111 mike: 2  

http://xsltransform.net/nc4nzqb


Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -