[ask]xml to flat file


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting [ask]xml to flat file
# 1  
Old 04-19-2013
[ask]xml to flat file

dear all,

i need your advice, i have xml file like this
input.xml
Code:
<?xml version="1.0" encoding="UTF-8"?>
<session xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'>
<capture>
	<atribut name="tmp_Filename" value="INTest.rbs"/>
	<atribut name="size_Filename" value="INTest.rbs"/>
	<content destination="TXT_P2P" direction="MFH" encrypted="3DES, with compression" group="192.168.3.252" source="192.168.3.1" sourceMessageSize="170" transaction="3446429495">
		<note>
		<to>Tove</to>
		<from>Jani</from>
		<heading>Reminder</heading>
		<body>Don't forget me this weekend!</body>
		</note>
	</content>
<capture>
<capture>
	<atribut name="tmp_Filename" value="INTest2.rbs"/>
	<atribut name="size_Filename" value="INTest2.rbs"/>
	<content careof="S66955165" content="TXT_IM" destination="192.168.3.1" direction="MTH" encrypted="3DES, with compression" source="BBM_P2P" sourceMessageSize="74" transaction="2339604654">
		<note>
		<to>Jack</to>
		<from>JTove</from>
		<heading>Reminder</heading>
		<body>Ok</body>
		</note>
	</content>
<capture>
</session>

so i want output file like this.

output.txt
Code:
INTest.rbs|INTest.rbs|TXT_P2P|MFH|3DES, with compression|192.168.3.252|192.168.3.1|170|3446429495|Tove|Jani|Reminder|Don't forget me this weekend!
INTest2.rbs|INTest2.rbs|TXT_IM|192.168.3.1|MTH|3DES, with compression|BBM_P2P|74|2339604654|Jack|JTove|Reminder|Ok

how i can filter thats xml like output...any one can explain using awk or sed

thx before.
# 2  
Old 04-19-2013
One solution is 1st to make all attributes to elements.

xml2elements.xsl
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

<xsl:output indent="yes" />

<xsl:strip-space elements="*" />

<xsl:template match="*">
<xsl:copy>
   <xsl:if test="@*">
       <xsl:for-each select="@*">
	  <xsl:element name="{name()}">
	      <xsl:value-of select="." />
	  </xsl:element>
       </xsl:for-each>
   </xsl:if>
   <xsl:apply-templates />
</xsl:copy>
</xsl:template>
</xsl:stylesheet>

Code:
# convert into elements
xsltproc xml2elements.xsl example.xml > example.2.xml

Convert xml to csv
xml2csv.xsl
Code:
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" encoding="iso-8859-1"/>

<xsl:param name="delim" select="';'" />
<xsl:param name="break" select="'
'" /><!-- xA = NL, xD = CR -->
<xsl:param name="colnames" select="'y'"/>

<xsl:strip-space elements="*" />


<xsl:template match="/*/child::*">
<!--headerline-->
<xsl:if test="$colnames = 'y'">
  <xsl:if test="position() = 1">
      <xsl:for-each select="child::*">
	     <xsl:value-of select="name()"/>
             <xsl:if test="position() != last()">
	           <xsl:value-of select="$delim"/>
             </xsl:if>
      </xsl:for-each>
      <!-- hardcode version newline -->
	<!--<xsl:text>
</xsl:text>-->
      <!-- linebreak, nicer -->
      <xsl:value-of select="$break" />
  </xsl:if>
</xsl:if>


<!--dataline-->
<xsl:for-each select="child::*">

<xsl:if test="position() != last()"><xsl:value-of select="normalize-space(.)"/>
	  <xsl:value-of select="$delim" />
</xsl:if>

<xsl:if test="position()  = last()"><xsl:value-of select="normalize-space(.)"/>
	  <xsl:value-of select="$break" />
</xsl:if>

</xsl:for-each>
</xsl:template>

</xsl:stylesheet>

Examples to use:
Code:
# convert into elements
xsltproc xml2elements.xsl example.xml > example.2.xml
# convert into csv, using default parameters
xsltproc xml2csv.xsl example.2.xml
# convert into csv, without column names
xsltproc -param colnames n xml2csv.xsl example.2.xml
# convert using delimiter | and without colnames
xsltproc -param colnames n -stringparam delim "|" xml2csv.xsl example.2.xml

I'm not xslt heavy user, found lot of examples and done those versions.

Sometimes also need to remove/flat/... before making csv. Here is some example to edit xml data.
remove.xsl
Code:
<?xml version="1.0" encoding="ISO-8859-1"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">

   <xsl:output indent="yes" />
   <xsl:strip-space elements="*" />

   <!-- default copy all node -->
   <xsl:template match="@*|node()">
     <xsl:copy>
       <xsl:apply-templates select="@*|node()"/>
     </xsl:copy>
   </xsl:template>

   <!-- template for the document element -->
   <!-- = remove root node -->
   <xsl:template match="/*">
     <xsl:apply-templates select="node()" />
   </xsl:template>

  <!-- remove attributes  <xxx attr="some">value</xxx> -->
  <xsl:template match="@attr"/>
  <xsl:template match="@format"/>
  <xsl:template match="@author"/>


  <!-- remove element  <xxx>value</xxx> -->
  <xsl:template match="xxx"/>
  <xsl:template match="country"/>

  <!-- remove only element childs <xxx><child1>val</child1></xxx> -->
  <xsl:template match="xxx/*"  />

   <!-- move children element value for mother and remove child = more flat, 
           less hierarchical -->
   <xsl:template match="subnode/*">
     <xsl:apply-templates select="node()" />
   </xsl:template>

   <!-- remove some element but leave the child = more flat, less hierarchical -->
   <xsl:template match="some">
     <xsl:apply-templates select="node()" />
   </xsl:template>

   <!-- remove some data based on attribute value -->
   <xsl:template match="product[@id='p6']" />

   <!-- remove comments -->
   <xsl:template match="comment()"/>

   <!-- rename element name=>itemname -->
   <xsl:template match="name">
    <itemname>
      <xsl:apply-templates select="@*|node()"/>
    </itemname>
   </xsl:template>

</xsl:stylesheet>


Or use keyword awk xml to search many example in this forum.
# 3  
Old 04-22-2013
thx for advice,

now i want to output xml like this:

Code:
<capture><atribut name="tmp_Filename" value="INTest.rbs"/><atribut name="size_Filename" value="INTest.rbs"/><note><to>Tove</to><from>Jani</from>/capture>
<capture><atribut name="tmp_Filename" value="INTest2.rbs"/><atribut name="size_Filename" value="INTest2.rbs"/><note><to>Jack</to><from>JTove</from><heading>Reminder</heading></note/></content>

from my input cause i want read line by line with this model

thx before
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Comparing delta values of one xml file in other xml file

Hi All, I have two xml files. One is having below input <NameValuePair> <name>Daemon</name> <value>tcp:7474</value> </NameValuePair> <NameValuePair> <name>Network</name> <value></value> </NameValuePair> ... (2 Replies)
Discussion started by: sharsour
2 Replies

2. Shell Programming and Scripting

XML Parsing having optional tags into flat file

In xml file i have following data where some tags like<ChrgBr> may not be present in every next file. So i want these values to be stored in some variable like var1="405360,00" , var2="DEBT" and so on ,but if <ChrgBr> tag has no value or is absent var2 should have space like var2=" " so that i... (1 Reply)
Discussion started by: sandipgawale
1 Replies

3. Shell Programming and Scripting

Help required in Splitting a xml file into multiple and appending it in another .xml file

HI All, I have to split a xml file into multiple xml files and append it in another .xml file. for example below is a sample xml and using shell script i have to split it into three xml files and append all the three xmls in a .xml file. Can some one help plz. eg: <?xml version="1.0"?>... (4 Replies)
Discussion started by: ganesan kulasek
4 Replies

4. Shell Programming and Scripting

Help with converting XML to Flat file

Hi Friends, I want to convert a XML file to flat file. Sample I/p: <?xml version='1.0' encoding='UTF-8' ?> <DataFile xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance' contactCount='4999' date='2012-04-14' time='22:00:14' xsi:noNamespaceSchemaLocation='gen .xsd'> <Contact... (3 Replies)
Discussion started by: karumudi7
3 Replies

5. Shell Programming and Scripting

Reading XML data in a FLAT FILE

I have a requirement to read the xml file and split the files into two diffrent files in Unix shell script. Could anyone please help me out with this requirement. Sample file --------------- 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Information... (3 Replies)
Discussion started by: kmanivan82
3 Replies

6. Shell Programming and Scripting

To read a flat file containing XML data

I have a file something like this:aaaa.xml content of the file is 0,<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <storeInformation xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <s> <BRANCH_NO>3061</BRANCH_NO> <BRANCH_NAME>GREEN EXPRESS</BRANCH_NAME> ... (4 Replies)
Discussion started by: kmanivan82
4 Replies

7. Shell Programming and Scripting

Converting a flat file in XML

Hello Friends, I am new to UNIX shell scripting. Using bash....Could you please help me in converting a flat file into an XML style output file. Flat file: (Input File entries looks like this) John Miller: 617-569-7996:15 Bunting lane, staten Island, NY: 10/21/79: 60600 The... (4 Replies)
Discussion started by: humkhn
4 Replies

8. Programming

compare XML/flat file with UNIX file system structure

Before i start doing something, I wanted to know whether the approach to compare XML file with UNIX file system structure. I have a pre-configured file(contains a list of paths to executables) and i need to check against the UNIX directory structure. what are the various approches should i use ? I... (6 Replies)
Discussion started by: shafi2all
6 Replies

9. Shell Programming and Scripting

XML to flat file

Hi all, can u please help me in converting any given XML file to flat file. thanks in advance. -bali (2 Replies)
Discussion started by: balireddy_77
2 Replies

10. UNIX for Advanced & Expert Users

XML to flat file in Unix

Hello, How can I take a file in XML format and convert it to a comma separated format? Is there any scripts or programs that can do this for Unix? I tried surfing the net for such an application, but everything seems to be for Windows OS. Any help or suggestions are greatly appreciated. ... (2 Replies)
Discussion started by: oscarr
2 Replies
Login or Register to Ask a Question