Convert XML to Data File in Shell Script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Convert XML to Data File in Shell Script
# 8  
Old 05-10-2010
Quote:
Originally Posted by ragha81
...I am trying to create ProductRef.txt and Products.txt just like you did for other three but its not working as expected. Is it because there are some extra tags within those tags.
...
It's not working because the regular expression is incorrect for "ProductRefs". It's correct for "Portfolio", "Family" and "SubFamily" though.

Code:
perl -lne 'BEGIN {undef $/}
            while (/<(Portfolio|Family|SubFamily) productCode="(.*?)".*?value="(.*?)".*?

value="(.*?)".*?<\/(Portfolio|Family|SubFamily)>/sg) {
              if ($1 eq "Portfolio") {push @p, "$2|$3|$4"}
              elsif ($1 eq "Family") {push @f, "$2|$3|$4"}
              elsif ($1 eq "SubFamily") {push @sf, "$2|$3|$4"}
              elsif ($1 eq "ProductRefs") {push @pr, "$2|$3|$4|$4|$5|$6|$7|$8|$9|$10"}
              
            }
           END {if (@p)  {open(F, ">portfolio.txt"); foreach(@p) {print F $_} close(F)}
                if (@f)  {open(F, ">family.txt"); foreach(@f) {print F $_} close(F)}
                if (@sf) {open(F, ">subfamily.txt"); foreach(@sf) {print F $_} close(F)}
                if (@pr) {open(F, ">ProductRefs.txt"); foreach(@pr) {print F $_} close(F)}
           }
           ' CPC.xml

What are the values of the text in red font ? - $1, $5, $6, ..., $10 ?
If you are unable to answer this question then I'd assume that you are not familiar with regular expressions, and in that case, I'd recommend you to get your concepts clear by studying and practising them.

Alternatively, you may want to check out Perl modules related to XML on CPAN.

tyler_durden
# 9  
Old 05-11-2010
Here is a partial XSLT1.* stylesheet which outputs the Portfolio and Family attribute values into separate text files. You can easily extend it to handle the remaining attribute values that you want to extract.
Code:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" >

<xsl:template match="*|text()|@*">
    <xsl:document href="portfolio.txt" method="text">
       <xsl:apply-templates select="//Portfolio" />
    </xsl:document>
    <xsl:document href="family.txt" method="text">
       <xsl:apply-templates select="//Family" />
    </xsl:document>
</xsl:template>

<xsl:template match="Portfolio" >
   <xsl:value-of select="@productCode"/>
   <xsl:text>|</xsl:text>
   <xsl:value-of select="./Attribute[1]/@value"/>
   <xsl:text>|</xsl:text>
   <xsl:value-of select="./Attribute[2]/@value"/>
   <xsl:text>& # 10 ;</xsl:text>
</xsl:text>
</xsl:template>

<xsl:template match="Family" >
   <xsl:value-of select="@productCode"/>
   <xsl:text>|</xsl:text>
   <xsl:value-of select="./Attribute[1]/@value"/>
   <xsl:text>|</xsl:text>
   <xsl:value-of select="./Attribute[2]/@value"/>
   <xsl:text>|</xsl:text>
   <xsl:value-of select="./ParentHierarchy/Item/@modelType"/>
   <xsl:text>& # 10 ;</xsl:text>
</xsl:text>
</xsl:template>

</xsl:stylesheet>

If you are using XSLT2, I suggest you use the <result-document> element instead of the <document> element to construct the multiple output documents.
Code:
$ cat *.txt
F1|Internet Access Services|Active|Portfolio
F2|Local Access Services|Active|Portfolio
P1|Access|Active
P2|Data|Active
P3|Voice|Active
P4|Wireless|Active
$

Note - Remove the spaces between the "& # 10 ;" in your stylesheet. I had to put the spaces in here as the forum code tags eat up certain XSLT constructs.

Last edited by fpmurphy; 05-11-2010 at 11:44 AM..
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Need get data from XML file through shell script..

hi all, here is the sample log file and these errors are repeated in log file.. i need all the repeated time stamp ,severity and message tags needs to print in output file.. through shell script <log-message> <timestamp>2019-03-13T04:52:49.648-05:00</timestamp> <severity>ERROR</severity>... (17 Replies)
Discussion started by: ravi
17 Replies

2. Shell Programming and Scripting

Convert XML to CSV using awk or shell script

Hello, I am working on a part of code where I need a awk or shell script to convert the given XML file to CSV or TXT file. There are multiple xml files and of different structure, so a single script is required for converting data. I did find a lot of solutions in the forum but... (16 Replies)
Discussion started by: Rashmitha
16 Replies

3. Shell Programming and Scripting

How to Parse the XML data along with the URL in Shell Script?

Hi, Can anybody help to solve this. I want to parse some xmldata along with the URL in the Shell. I'm calling the URL via the curl command Given below is my shell script file export... (7 Replies)
Discussion started by: Megala
7 Replies

4. UNIX for Advanced & Expert Users

Convert column data to row data using shell script

Hi, I want to convert a 3-column data to 3-row data using shell script. Any suggestion in this regard is highly appreciated. Thanks. (4 Replies)
Discussion started by: sktkpl
4 Replies

5. Shell Programming and Scripting

convert one form of xml data to other

I would like to convert one form of xml tag data to another <DescriptionList> <DescriptionExt language="en" shortDesc="ITALIAN SAUSAGE SUB" longDesc="" sizeDesc="" smallImage="Pictures\sub-italian-sausage.png" largeImage="" forceImageUpdate="yes" /> ... (1 Reply)
Discussion started by: saisus
1 Replies

6. Shell Programming and Scripting

How to remove xml namespace from xml file using shell script?

I have an xml file: <AutoData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <Table1> <Data1 10 </Data1> <Data2 20 </Data2> <Data3 40 </Data3> <Table1> </AutoData> and I have to remove the portion xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" only. I tried using sed... (10 Replies)
Discussion started by: Gary1978
10 Replies

7. Shell Programming and Scripting

Help with shell script to extract data from XML file

Hello Scripting Gurus, I need help with extracting data from the XML file using shell script. The data is in a large XML and I need to extract the id values of all completedworkflows. Here is a sample of it. Input and output data is also in the attached text files. <wfregistry>... (5 Replies)
Discussion started by: yajaykumar
5 Replies

8. Shell Programming and Scripting

Sample Unix script file to convert .xml to .csv

Dear all, Can you send me a script file the changes .xml to .csv file. Thanks, Srinivasa (4 Replies)
Discussion started by: srinivasaphani
4 Replies

9. Shell Programming and Scripting

extract data from xml- shell script using awk

Hi, This is the xml file that i have. - <front-servlet platform="WAS4.0" request-retriever="SiteMinder-aware" configuration-rescan-interval="60000"> <concurrency-throttle maximum-concurrency="50" redirect-page="/jsp/defaulterror.jsp" /> - <loggers> <instrumentation... (5 Replies)
Discussion started by: nishana
5 Replies

10. Shell Programming and Scripting

Plz Help To convert xml file to text file using bourn shell scripts

If someone out there could help me out with this problem. I would really appreciate it. I am trying to convert xml into text file(fixed length) using Unix Borne shell scripts. My xml file: <root> <header_rec recordtype="00"> <record_id>00</record_id> <country_code>AK></country_code>... (0 Replies)
Discussion started by: ram2s2001
0 Replies
Login or Register to Ask a Question