xmllint --xpath add new line to output

S

sky

The output from running the following command is concatenated together, how do I add a new line to each result?

Here is the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<digital_tpp cycle="1213" from_edate="0901Z 12/13/12" to_edate="0901Z 01/10/13">
<state_code ID="AK" state_fullname="Alaska">
<city_name ID="ADAK ISLAND" volume="AK-1">
<airport_name ID="ADAK" military="N" apt_ident="ADK" icao_ident="PADK" alnum="1244">
<record>
<chartseq>10100</chartseq>
<chart_code>MIN</chart_code>
<chart_name>TAKEOFF MINIMUMS</chart_name>
<useraction/>
<pdf_name>AKTO.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection>L</bvsection>
<bvpage/>
<procuid/>
<two_colored>N</two_colored>
<civil> </civil>
<faanfd15/>
<faanfd18/>
<copter/>
</record>
<record>
<chartseq>53525</chartseq>
<chart_code>IAP</chart_code>
<chart_name>RNAV (GPS) RWY 23</chart_name>
<useraction/>
<pdf_name>01244R23.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection> </bvsection>
<bvpage>1</bvpage>
<procuid>15177</procuid>
<two_colored>Y</two_colored>
<civil>C</civil>
<faanfd15>P23</faanfd15>
<faanfd18>R23</faanfd18>
<copter>N</copter>
</record>
<record>
<chartseq>57000</chartseq>
<chart_code>IAP</chart_code>
<chart_name>NDB/DME RWY 23</chart_name>
<useraction/>
<pdf_name>01244ND23.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection> </bvsection>
<bvpage>2</bvpage>
<procuid>15176</procuid>
<two_colored>Y</two_colored>
<civil>C</civil>
<faanfd15>N23</faanfd15>
<faanfd18>Q23</faanfd18>
<copter>N</copter>
</record>
</airport_name>
</city_name>
</state_code>
</digital_tpp>

Here is the command I'm running to select the value between the pdf_name tags

bash$ xmllint --xpath '//airport_name[@apt_ident="ADK"]/record[chart_code="IAP" or "DP" or "STAR" or "APD"]/pdf_name/text()' file.xml

Here is what the output looks like, as you can see it is missing new lines between the values returned, how do I add new lines?

AKTO.PDF01244R23.PDF01244ND23.PDFbash$

I want the output to look like this

AKTO.PDF
01244R23.PDF
01244ND23.PDF
 
M

Manuel Collado

El 12/12/2012 18:30, sky escribió:
The output from running the following command is concatenated together, how do I add a new line to each result?

Here is the xml file:

<?xml version="1.0" encoding="UTF-8"?>
<digital_tpp cycle="1213" from_edate="0901Z 12/13/12" to_edate="0901Z 01/10/13">
<state_code ID="AK" state_fullname="Alaska">
<city_name ID="ADAK ISLAND" volume="AK-1">
<airport_name ID="ADAK" military="N" apt_ident="ADK" icao_ident="PADK" alnum="1244">
<record>
<chartseq>10100</chartseq>
<chart_code>MIN</chart_code>
<chart_name>TAKEOFF MINIMUMS</chart_name>
<useraction/>
<pdf_name>AKTO.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection>L</bvsection>
<bvpage/>
<procuid/>
<two_colored>N</two_colored>
<civil> </civil>
<faanfd15/>
<faanfd18/>
<copter/>
</record>
<record>
<chartseq>53525</chartseq>
<chart_code>IAP</chart_code>
<chart_name>RNAV (GPS) RWY 23</chart_name>
<useraction/>
<pdf_name>01244R23.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection> </bvsection>
<bvpage>1</bvpage>
<procuid>15177</procuid>
<two_colored>Y</two_colored>
<civil>C</civil>
<faanfd15>P23</faanfd15>
<faanfd18>R23</faanfd18>
<copter>N</copter>
</record>
<record>
<chartseq>57000</chartseq>
<chart_code>IAP</chart_code>
<chart_name>NDB/DME RWY 23</chart_name>
<useraction/>
<pdf_name>01244ND23.PDF</pdf_name>
<cn_flg>N</cn_flg>
<cnsection/>
<cnpage/>
<bvsection> </bvsection>
<bvpage>2</bvpage>
<procuid>15176</procuid>
<two_colored>Y</two_colored>
<civil>C</civil>
<faanfd15>N23</faanfd15>
<faanfd18>Q23</faanfd18>
<copter>N</copter>
</record>
</airport_name>
</city_name>
</state_code>
</digital_tpp>

Here is the command I'm running to select the value between the pdf_name tags

bash$ xmllint --xpath '//airport_name[@apt_ident="ADK"]/record[chart_code="IAP" or "DP" or "STAR" or "APD"]/pdf_name/text()' file.xml

Here is what the output looks like, as you can see it is missing new lines between the values returned, how do I add new lines?

AKTO.PDF01244R23.PDF01244ND23.PDFbash$

I want the output to look like this

AKTO.PDF
01244R23.PDF
01244ND23.PDF

The output of xmllint is correct. The text nodes containing the newlines
are not selected by the given XPath expression.

To add newlines after each matched node you have to use a different
tool. You could write an XSLT transformation (instead of just a XPath
expression) and apply it with an XSLT processor.

Fortunately, there is xmlstarlet (the executable is "xml"), that you can
use instead of xmllint. xmlstarlet is a tool that automagically can
internally create the XSLT for you in some simple cases.

The particular command line in your case is (warning, single long line
probably wrapped in the message):

xml sel -t -m "//airport_name[@apt_ident='ADK']/record[chart_code='IAP'
or 'D P' or 'STAR' or 'APD']/pdf_name/text()" -c "." -n file.xml

It gives:

AKTO.PDF
01244R23.PDF
01244ND23.PDF

Caveat: it takes some time to master the intricacies of the command line
parameters of xmlstarlet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,828
Messages
2,569,736
Members
45,519
Latest member
SadyeCrisw

Latest Threads

Top