Home > Mobile >  Extracting data from xml to a text file
Extracting data from xml to a text file

Time:12-16

I am trying to extract the data from xml file and write it to a text file. I have a xml file and xsl file but I am not getting the output as expected.

XML

  <?xml version="1.0" encoding="utf-8"?>
    <MessageStart>
        <Header>
                <College>University of Data</College>
                <State>AAA</State>
                <Country>BBB</Country>
        </Header>
        <Body>
            <Department>
                    <DeptNum>001</DeptNum>
                    <DeptName>Computers</DeptName>
                    <DeptTotal>100</DeptTotal>
            </Department>
            <Department>
                    <DeptNum>002</DeptNum>
                    <DeptName>IT</DeptName>
                    <DeptTotal>200</DeptTotal>
            </Department>
            <Department>
                    <DeptNum>003</DeptNum>
                    <DeptName>Civil</DeptName>
                    <DeptTotal>300</DeptTotal>
            </Department>   
        </Body>
    </MessageStart>

XSLT

 <?xml version="1.0" encoding="utf-8"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
        <xsl:output method="text" encoding="utf-8" omit-xml-declaration="yesy" />
        <xsl:template match="/">
            <xsl:for-each select="MessageStart">
                <xsl:value-of select="Header/College"/><xsl:text>|</xsl:text>
            <xsl:for-each select="Body/Department">
                 <xsl:value-of select="DeptNum"/><xsl:text>|</xsl:text>
                 <xsl:value-of select="DeptName"/><xsl:text>|</xsl:text>
                 <xsl:value-of select="DeptTotal"/>
                 <xsl:text>&#10;</xsl:text>
            </xsl:for-each>
            </xsl:for-each>
        </xsl:template>
    </xsl:stylesheet>


enter code here
Expected Output:

University of Data|AAA|BBB|001|Computers|100 University of Data|AAA|BBB|002|IT|200 University of Data|AAA|BBB|003|Civil|300

CodePudding user response:

I think you want:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>

<xsl:template match="/MessageStart">
    <xsl:variable name="college" select="Header/College"/>
    <xsl:for-each select="Body/Department">
         <xsl:value-of select="$college"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptNum"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptName"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptTotal"/>
         <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>
    
</xsl:stylesheet>   

If you have multiple values in the header then use:

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="utf-8"/>

<xsl:template match="/MessageStart">
    <xsl:variable name="header">
         <xsl:value-of select="Header/College"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="Header/State"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="Header/Country"/>
    </xsl:variable>
    <xsl:for-each select="Body/Department">
         <xsl:value-of select="$header"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptNum"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptName"/>
         <xsl:text>|</xsl:text>
         <xsl:value-of select="DeptTotal"/>
         <xsl:text>&#10;</xsl:text>
    </xsl:for-each>
</xsl:template>
    
</xsl:stylesheet> 
  • Related