Multiple matches of xml to single csv row (xmlstarlet)










2















XML Sample:



<hosts>
<host>
<name>Server A</name>
<status>0</status>
<groups>
<group>
<name>Discovered hosts</name>
</group>
</groups>
<interfaces>
<interface>
<ip>10.1.2.3</ip>
</interface>
</interfaces>
</host>
<host>
<name>Server B</name>
<status>0</status>
<groups>
<group>
<name>Discovered hosts</name>
</group>
</groups>
<interfaces>
<interface>
<ip>10.1.2.4</ip>
</interface>
</interfaces>
</host>
</hosts>


I try to export entries to CSV file like this:



Discovered hosts,Server A,10.1.2.3,0
Discovered hosts,Server B,10.1.2.4,0


with xmlstarlet, guiding with:




There can be multiple --match, --copy-of, --value-of, etc options in a
single template. The effect of applying command line templates can be
illustrated with the following XSLT analogue



xml sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3"
-t -m "xpath4" -c "xpath5"




But my result is different from the expected:



$ xmlstarlet sel -t 
-m "//host/groups/group/name" -v . -o ","
-m "//host/name" -v . -o ","
-m "//host/interfaces/interface/ip" -v . -o ","
-m "//host/status" -v . -n sample.xml
Discovered hosts,Server A,10.1.2.3,0
0
10.1.2.4,0
0
Server B,10.1.2.3,0
0
10.1.2.4,0
0
Discovered hosts,Server A,10.1.2.3,0
0
10.1.2.4,0
0
Server B,10.1.2.3,0
0
10.1.2.4,0
0


It is evident to use grep like workaround (that's exactly what I did), but I want to understand how to use it properly and what caused this unobvious behavior.










share|improve this question


























    2















    XML Sample:



    <hosts>
    <host>
    <name>Server A</name>
    <status>0</status>
    <groups>
    <group>
    <name>Discovered hosts</name>
    </group>
    </groups>
    <interfaces>
    <interface>
    <ip>10.1.2.3</ip>
    </interface>
    </interfaces>
    </host>
    <host>
    <name>Server B</name>
    <status>0</status>
    <groups>
    <group>
    <name>Discovered hosts</name>
    </group>
    </groups>
    <interfaces>
    <interface>
    <ip>10.1.2.4</ip>
    </interface>
    </interfaces>
    </host>
    </hosts>


    I try to export entries to CSV file like this:



    Discovered hosts,Server A,10.1.2.3,0
    Discovered hosts,Server B,10.1.2.4,0


    with xmlstarlet, guiding with:




    There can be multiple --match, --copy-of, --value-of, etc options in a
    single template. The effect of applying command line templates can be
    illustrated with the following XSLT analogue



    xml sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3"
    -t -m "xpath4" -c "xpath5"




    But my result is different from the expected:



    $ xmlstarlet sel -t 
    -m "//host/groups/group/name" -v . -o ","
    -m "//host/name" -v . -o ","
    -m "//host/interfaces/interface/ip" -v . -o ","
    -m "//host/status" -v . -n sample.xml
    Discovered hosts,Server A,10.1.2.3,0
    0
    10.1.2.4,0
    0
    Server B,10.1.2.3,0
    0
    10.1.2.4,0
    0
    Discovered hosts,Server A,10.1.2.3,0
    0
    10.1.2.4,0
    0
    Server B,10.1.2.3,0
    0
    10.1.2.4,0
    0


    It is evident to use grep like workaround (that's exactly what I did), but I want to understand how to use it properly and what caused this unobvious behavior.










    share|improve this question
























      2












      2








      2








      XML Sample:



      <hosts>
      <host>
      <name>Server A</name>
      <status>0</status>
      <groups>
      <group>
      <name>Discovered hosts</name>
      </group>
      </groups>
      <interfaces>
      <interface>
      <ip>10.1.2.3</ip>
      </interface>
      </interfaces>
      </host>
      <host>
      <name>Server B</name>
      <status>0</status>
      <groups>
      <group>
      <name>Discovered hosts</name>
      </group>
      </groups>
      <interfaces>
      <interface>
      <ip>10.1.2.4</ip>
      </interface>
      </interfaces>
      </host>
      </hosts>


      I try to export entries to CSV file like this:



      Discovered hosts,Server A,10.1.2.3,0
      Discovered hosts,Server B,10.1.2.4,0


      with xmlstarlet, guiding with:




      There can be multiple --match, --copy-of, --value-of, etc options in a
      single template. The effect of applying command line templates can be
      illustrated with the following XSLT analogue



      xml sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3"
      -t -m "xpath4" -c "xpath5"




      But my result is different from the expected:



      $ xmlstarlet sel -t 
      -m "//host/groups/group/name" -v . -o ","
      -m "//host/name" -v . -o ","
      -m "//host/interfaces/interface/ip" -v . -o ","
      -m "//host/status" -v . -n sample.xml
      Discovered hosts,Server A,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Server B,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Discovered hosts,Server A,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Server B,10.1.2.3,0
      0
      10.1.2.4,0
      0


      It is evident to use grep like workaround (that's exactly what I did), but I want to understand how to use it properly and what caused this unobvious behavior.










      share|improve this question














      XML Sample:



      <hosts>
      <host>
      <name>Server A</name>
      <status>0</status>
      <groups>
      <group>
      <name>Discovered hosts</name>
      </group>
      </groups>
      <interfaces>
      <interface>
      <ip>10.1.2.3</ip>
      </interface>
      </interfaces>
      </host>
      <host>
      <name>Server B</name>
      <status>0</status>
      <groups>
      <group>
      <name>Discovered hosts</name>
      </group>
      </groups>
      <interfaces>
      <interface>
      <ip>10.1.2.4</ip>
      </interface>
      </interfaces>
      </host>
      </hosts>


      I try to export entries to CSV file like this:



      Discovered hosts,Server A,10.1.2.3,0
      Discovered hosts,Server B,10.1.2.4,0


      with xmlstarlet, guiding with:




      There can be multiple --match, --copy-of, --value-of, etc options in a
      single template. The effect of applying command line templates can be
      illustrated with the following XSLT analogue



      xml sel -t -c "xpath0" -m "xpath1" -m "xpath2" -v "xpath3"
      -t -m "xpath4" -c "xpath5"




      But my result is different from the expected:



      $ xmlstarlet sel -t 
      -m "//host/groups/group/name" -v . -o ","
      -m "//host/name" -v . -o ","
      -m "//host/interfaces/interface/ip" -v . -o ","
      -m "//host/status" -v . -n sample.xml
      Discovered hosts,Server A,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Server B,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Discovered hosts,Server A,10.1.2.3,0
      0
      10.1.2.4,0
      0
      Server B,10.1.2.3,0
      0
      10.1.2.4,0
      0


      It is evident to use grep like workaround (that's exactly what I did), but I want to understand how to use it properly and what caused this unobvious behavior.







      xml csv export-to-csv xmlstarlet






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 14 '18 at 10:25









      viktorkhoviktorkho

      297




      297






















          1 Answer
          1






          active

          oldest

          votes


















          1














          You're not using the option -m correctly.



          The -m option is the xpath from where you extract values (with -v option).



          So your query should be:



          xmlstarlet sel -t -m "hosts/host" 
          -v "groups/group/name" -o ","
          -v "name" -o ","
          -v "interfaces/interface/ip" -o ","
          -v "status"
          -n file


          where hosts/host is the root of your search query.






          share|improve this answer























          • Yes, you are right, now everything became clear. Thanks!

            – viktorkho
            Nov 14 '18 at 11:28










          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297956%2fmultiple-matches-of-xml-to-single-csv-row-xmlstarlet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          You're not using the option -m correctly.



          The -m option is the xpath from where you extract values (with -v option).



          So your query should be:



          xmlstarlet sel -t -m "hosts/host" 
          -v "groups/group/name" -o ","
          -v "name" -o ","
          -v "interfaces/interface/ip" -o ","
          -v "status"
          -n file


          where hosts/host is the root of your search query.






          share|improve this answer























          • Yes, you are right, now everything became clear. Thanks!

            – viktorkho
            Nov 14 '18 at 11:28















          1














          You're not using the option -m correctly.



          The -m option is the xpath from where you extract values (with -v option).



          So your query should be:



          xmlstarlet sel -t -m "hosts/host" 
          -v "groups/group/name" -o ","
          -v "name" -o ","
          -v "interfaces/interface/ip" -o ","
          -v "status"
          -n file


          where hosts/host is the root of your search query.






          share|improve this answer























          • Yes, you are right, now everything became clear. Thanks!

            – viktorkho
            Nov 14 '18 at 11:28













          1












          1








          1







          You're not using the option -m correctly.



          The -m option is the xpath from where you extract values (with -v option).



          So your query should be:



          xmlstarlet sel -t -m "hosts/host" 
          -v "groups/group/name" -o ","
          -v "name" -o ","
          -v "interfaces/interface/ip" -o ","
          -v "status"
          -n file


          where hosts/host is the root of your search query.






          share|improve this answer













          You're not using the option -m correctly.



          The -m option is the xpath from where you extract values (with -v option).



          So your query should be:



          xmlstarlet sel -t -m "hosts/host" 
          -v "groups/group/name" -o ","
          -v "name" -o ","
          -v "interfaces/interface/ip" -o ","
          -v "status"
          -n file


          where hosts/host is the root of your search query.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 14 '18 at 11:03









          olivoliv

          8,3891130




          8,3891130












          • Yes, you are right, now everything became clear. Thanks!

            – viktorkho
            Nov 14 '18 at 11:28

















          • Yes, you are right, now everything became clear. Thanks!

            – viktorkho
            Nov 14 '18 at 11:28
















          Yes, you are right, now everything became clear. Thanks!

          – viktorkho
          Nov 14 '18 at 11:28





          Yes, you are right, now everything became clear. Thanks!

          – viktorkho
          Nov 14 '18 at 11:28



















          draft saved

          draft discarded
















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid


          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.

          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53297956%2fmultiple-matches-of-xml-to-single-csv-row-xmlstarlet%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          這個網誌中的熱門文章

          How to read a connectionString WITH PROVIDER in .NET Core?

          In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

          Museum of Modern and Contemporary Art of Trento and Rovereto