The indirect method FOR /F "delims=" %A IN ('xidel "input.xml" -e "my_var2:=//div" --output-format^=cmd') DO %A
works a little better:
echo%my_var2%HelloWorld
and gets both nodes. But the tags are stripped.
The solution would be to somehow combine the indirect method with --output-format xml. Is that possible?
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
In cmd you can't assign new lines (\r\n) to a variable.
The first method only works for 1 single line output, so you'd have to string-join the text-nodes with xidel if you want "Hello World" as output:
On Win10 you could try without --output-declaration, but as my cmd's code-page defaults to 437 I'm getting <?xml version="1.0" encoding="ibm437"?>
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Anonymous
Anonymous
-
2020-05-02
It looks like we have to stick to your original answer :D.
For xidel stand alone, your new version (with or without --output-declaration in Windows 10) does produce valid xm. But when wrapped inside a FOR/DO it fails, since my_var1 is the last node tag, not the whole input file. The outcome is the same even if, instead of -se "//div/*", you use -se "join(//div/*)" or -se "join(//div/*/outer-xml(.))". So, your original still stands...
By way of background (I didn't want to clutter the original question with this), but I may not necessarily want my_var1 to be valid xml. The purpose of the exercise, behind the scenes, is to use the variable as an input for an insertion of a new node into another xml file (basically, copy element from file 1 as a variable and use the variable to paste a new node into file 2). There are other ways of achiving this, but using this one, the variable can be thought of as more of a string literal than a valid xml document - it just needs to look like a valid xml fragment suitable for insertion into file 2. So with that in mind - there's another reason to stick with the original.
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I'm aware of this instruction in the documentation
and of these answers on Stack Overflow but I still get errors (Windows 10) mostly along the lines of
So: if I have file "in.xml" and I want to select node
<my_node>
and export it to a cmd variable "my_var" - what exactly do I need to do?Thanks.
Either directly...
...or let Xidel do it...
%A
is for the command-line. In a batch-file use%%A
.The 'direct' method doesn't really work, as it creates a variable only from the text node of the last element. To illustrate, if
input.xml
isand I use
then
outputs
World
The indirect method
FOR /F "delims=" %A IN ('xidel "input.xml" -e "my_var2:=//div" --output-format^=cmd') DO %A
works a little better:
and gets both nodes. But the tags are stripped.
The solution would be to somehow combine the indirect method with
--output-format xml
. Is that possible?In
cmd
you can't assign new lines (\r\n
) to a variable.The first method only works for 1 single line output, so you'd have to string-join the text-nodes with
xidel
if you want "Hello World" as output:That's what
xidel
does. If you really want the tags preserved, you'd have to useouter-xml()
:Nothing is ever simple, but it worked:
Thank you very much! Learned something new today.
That's great, but I think it's worth noting however that if you want to output valid xml, then you'd better do...
On Win10 you could try without
--output-declaration
, but as mycmd
's code-page defaults to 437 I'm getting<?xml version="1.0" encoding="ibm437"?>
It looks like we have to stick to your original answer :D.
For xidel stand alone, your new version (with or without
--output-declaration
in Windows 10) does produce valid xm. But when wrapped inside aFOR/DO
it fails, sincemy_var1
is the last node tag, not the whole input file. The outcome is the same even if, instead of-se "//div/*"
, you use-se "join(//div/*)"
or-se "join(//div/*/outer-xml(.))"
. So, your original still stands...By way of background (I didn't want to clutter the original question with this), but I may not necessarily want
my_var1
to be valid xml. The purpose of the exercise, behind the scenes, is to use the variable as an input for an insertion of a new node into another xml file (basically, copy element from file 1 as a variable and use the variable to paste a new node into file 2). There are other ways of achiving this, but using this one, the variable can be thought of as more of a string literal than a valid xml document - it just needs to look like a valid xml fragment suitable for insertion into file 2. So with that in mind - there's another reason to stick with the original.