Search code examples
command-lineechocallxmlstarlet

DOS/Windows xmlstarlet usage with a String instead of a xml file


can xmlstarlet be used with a String instead of a xml file? e.g.:

xmlstarlet sel -t -v "/*" "<pathlist><path>C:\file.txt</path></pathlist>"

instead of

xmlstarlet sel -t -v "/*" pathlist.xml

or how else could i realize with a string ? when i echo the string and pipe it to xmlstarlet it does not work:

SET "_var=^<pathlist^>^<path^>C:\file.txt^</path^> ^</pathlist^>"
& 
call echo %^_var% | xmlstarlet sel -t -v "//*"

gives error:

< was unexpected at this time.
-:1.1: Document is empty

^
-:1.1: Start tag expected, '<' not found

^

this is a simple task actually, but i cant get it to work. i just want to echo a string to xmlstarlet within a One-Liner.


Solution

  • cmd.exe syntax is weird, the following trick using set /p seems to work:

    C:\tmp><nul (set /p ="<pathlist><path>C:\file.txt</path></pathlist>") | xmlstarlet sel -t -v /*
    C:\file.txt
    

    /* may get glob expanded (depending on what files you have). Unfortunately, there is no way to quote it from cmd.exe (the expansion is performed by libc on behalf of xmlstarlet), so you will have to rewrite the XPath in that case, e.g. /pathlist instead.

    Source: https://groups.google.com/d/msg/alt.msdos.batch.nt/RNug94fXI5s/BdgYJfNmXysJ via http://www.netikka.net/tsneti/info/tscmd047.htm


    I found no explanation of why escaping <> doesn't work with | redirection??

    C:\tmp> echo ^<^>
    <>
    C:\tmp> echo ^<^> | more
    > was unexpected at this time.