Menu

Exporting output to a Windows command variable

Help
Anonymous
2020-05-01
2020-05-02
  • Anonymous

    Anonymous - 2020-05-01

    I'm aware of this instruction in the documentation

    FOR /F "delims=" %%A IN ('xidel http://site -e "title:=//title" -e "links:=//a/@href" --output-format cmd') DO %%A

    and of these answers on Stack Overflow but I still get errors (Windows 10) mostly along the lines of

    %%A was unexpected at this time.

    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.

     
  • Reino

    Reino - 2020-05-01

    Either directly...

    FOR /F "delims=" %A IN ('xidel "in.xml" -e "//my_node"') DO SET my_var=%A
    

    ...or let Xidel do it...

    FOR /F "delims=" %A IN ('xidel "in.xml" -e "my_var:=//my_node" --output-format^=cmd') DO %A
    

    %A is for the command-line. In a batch-file use %%A.

     
  • Anonymous

    Anonymous - 2020-05-02

    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 is

    <div>
      <p>Hello</p>
      <span>World</span>
     </div>
    

    and I use

    FOR /F "delims=" %A IN ('xidel "input.xml" -e "//div"') DO SET my_var1=%A
    

    then

    echo %my_var1%
    

    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:

    echo %my_var2%
    
    Hello  World
    

    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?

     
  • Reino

    Reino - 2020-05-02

    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:

    C:\>FOR /F "delims=" %A IN ('xidel input.xml -se "join(//div/*)"') DO SET my_var1=%A
    
    C:\>SET my_var1=Hello World
    

    But the tags are stripped.

    That's what xidel does. If you really want the tags preserved, you'd have to use outer-xml():

    xidel input.xml -se "//div/*/outer-xml(.)"
    <p>Hello</p>
    <span>World</span>
    
     
  • Anonymous

    Anonymous - 2020-05-02

    Nothing is ever simple, but it worked:

    FOR /F "delims=" %A IN ('xidel input.xml -se "join(//div/*/outer-xml(.))"') DO SET my_var1=%A
    

    Thank you very much! Learned something new today.

     
  • Reino

    Reino - 2020-05-02

    That's great, but I think it's worth noting however that if you want to output valid xml, then you'd better do...

    xidel input.xml -se "//div/*" --output-format=xml --output-declaration="<?xml version=\"1.0\" encoding=\"utf-8\"?>"
    <?xml version="1.0" encoding="utf-8"?>
    <xml>
    <p>Hello</p>
    <span>World</span>
    </xml>
    

    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"?>

     
  • 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.

     

Anonymous
Anonymous

Add attachments
Cancel





MongoDB Logo MongoDB