Help needed XML Field Extraction


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Help needed XML Field Extraction
# 8  
Old 09-13-2010
[QUOTE=raghunsi;302452810]

Code:
gzmore dealerlistservice_Log[1-5]_2010*.log.gz| grep -i Error* | nawk -RS'<ns1:providerErrorCode>^\*|</ns1:providerErrorCode>' '{print $NF}'

Try:

Code:
gzmore dealerlistservice_Log[1-5]_2010*.log.gz|nawk '/<ns1:providerError.*>.*<\/ns1:providerError.*>/'

# 9  
Old 09-14-2010
But even this is not pulling the right name space from the XML.So here is the output of it.

Code:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <SOAP-ENV:Header>
        <m:wsMessageHeader xmlns:m="http://integration.xyz.com/common/header/WSMessageHeader/v2">
            <m:trackingMessageHeader>
                <m:applicationId>XNM</m:applicationId>
                <m:applicationUserId>XNM</m:applicationUserId>
                <m:consumerId/>
                <m:messageId>1234</m:messageId>
                <m:conversationId/>
                <m:timeToLive>100</m:timeToLive>
                <m:replyCompletionCode>0</m:replyCompletionCode>
                <m:messageDateTimeStamp>2009-08-29T10:06:34-05:00</m:messageDateTimeStamp>
            </m:trackingMessageHeader>
        </m:wsMessageHeader>
    </SOAP-ENV:Header>
    <SOAP-ENV:Body xmlns:tns="http://integration.xyz.com/interfaces/queryLocation/v1/queryLocation.xsd" xmlns:ns1="http://integration.sprint.com/common/Error.xsd">
        <SOAP-ENV:Fault>
            <faultcode>ClientError.700</faultcode>
            <faultstring>Data Not Found</faultstring>
            <faultactor>5P</faultactor>
            <detail>
                <ns1:errorDetailItem>
                    <ns1:providerError>
                        <ns1:providerErrorCode>3</ns1:providerErrorCode>
                        <ns1:providerErrorText>No Results Found</ns1:providerErrorText>
                    </ns1:providerError>
                </ns1:errorDetailItem>
            </detail>
        </SOAP-ENV:Fault>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

From the above Output I want only the below name space, not the whole XML Tree.

Code:
<faultcode>ClientError.700</faultcode>
<faultstring>Data Not Found</faultstring>

# 10  
Old 09-14-2010
It works:
Code:
# awk '/<fault(c|s).*>.*<\/fault.*>/' file
            <faultcode>ClientError.700</faultcode>
            <faultstring>Data Not Found</faultstring>

# 11  
Old 09-14-2010
So here is the output of the syntax you provided

Code:
awk '/<fault(c|s).*>.*<\/fault.*>/'dealerlistservice_Logs[1-5]_2010*.log.gz
awk: record `^8K        l@êb}Á8çH1ßÑÃ[²ü¶5È{
                                             record number 2207

awk '/<fault(c|s).*>.*<\/fault.*>/' dealerlistservice_Logs[1-5]
awk: record `ERROR|2010-09-14 00:...' too long
 record number 5

no luck.
# 12  
Old 09-14-2010
Code:
 awk '/<ns1:providerError>/,/<\/ns1:providerError>/' infile |egrep "providerErrorCode|providerErrorText"

                        <ns1:providerErrorCode>Error.800</ns1:providerErrorCode>
                        <ns1:providerErrorText>Failed to establish a backside connection</ns1:providerErrorText>
                        <ns1:providerErrorCode>Error.800</ns1:providerErrorCode>
                        <ns1:providerErrorText>Communicating  error</ns1:providerErrorText>

# 13  
Old 09-18-2010
It is usually best to use an XSLT processor to transform an XML document. Here is a stylesheet which emits the required information:
Code:
<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0"
  xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:ns1="http://integration">

  <xsl:output method="xml" omit-xml-declaration="yes" />

  <xsl:template match="/">
     <xsl:apply-templates select="//ns1:errorDetailItem" />
  </xsl:template>

  <xsl:template match="//ns1:errorDetailItem" >
     <xsl:apply-templates select="//ns1:providerError[1]/ns1:providerErrorCode" />
     <xsl:apply-templates select="//ns1:providerErrorText" />
  </xsl:template>

  <xsl:template match="//ns1:providerErrorCode" >
     <xsl:element name="{local-name()}">
        <xsl:value-of select="."/>
     </xsl:element>
     <xsl:text>
</xsl:text>
  </xsl:template>

  <xsl:template match="//ns1:providerErrorText" >
     <xsl:element name="{local-name()}">
        <xsl:value-of select="."/>
     </xsl:element>
     <xsl:text>
</xsl:text>
  </xsl:template>

</xsl:stylesheet>

Note that this stylesheet (soap.xsl) does not emit the "ns1" namespace prefixes because to do so would cause the relevant namespace declarations to also be emitted. Instead I use the sed utility to "add" back in the "ns1" namespace prefix to each element.

Here I use the xsltproc XSLT1.0 processor to process the sample (soap.xml) you provided in your initial post:
Code:
$ xsltproc soap.xsl soap.xml | sed -e 's/provider/ns1:provider/g'
<ns1:providerErrorCode>Error.800</ns1:providerErrorCode>
<ns1:providerErrorText>Failed to establish a backside connection</ns1:providerErrorText>
<ns1:providerErrorText>Communicating  error</ns1:providerErrorText>

# 14  
Old 09-20-2010
Thanks a lot murphy,

But one thing i need to know, since our XML are in the .log format. Do you want us to change the format to xml and need to run the above syntax to get the desired output.

If we need to convert to xml, what procedure do i need to use.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Help with tag value extraction from xml file based on a matching condition

Hi , I have a situation where I need to search an xml file for the presence of a tag <FollowOnFrom> and also , presence of partial part of the following tag <ContractRequest _LoadId and if these 2 exist ,then extract the value from the following tag <_LocalId> which is "CW2094139". There... (2 Replies)
Discussion started by: paul1234
2 Replies

2. Shell Programming and Scripting

Help with XML tag value extraction based on condition

sample xml file part <?xml version="1.0" encoding="UTF-8"?><ContractWorkspace xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" _LoadId="export_AJ6iAFmh+pQHq1" xsi:noNamespaceSchemaLocation="ContractWorkspace.xsd"> <_LocalId>CW2218471</_LocalId> <Active>true</Active> ... (3 Replies)
Discussion started by: paul1234
3 Replies

3. Shell Programming and Scripting

Help with XML tag value extraction based on matching condition

sample xml file part <DocumentMinorVersion>0</DocumentMinorVersion> <DocumentVersion>1</DocumentVersion> <EffectiveDate>2017-05-30T00:00:00Z</EffectiveDate> <FollowOnFrom> <ContractRequest _LoadId="export_AJ6iAFoh6g0rE9"> <_LocalId>CRW2218451</_LocalId> ... (4 Replies)
Discussion started by: paul1234
4 Replies

4. Shell Programming and Scripting

Data extraction from .xml file

Hello, I'm attempting to extract 13 digit numbers beginning with 978 from a data file with the following command: awk '{ for(i=1;i<=NF;i++) if($i ~ /^978/) print $i; }' datafile > outfile This typically works. However, the new data file is an .xml file, and this command is no longer working... (6 Replies)
Discussion started by: palex
6 Replies

5. Shell Programming and Scripting

Regular Expression needed for the xml

I would like to match the regular below xml snippet by using the following grep command. I want match the lines as well could some please help me .. grep -il "<tokenValue>.*.</tokenValue>\n...*.amey.*.</userName>" ... (6 Replies)
Discussion started by: ameyrk
6 Replies

6. Red Hat

How to get a particular field from xml file?

i have a xml file and in that input file path is given. how to fetch that input file path using shell script. (4 Replies)
Discussion started by: ramsavi
4 Replies

7. Shell Programming and Scripting

XML parsing using nawk help needed

i need one help, below is one more xml file with diff pattern i tried it but dint get it , iam sure its a peice of cake for you guys. <xn:MeContext id="LSVLKY001"> <xn:ManagedElement id="1"> <un:RncFunction id="1"> <un:UtranCell... (2 Replies)
Discussion started by: tech_frk
2 Replies

8. Shell Programming and Scripting

How to get a field from an XML?

We have an input XML of the following form <item> <account>1234</account> <id>345</id> </item> How do we get the value of <account> tag (1234) using UNIX ? (6 Replies)
Discussion started by: Jassz
6 Replies

9. Shell Programming and Scripting

data extraction from xml file

I have an of xml file as shown below <?xml version='1.0' encoding='ASCII' standalone='yes' ?> <Station Index="10264" > <Number Value="237895890" /> <Position Lat="-29.5" Lon="3.5" /> <MaxDepth Value="-4939" /> <VeloLines Count="24"> <VeloLine Index="0" > <Depth... (3 Replies)
Discussion started by: shashi792
3 Replies

10. Shell Programming and Scripting

Problem in extraction when space is a field delimiter

I have more than 1000 files to parse. Each file contains few lines (number of lines varies) followed by a header line having all column's name (SPOT, NAME etc) and then values for those columns. **Example File: sdgafh dfhaadfha sfgaf dhah jkthdj SPOT NAME GENE_NAME CH_MEAN CHDN_MED ... (11 Replies)
Discussion started by: AshwaniSharma09
11 Replies
Login or Register to Ask a Question