0

How to get information of all descendant child parts.

I wish to convert xml to csv. I am just trying out a generic solution, if siblings then use "," . If no sibling then then use "|" as delimiter.

For e.g. if I have input as

<?xml version="1.0" encoding="ISO-8859-1"?>
<?xml-stylesheet type="text/xsl" href="Q2.xsl"?>
<data>
    <ShoppingMalls>
      <Shop>
       <ShopNumber>1</ShopNumber>
       <LineNumber>1</LineNumber>
       <Address>
         <Street> East </Street>
         <PinCode>1 </PinCode>
       </Address>
      </Shop>
      <Shop>
       <ShopNumber>2</ShopNumber>
       <LineNumber>2</LineNumber>
       <Address>
         <Street> West </Street>
         <PinCode>1 </PinCode>
       </Address>                  
      </Shop>                 
    </ShoppingMalls>
    <Inventory>
      <Line>
       <LineNumber>line</LineNumber>
       <Description>desc</Description>
       <Matrix>quan</Matrix>
       <Matrix>quan1</Matrix> <!-- added -->
       <Date>date</Date>
      </Line>
      <Line>
       <LineNumber>1</LineNumber>
       <Description>Oak chairs</Description>
       <Matrix>5</Matrix>
       <Matrix>20</Matrix> <!-- added -->
       <Matrix>16</Matrix> <!-- added -->
       <Date>31 Dec 2004</Date>
      </Line>
      <Line>
       <LineNumber>2</LineNumber>
       <Description>Dining tables</Description>
       <Matrix>
        <Module1>
           <Module11> 234</Module11>
           <Module11> 333</Module11> <!-- could be nested till any level i.e. might have any number of descendants-->          
        </Module1>
        <SubComp>300</SubComp>
        <SubComp>500</SubComp>
        <SubComp>800</SubComp>                  
        </Matrix>
       <Date>31 Dec 2004</Date>
      </Line>
      <Line>
       <LineNumber>3</LineNumber>
       <Description>Folding chairs</Description>
       <Matrix>4</Matrix>
       <Date>29 Dec 2004</Date>
      </Line>
      <Line>
       <LineNumber>4</LineNumber>
       <Description>Couch</Description>
       <Matrix>1</Matrix>
       <Date>31 Dec 2004</Date>
      </Line>
     </Inventory>
</data>

Then I wish to have output as below...This is to be imported to a csv file

            ShoppingMalls Information
            1|1|East,1
            2|2|West,1
            Line Information
            line|desc|quan,quan1|date
            1|Oak chairs|5,20,16|31 Dec 2004
            2|Dining tables| 234,333|300,500,800|31 Dec 2004
            3|Folding chairs|4|29 Dec 2004
            4|Couch|1|31 Dec 2004

I want to navigate nodes till any depth. If there are attributes with same name then .then I wish to concatenate using "," ( siblings or Multi-value attrinbutes ) For other nodes, the delimiter shuld be "|" between attributes and newline as separator between Line nodes. I have used the solution from earlier If there are multiple values in the node,then concatenate these into a single string. I wish to use xslt 1.0 My solution for which i seek your help.

 <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text" indent="yes" encoding="ISO-8859-1" omit-xml-declaration="yes"/>
    <xsl:strip-space  elements="*"/>

    <xsl:template name="Newline"><xsl:text>
    </xsl:text></xsl:template>

    <xsl:template match="/data/Inventory">
            <xsl:text>ShoppingMalls Information</xsl:text>
            <xsl:call-template name="Newline" />
            <xsl:apply-templates select="Line"/>
    </xsl:template>

    <xsl:template match="data/ShoppingMalls">
            <xsl:text>ShoppingMalls Information</xsl:text>
            <xsl:call-template name="Newline" />            
            <xsl:apply-templates select="Shop"/>
    </xsl:template>
    <!-- the same solution for Inventory/Line  can be used for ShoppingMalls/Shop 
    -->


    <xsl:template match="Line">
      <xsl:for-each select="*">  <!-- somehow I belive the ndes can be identfied over here -->


      <!--
      I THINK THIS BLOCK WILL HAVE SOLUTION
      BEGIN 
         <xsl:if test="count(./child::*) &gt; 1">

            <xsl:when test="name(./child/following-sibling::*)=name(./child::*)" >
                <xsl:text>,</xsl:text>
            </xsl:when>  
            <xsl:otherwise>
                <xsl:if test="position() != last()">
                 <xsl:text>|</xsl:text>
               </xsl:if>
            </xsl:otherwise>         

         </xsl:if> 
      END
      -->


            <xsl:choose>
            <!-- below blocks gives a simple way to identify siblings, but it not extensible -->

            <xsl:when test="name(./following-sibling::*)=name(.)" >
               <xsl:text>,</xsl:text>
            </xsl:when>   
            <xsl:otherwise>
                <xsl:if test="position() != last()">
                <xsl:value-of select="'|'"/>
               </xsl:if>
            </xsl:otherwise>

            </xsl:choose>


     </xsl:for-each>
      <xsl:text>&#xd;&#xa;</xsl:text>
     </xsl:template>

    </xsl:stylesheet>
Community
  • 1
  • 1
kapil.murarkar
  • 247
  • 1
  • 3
  • 6

0 Answers0