Home > Net >  Does exists any way to redirect pipe output as file?
Does exists any way to redirect pipe output as file?

Time:03-15

I'm use xmllint to select node, and my test purposed 1.xml is looks like this

<resources>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
   <item>
        <label>LABEL</label>
        <value>VALUE</value>
        <description>DESCRIPTION</description>
   </item>
</resources>
$ xmllint --xpath '/resources/item/value' 1.xml
<value>VALUE</value><value>VALUE</value>

Command likes above is work well.

And then i try to combine with pipe |, error occurred

$ cat 1.xml | xmllint --xpath '/resources/item/value'
Usage : xmllint [options] XMLfiles ...
...(help info)

I suppose the reason is pipe transmit process cat's output as a stream, but xmllint can only receive file path as argument. So, does any way to solve this problem? or maybe some alternative?

Of course. If my guess if fault, point at real reason is also pretty helpful to me.

sorry

My English is poor. Please excuse grammar or typing error. I'm trying my best to improve.

CodePudding user response:

I have never used xmllint, but from its man page, I can see:

The xmllint program parses one or more XML files, specified on the command line as XML-FILE (or the standard input if the filename provided is -

Therefore, the following should work:

xmllint --xpath '/resources/item/value' 1.xml

or, if you insist that the input should come via stdin,

xmllint --xpath '/resources/item/value' - <1.xml

CodePudding user response:

As an alternative, you can pass xmllint command to stdin via --shell option

echo -e 'cat /resources/item/value\nbye' | xmllint --shell 1.xml

Or

(echo 'cat /resources/item/value'; echo 'bye') | xmllint --shell 1.xml
  • Related