Xml multi parse question

awk -F "[<>]" '/<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/ {print $2, $3}' OFS='\t' Test.xml 

The above code works great, but lets say I wanted the <Analyte> name (<Name>STAT3). The word <name> is unique so there will be multiple records pulled. Is there a way to make the name dependent on a word?

For example, STAT3 would be dependent on Analyte and all other records would be ignored? Thanks.

I'm not following your question fully, perhaps you could show an example input line and what you want to match on in it?

Unless you just mean to ask how to choose a given field to match on, in which case, you can replace:

/<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/

with

$2 ~ /<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>/

To have it only output lines where field 2 matches something in the list you've specified.

Lets say I wanted the <Condition>|<name> which = Hyperimmunoglobulin E syndrome, but not any other occurrence of <name>.

awk -F "[<>]" ' $2 ~ /<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>|<Condition>|<name>/
{print $2, $3}' OFS='\t' GTR.doc > output.txt

As you can see the <name> is not unique, but there is a unique header associated with it. Thanks.

If you are just looking for lines that match <Name>Hyperimmunoglobulin E syndrome, couldn't you just search for that instead of just <Name>?
ie

awk -F "[<>]" ' /<Id>|<id>|<Source>|<source>|<Accession>|<accession>|<TestName>|<testname>|<Condition>|<Name>Hyperimmunoglobulin E syndrome/
{print $2, $3}' OFS='\t' GTR.doc > output.txt

Please give us the input and desired output, we can help

The input file is attached.

The desired out would be:

Id 508680
TestName Whole Exome Sequencing
Offerer Baylor Medical Genetics Laboratories Baylor College of Medicine
DirectorList Sarah Elsea, PhD, ABMG, FACMG, Diplomate of the American Board of Medical Genetics, Lab Director
DirectorList Christine Eng, MD, ABMG, FACMG, Diplomate of the American Board of Medical Genetics, Medical Director, Laboratory Director
DirectorList Marilyn Li, MD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director
DirectorList Ankita Patel, PhD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director
DirectorList Lee-Jun Wong, PhD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director

awk -F "[<>]" ' /<Id>|<id>|< TestName>|<testname>|<Offerer>|<offerer>|<DirectorList>|<sting>/ {print $2, $3}' OFS='\t' exome.doc > output.txt

Thanks.

That attachment is:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE eSummaryResult PUBLIC "-//NLM//DTD esummary gtr 20140110//EN" "http://eutils.ncbi.nlm.nih.gov/eutils/dtd/20140110/esummary_gtr.dtd">
<eSummaryResult>
<DocumentSummarySet status="OK">
<DbBuild>Build140417-0530.1</DbBuild>

<DocumentSummary uid="508680">
        <Id>508680</Id>
        <Source>GTR</Source>
        <Accession>GTR000508680</Accession>
        <TestName>Whole Exome Sequencing</TestName>
        <TestType>Clinical</TestType>
        <ConditionList>
                <Condition>
                        <Name>Intellectual functioning disability</Name>
                        <Acronym></Acronym>
                        <CUI>C0025362</CUI>
                </Condition>
        </ConditionList>
        <Analytes>
        </Analytes>
        <GeneList>
        </GeneList>
        <Offerer>Baylor Medical Genetics Laboratories Baylor College of Medicine</Offerer>
        <OffererLocation>
                <Line1> 2450 Holcombe, Grand Blvd. - Sample Receiving Dock</Line1>
                <Line2></Line2>
                <Line3></Line3>
                <City>Houston</City>
                <State>Texas</State>
                <PostCode>77021-2024</PostCode>
                <Country>United States</Country>
        </OffererLocation>
        <OffererID>1006</OffererID>
        <DirectorList>
                <string>Sarah Elsea, PhD, ABMG, FACMG, Diplomate of the American Board of Medical Genetics, Lab Director</string>
                <string>Christine Eng, MD, ABMG, FACMG, Diplomate of the American Board of Medical Genetics, Medical Director, Laboratory Director</string>
                <string>Marilyn Li, MD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director</string>
                <string>Ankita Patel, PhD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director</string>
                <string>Lee-Jun Wong, PhD, ABMG, Diplomate of the American Board of Medical Genetics, Lab Director</string>
        </DirectorList>
        <Summary></Summary>
        <Flags>
        </Flags>
        <Method>
                <TopCategory>
                        <Name>Molecular Genetics</Name>
                        <CategoriesString>_C_______________</CategoriesString>
                        <CategoryList>
                                <Category>
                                        <Name>Sequence analysis of the entire coding region</Name>
                                        <Code>C</Code>
                                        <MethodList>
                                                <string>Next-Generation (NGS)/Massively parallel sequencing (MPS)</string>
                                        </MethodList>
                                </Category>
                        </CategoryList>
                </TopCategory>
        </Method>
        <AnalyticalValidity>Sensitivity 97%</AnalyticalValidity>
        <TargetPopulation></TargetPopulation>
        <Certifications>
                <Certification>
                        <CertificationType>MD - Maryland Department of Health and Mental Hygiene DHMH</CertificationType>
                        <id>1419</id>
                </Certification>
                <Certification>
                        <CertificationType>NY - New York State Department of Health NYSDOH</CertificationType>
                        <id>PFI-4584 Code 844052A2</id>
                </Certification>
                <Certification>
                        <CertificationType>FL - Florida Agency for Health Care Administration AHCA</CertificationType>
                        <id>L800012320</id>
                </Certification>
                <Certification>
                        <CertificationType>RI - State of Rhode Island Department of Health RIDOH</CertificationType>
                        <id>LCO00545</id>
                </Certification>
                <Certification>
                        <CertificationType>CLIA</CertificationType>
                        <id>45D0660090</id>
                </Certification>
                <Certification>
                        <CertificationType>CAP</CertificationType>
                        <id>2109314</id>
                </Certification>
        </Certifications>
        <StudyDesc></StudyDesc>
        <TestTargetList>
                <string>Human genome</string>
                <string>Whole exome</string>
        </TestTargetList>
        <ConditionCount>1</ConditionCount>
        <TestTargetCount>2</TestTargetCount>
        <Extra><![CDATA[]]></Extra>
</DocumentSummary>

</DocumentSummarySet>
</eSummaryResult>

As I understand your requirements, they would reject this data. What would data you do want look like?

How about perl

#!/usr/bin/perl
use XML::XPath;
use XML::XPath::XMLParser;

my $xpath=XML::XPath->new(filename  =>  "/path/exome.xml");

my $nodelist=$xpath->findnodes("/eSummaryResult/DocumentSummarySet/DocumentSummary");
foreach my $node ($nodelist->get_nodelist) {
  foreach my $c ($node->getChildNodes) {
    if ($c->getName =~ /\b(TestName|Offerer|DirectorList)\b/ ) { print $c->getName,"\t",$c->string_value,"\n"; }
  }

}