0

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.

Charles Duffy
  • 280,126
  • 43
  • 390
  • 441
Sandra
  • 103
  • 2
  • 14

1 Answers1

0

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.
npostavs
  • 4,877
  • 1
  • 24
  • 43
  • what do you get as a result ? i get more errors, what does the provided command do? – Sandra Feb 10 '15 at 23:53
  • @Sandra: I added prompt and output for the working first command, it outputs `C:\file.txt` which is what you are after right? – npostavs Feb 11 '15 at 00:45
  • that does not work with me, i get weird errors then. does this create an empty file? – Sandra Feb 11 '15 at 22:56
  • There should be no file, it just prints to stdout. I've tested on Windows Vista and 7. – npostavs Feb 12 '15 at 03:10
  • i get following errors `C:\Users\user1>C:\file.txt") | xmlstarlet sel -t -v /* /Cygwin.bat:1.1: Start tag expected, '<' not found @echo off ^ /Cygwin.ico:1.1: Document is empty ^ /Cygwin.ico:1.1: Start tag expected, '<' not found ^ Is a directory: gzread() /bin:1.1: Document is empty ^ /bin:1.1: Start tag expected, '<' not found ^ /cygdrive:1.1: Document is empty ^ /cygdrive:1.1: Start tag expected, '<' not found ^ Is a directory: gzread() /dev:1.1: Document is empty ` – Sandra Feb 12 '15 at 19:44
  • ok, got it, i replaced /* with //path and that got me the result. THANKS A LOT!!!! – Sandra Feb 12 '15 at 19:54
  • do you have any idea how i could put the result of xmlstarlet (C:\file.txt) in another shell variable? – Sandra Feb 12 '15 at 20:19