xmllint --xpath add new line to output

Discussion in 'XML' started by sky, Dec 12, 2012.

  1. sky

    sky Guest

    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
     
    sky, Dec 12, 2012
    #1
    1. Advertising

  2. 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.

    --
    Manuel Collado - http://lml.ls.fi.upm.es/~mcollado
     
    Manuel Collado, Dec 12, 2012
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Marvin_123456

    "Memory leak" in javax.xml.xpath.XPath

    Marvin_123456, Jul 29, 2005, in forum: Java
    Replies:
    4
    Views:
    2,027
    jan V
    Jul 29, 2005
  2. Neil McNaughton

    Using xmllint and xpath without --shell

    Neil McNaughton, Jan 7, 2005, in forum: XML
    Replies:
    1
    Views:
    4,756
    grouch
    Jan 8, 2005
  3. Stan R.
    Replies:
    4
    Views:
    17,512
    Stan R.
    Jun 8, 2006
  4. Geoff

    xmllint & dom.Writer

    Geoff, Oct 16, 2006, in forum: XML
    Replies:
    2
    Views:
    527
    Joe Kesselman
    Oct 16, 2006
  5. Replies:
    2
    Views:
    1,413
Loading...

Share This Page