user3814005
user3814005

Reputation: 7

xmllint usage in linux (shell script)

    <?xml version="1.0" encoding="utf-8"?><DataTransfer xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="http://cbi.marian.com/xmlschemas/jde_interface_v1.0.xsd"><Summary>
    <PO>
        <PONumber>1798128110160-1</PONumber>
        <PORevision>6</PORevision>
        <POSystemID>273906</POSystemID>
        <OrderDate>19-02-2015</OrderDate>
        <RequiredDate>15-10-2016</RequiredDate>
        <PromisedDeliveryDate>27-03-2017</PromisedDeliveryDate>
        <RetentionPercentage></RetentionPercentage>
        <POReference1>1798128110160-1</POReference1>
        <POReference2></POReference2>
    </PO>
    <POItem>
        <LineNumber>   1</LineNumber>
        <ItemDescription>Enclosure containing </ItemDescription>
        <POReference1>1798128110160-1</POReference1>
        <POReference2></POReference2>
    </POItem>
        <POItem>
        <LineNumber>  2</LineNumber>
        <ItemDescription>Compressor</ItemDescription>
        <POReference1>1798128110160-1</POReference1>
        <POReference2></POReference2>
    </POItem>
    <POItem>
        <LineNumber>  3</LineNumber>
        <ItemDescription>Cooler</ItemDescription>
        <POReference1>1798128110160-1</POReference1>
        <POReference2></POReference2>
    </POItem>

In the above xml I want to read only the 1st LineNumber under POItem. I am using the following command:

echo cat "//POItem/LineNumber/text()" | xmllint --shell po123.xml

This displays all 3 LineNumber values. Is it possible to fetch only the 1st LineNumber value without using the sed command. XML file name is po123.xml

I don't have xpath in my current xmllint.

Upvotes: 1

Views: 2006

Answers (1)

Ross Ridge
Ross Ridge

Reputation: 39551

Try this:

echo cat '//POItem[1]/LineNumber/text()' | xmllint --shell po123.xml

This prints the LineNumber of the first POItem element.

Upvotes: 1

Related Questions