Parse .xml file in a specific way

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
1
down vote

favorite












I have the following .xml and I want a specific output:



<ContactPerson>
<GivenName>John</GivenName>
<FamilyName>Wayne</FamilyName>
<PhoneFixList>
<PhoneFix>78787878</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>john.wayne@gmail.com</Email>
</EmailList>
</ContactPerson>

<ContactPerson>
<GivenName>James</GivenName>
<FamilyName>Stewart</FamilyName>
<PhoneFixList>
<PhoneFix>90909090</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>james.stewart@gmail.com</Email>
</EmailList>
</ContactPerson>


Output:



John Wayne 78787878 john.wayne@gmail.com
James Stewart 90909090 james.stewart@gmail.com


In some cases, I have more than two <ContactPerson> so I would be nice to have a great loop to resolve this. I've tried to resolve the problem with a for loop but I always have the result:



John Wayne 78787878 john.wayne@gmail.com
John Wayne 78787878 john.wayne@gmail.com






share|improve this question


















  • 2




    The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
    – Kusalananda
    Feb 9 at 11:30














up vote
1
down vote

favorite












I have the following .xml and I want a specific output:



<ContactPerson>
<GivenName>John</GivenName>
<FamilyName>Wayne</FamilyName>
<PhoneFixList>
<PhoneFix>78787878</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>john.wayne@gmail.com</Email>
</EmailList>
</ContactPerson>

<ContactPerson>
<GivenName>James</GivenName>
<FamilyName>Stewart</FamilyName>
<PhoneFixList>
<PhoneFix>90909090</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>james.stewart@gmail.com</Email>
</EmailList>
</ContactPerson>


Output:



John Wayne 78787878 john.wayne@gmail.com
James Stewart 90909090 james.stewart@gmail.com


In some cases, I have more than two <ContactPerson> so I would be nice to have a great loop to resolve this. I've tried to resolve the problem with a for loop but I always have the result:



John Wayne 78787878 john.wayne@gmail.com
John Wayne 78787878 john.wayne@gmail.com






share|improve this question


















  • 2




    The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
    – Kusalananda
    Feb 9 at 11:30












up vote
1
down vote

favorite









up vote
1
down vote

favorite











I have the following .xml and I want a specific output:



<ContactPerson>
<GivenName>John</GivenName>
<FamilyName>Wayne</FamilyName>
<PhoneFixList>
<PhoneFix>78787878</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>john.wayne@gmail.com</Email>
</EmailList>
</ContactPerson>

<ContactPerson>
<GivenName>James</GivenName>
<FamilyName>Stewart</FamilyName>
<PhoneFixList>
<PhoneFix>90909090</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>james.stewart@gmail.com</Email>
</EmailList>
</ContactPerson>


Output:



John Wayne 78787878 john.wayne@gmail.com
James Stewart 90909090 james.stewart@gmail.com


In some cases, I have more than two <ContactPerson> so I would be nice to have a great loop to resolve this. I've tried to resolve the problem with a for loop but I always have the result:



John Wayne 78787878 john.wayne@gmail.com
John Wayne 78787878 john.wayne@gmail.com






share|improve this question














I have the following .xml and I want a specific output:



<ContactPerson>
<GivenName>John</GivenName>
<FamilyName>Wayne</FamilyName>
<PhoneFixList>
<PhoneFix>78787878</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>john.wayne@gmail.com</Email>
</EmailList>
</ContactPerson>

<ContactPerson>
<GivenName>James</GivenName>
<FamilyName>Stewart</FamilyName>
<PhoneFixList>
<PhoneFix>90909090</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>james.stewart@gmail.com</Email>
</EmailList>
</ContactPerson>


Output:



John Wayne 78787878 john.wayne@gmail.com
James Stewart 90909090 james.stewart@gmail.com


In some cases, I have more than two <ContactPerson> so I would be nice to have a great loop to resolve this. I've tried to resolve the problem with a for loop but I always have the result:



John Wayne 78787878 john.wayne@gmail.com
John Wayne 78787878 john.wayne@gmail.com








share|improve this question













share|improve this question




share|improve this question








edited Feb 9 at 12:06

























asked Feb 9 at 11:27









lendoiro

163




163







  • 2




    The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
    – Kusalananda
    Feb 9 at 11:30












  • 2




    The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
    – Kusalananda
    Feb 9 at 11:30







2




2




The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
– Kusalananda
Feb 9 at 11:30




The XML is faulty. It has no root node and <PhoneMobileList> has no end tag. This can not be parsed with an XML parser.
– Kusalananda
Feb 9 at 11:30










3 Answers
3






active

oldest

votes

















up vote
3
down vote













Assuming the following (modified) XML:



<?xml version="1.0"?>
<root>
<ContactPerson>
<GivenName>John</GivenName>
<FamilyName>Wayne</FamilyName>
<PhoneFixList>
<PhoneFix>78787878</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>john.wayne@gmail.com</Email>
</EmailList>
</ContactPerson>
<ContactPerson>
<GivenName>James</GivenName>
<FamilyName>Stewart</FamilyName>
<PhoneFixList>
<PhoneFix>90909090</PhoneFix>
</PhoneFixList>
<EmailList>
<Email>james.stewart@gmail.com</Email>
</EmailList>
</ContactPerson>
</root>


The XMLStarlet
invocation



xml sel -t -m '//ContactPerson' -v 'concat(GivenName, " ", FamilyName, " ", PhoneFixList/PhoneFix, " ", EmailList/Email)' -nl file.xml


produces



John Wayne 78787878 john.wayne@gmail.com
James Stewart 90909090 james.stewart@gmail.com


The invocation of XMLStarlet contains an implicit loop over all ContactPerson nodes, and for each of these, the specific fields are concatenated to produce the correct output. The -nl at the end causes a newline to be printed.






share|improve this answer






















  • Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
    – lendoiro
    Feb 9 at 11:51







  • 1




    @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
    – Kusalananda
    Feb 9 at 12:05










  • Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
    – lendoiro
    Feb 9 at 12:08







  • 1




    Don't even try to process XML with tools that don't understand XML.
    – user32929
    Feb 9 at 13:04










  • That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
    – Kusalananda
    Feb 9 at 13:32

















up vote
1
down vote













With xml2 (and a shell with support for the $'...' quotes):



 echo "<r>"; cat file.xml; echo "</r>"; |
| xml2 | 2csv -d $'t' ContactPerson GivenName FamilyName EmailList/Email


(for ContactPersons with more than one email address, the last one is returned)






share|improve this answer



























    up vote
    1
    down vote













    If you're OK with having a second file for processing this, you can make an XSLT file and use it to transform your XML file using xsltproc.



    phones.xsl:



    <?xml version="1.0"?>
    <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="text"/>
    <xsl:strip-space elements="*"/>
    <xsl:template match="ContactPerson">
    <xsl:value-of select="GivenName"/><xsl:text> </xsl:text><xsl:value-of select="FamilyName"/>
    <xsl:for-each select="PhoneFixList/PhoneFix">
    <xsl:text> </xsl:text><xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:for-each select="EmailList/Email">
    <xsl:text> </xsl:text><xsl:value-of select="."/>
    </xsl:for-each>
    <xsl:text>
    </xsl:text>
    </xsl:template>
    </xsl:stylesheet>


    Then transform the file (using @Kusalananda's XML as phones.xml):



    $ xsltproc phones.xsl phones.xml
    John Wayne 78787878 john.wayne@gmail.com
    James Stewart 90909090 james.stewart@gmail.com


    Using XSLT has the advantage that you can loop through repeating elements, do conditional output and so forth. If you're comfortable with a bit of study and programming, it's very powerful.






    share|improve this answer




















      Your Answer







      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "106"
      ;
      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',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );








       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f423013%2fparse-xml-file-in-a-specific-way%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      3
      down vote













      Assuming the following (modified) XML:



      <?xml version="1.0"?>
      <root>
      <ContactPerson>
      <GivenName>John</GivenName>
      <FamilyName>Wayne</FamilyName>
      <PhoneFixList>
      <PhoneFix>78787878</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>john.wayne@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      <ContactPerson>
      <GivenName>James</GivenName>
      <FamilyName>Stewart</FamilyName>
      <PhoneFixList>
      <PhoneFix>90909090</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>james.stewart@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      </root>


      The XMLStarlet
      invocation



      xml sel -t -m '//ContactPerson' -v 'concat(GivenName, " ", FamilyName, " ", PhoneFixList/PhoneFix, " ", EmailList/Email)' -nl file.xml


      produces



      John Wayne 78787878 john.wayne@gmail.com
      James Stewart 90909090 james.stewart@gmail.com


      The invocation of XMLStarlet contains an implicit loop over all ContactPerson nodes, and for each of these, the specific fields are concatenated to produce the correct output. The -nl at the end causes a newline to be printed.






      share|improve this answer






















      • Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
        – lendoiro
        Feb 9 at 11:51







      • 1




        @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
        – Kusalananda
        Feb 9 at 12:05










      • Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
        – lendoiro
        Feb 9 at 12:08







      • 1




        Don't even try to process XML with tools that don't understand XML.
        – user32929
        Feb 9 at 13:04










      • That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
        – Kusalananda
        Feb 9 at 13:32














      up vote
      3
      down vote













      Assuming the following (modified) XML:



      <?xml version="1.0"?>
      <root>
      <ContactPerson>
      <GivenName>John</GivenName>
      <FamilyName>Wayne</FamilyName>
      <PhoneFixList>
      <PhoneFix>78787878</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>john.wayne@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      <ContactPerson>
      <GivenName>James</GivenName>
      <FamilyName>Stewart</FamilyName>
      <PhoneFixList>
      <PhoneFix>90909090</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>james.stewart@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      </root>


      The XMLStarlet
      invocation



      xml sel -t -m '//ContactPerson' -v 'concat(GivenName, " ", FamilyName, " ", PhoneFixList/PhoneFix, " ", EmailList/Email)' -nl file.xml


      produces



      John Wayne 78787878 john.wayne@gmail.com
      James Stewart 90909090 james.stewart@gmail.com


      The invocation of XMLStarlet contains an implicit loop over all ContactPerson nodes, and for each of these, the specific fields are concatenated to produce the correct output. The -nl at the end causes a newline to be printed.






      share|improve this answer






















      • Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
        – lendoiro
        Feb 9 at 11:51







      • 1




        @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
        – Kusalananda
        Feb 9 at 12:05










      • Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
        – lendoiro
        Feb 9 at 12:08







      • 1




        Don't even try to process XML with tools that don't understand XML.
        – user32929
        Feb 9 at 13:04










      • That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
        – Kusalananda
        Feb 9 at 13:32












      up vote
      3
      down vote










      up vote
      3
      down vote









      Assuming the following (modified) XML:



      <?xml version="1.0"?>
      <root>
      <ContactPerson>
      <GivenName>John</GivenName>
      <FamilyName>Wayne</FamilyName>
      <PhoneFixList>
      <PhoneFix>78787878</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>john.wayne@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      <ContactPerson>
      <GivenName>James</GivenName>
      <FamilyName>Stewart</FamilyName>
      <PhoneFixList>
      <PhoneFix>90909090</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>james.stewart@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      </root>


      The XMLStarlet
      invocation



      xml sel -t -m '//ContactPerson' -v 'concat(GivenName, " ", FamilyName, " ", PhoneFixList/PhoneFix, " ", EmailList/Email)' -nl file.xml


      produces



      John Wayne 78787878 john.wayne@gmail.com
      James Stewart 90909090 james.stewart@gmail.com


      The invocation of XMLStarlet contains an implicit loop over all ContactPerson nodes, and for each of these, the specific fields are concatenated to produce the correct output. The -nl at the end causes a newline to be printed.






      share|improve this answer














      Assuming the following (modified) XML:



      <?xml version="1.0"?>
      <root>
      <ContactPerson>
      <GivenName>John</GivenName>
      <FamilyName>Wayne</FamilyName>
      <PhoneFixList>
      <PhoneFix>78787878</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>john.wayne@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      <ContactPerson>
      <GivenName>James</GivenName>
      <FamilyName>Stewart</FamilyName>
      <PhoneFixList>
      <PhoneFix>90909090</PhoneFix>
      </PhoneFixList>
      <EmailList>
      <Email>james.stewart@gmail.com</Email>
      </EmailList>
      </ContactPerson>
      </root>


      The XMLStarlet
      invocation



      xml sel -t -m '//ContactPerson' -v 'concat(GivenName, " ", FamilyName, " ", PhoneFixList/PhoneFix, " ", EmailList/Email)' -nl file.xml


      produces



      John Wayne 78787878 john.wayne@gmail.com
      James Stewart 90909090 james.stewart@gmail.com


      The invocation of XMLStarlet contains an implicit loop over all ContactPerson nodes, and for each of these, the specific fields are concatenated to produce the correct output. The -nl at the end causes a newline to be printed.







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited Feb 9 at 11:49

























      answered Feb 9 at 11:35









      Kusalananda

      103k13202318




      103k13202318











      • Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
        – lendoiro
        Feb 9 at 11:51







      • 1




        @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
        – Kusalananda
        Feb 9 at 12:05










      • Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
        – lendoiro
        Feb 9 at 12:08







      • 1




        Don't even try to process XML with tools that don't understand XML.
        – user32929
        Feb 9 at 13:04










      • That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
        – Kusalananda
        Feb 9 at 13:32
















      • Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
        – lendoiro
        Feb 9 at 11:51







      • 1




        @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
        – Kusalananda
        Feb 9 at 12:05










      • Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
        – lendoiro
        Feb 9 at 12:08







      • 1




        Don't even try to process XML with tools that don't understand XML.
        – user32929
        Feb 9 at 13:04










      • That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
        – Kusalananda
        Feb 9 at 13:32















      Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
      – lendoiro
      Feb 9 at 11:51





      Thank you Kusalananda. Is there any form to get the result without the xmlstarlet?
      – lendoiro
      Feb 9 at 11:51





      1




      1




      @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
      – Kusalananda
      Feb 9 at 12:05




      @lendoiro XMLStarlet in the command line XML parser that I know best. You could possibly do it by means of sed and/or awk as well, but these are generally not good tools for parsing XML, and I will not write an answer using them.
      – Kusalananda
      Feb 9 at 12:05












      Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
      – lendoiro
      Feb 9 at 12:08





      Ok. I was triying with sed and/or awk but I couldn't resolve it. Thank you for your reply
      – lendoiro
      Feb 9 at 12:08





      1




      1




      Don't even try to process XML with tools that don't understand XML.
      – user32929
      Feb 9 at 13:04




      Don't even try to process XML with tools that don't understand XML.
      – user32929
      Feb 9 at 13:04












      That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
      – Kusalananda
      Feb 9 at 13:32




      That last comment was probably for you @lendorio. I'm well aware of the issues with parsing XML using anything other than an XML parser.
      – Kusalananda
      Feb 9 at 13:32












      up vote
      1
      down vote













      With xml2 (and a shell with support for the $'...' quotes):



       echo "<r>"; cat file.xml; echo "</r>"; |
      | xml2 | 2csv -d $'t' ContactPerson GivenName FamilyName EmailList/Email


      (for ContactPersons with more than one email address, the last one is returned)






      share|improve this answer
























        up vote
        1
        down vote













        With xml2 (and a shell with support for the $'...' quotes):



         echo "<r>"; cat file.xml; echo "</r>"; |
        | xml2 | 2csv -d $'t' ContactPerson GivenName FamilyName EmailList/Email


        (for ContactPersons with more than one email address, the last one is returned)






        share|improve this answer






















          up vote
          1
          down vote










          up vote
          1
          down vote









          With xml2 (and a shell with support for the $'...' quotes):



           echo "<r>"; cat file.xml; echo "</r>"; |
          | xml2 | 2csv -d $'t' ContactPerson GivenName FamilyName EmailList/Email


          (for ContactPersons with more than one email address, the last one is returned)






          share|improve this answer












          With xml2 (and a shell with support for the $'...' quotes):



           echo "<r>"; cat file.xml; echo "</r>"; |
          | xml2 | 2csv -d $'t' ContactPerson GivenName FamilyName EmailList/Email


          (for ContactPersons with more than one email address, the last one is returned)







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Feb 9 at 12:53









          Stéphane Chazelas

          281k53516847




          281k53516847




















              up vote
              1
              down vote













              If you're OK with having a second file for processing this, you can make an XSLT file and use it to transform your XML file using xsltproc.



              phones.xsl:



              <?xml version="1.0"?>
              <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
              <xsl:output method="text"/>
              <xsl:strip-space elements="*"/>
              <xsl:template match="ContactPerson">
              <xsl:value-of select="GivenName"/><xsl:text> </xsl:text><xsl:value-of select="FamilyName"/>
              <xsl:for-each select="PhoneFixList/PhoneFix">
              <xsl:text> </xsl:text><xsl:value-of select="."/>
              </xsl:for-each>
              <xsl:for-each select="EmailList/Email">
              <xsl:text> </xsl:text><xsl:value-of select="."/>
              </xsl:for-each>
              <xsl:text>
              </xsl:text>
              </xsl:template>
              </xsl:stylesheet>


              Then transform the file (using @Kusalananda's XML as phones.xml):



              $ xsltproc phones.xsl phones.xml
              John Wayne 78787878 john.wayne@gmail.com
              James Stewart 90909090 james.stewart@gmail.com


              Using XSLT has the advantage that you can loop through repeating elements, do conditional output and so forth. If you're comfortable with a bit of study and programming, it's very powerful.






              share|improve this answer
























                up vote
                1
                down vote













                If you're OK with having a second file for processing this, you can make an XSLT file and use it to transform your XML file using xsltproc.



                phones.xsl:



                <?xml version="1.0"?>
                <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                <xsl:output method="text"/>
                <xsl:strip-space elements="*"/>
                <xsl:template match="ContactPerson">
                <xsl:value-of select="GivenName"/><xsl:text> </xsl:text><xsl:value-of select="FamilyName"/>
                <xsl:for-each select="PhoneFixList/PhoneFix">
                <xsl:text> </xsl:text><xsl:value-of select="."/>
                </xsl:for-each>
                <xsl:for-each select="EmailList/Email">
                <xsl:text> </xsl:text><xsl:value-of select="."/>
                </xsl:for-each>
                <xsl:text>
                </xsl:text>
                </xsl:template>
                </xsl:stylesheet>


                Then transform the file (using @Kusalananda's XML as phones.xml):



                $ xsltproc phones.xsl phones.xml
                John Wayne 78787878 john.wayne@gmail.com
                James Stewart 90909090 james.stewart@gmail.com


                Using XSLT has the advantage that you can loop through repeating elements, do conditional output and so forth. If you're comfortable with a bit of study and programming, it's very powerful.






                share|improve this answer






















                  up vote
                  1
                  down vote










                  up vote
                  1
                  down vote









                  If you're OK with having a second file for processing this, you can make an XSLT file and use it to transform your XML file using xsltproc.



                  phones.xsl:



                  <?xml version="1.0"?>
                  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                  <xsl:output method="text"/>
                  <xsl:strip-space elements="*"/>
                  <xsl:template match="ContactPerson">
                  <xsl:value-of select="GivenName"/><xsl:text> </xsl:text><xsl:value-of select="FamilyName"/>
                  <xsl:for-each select="PhoneFixList/PhoneFix">
                  <xsl:text> </xsl:text><xsl:value-of select="."/>
                  </xsl:for-each>
                  <xsl:for-each select="EmailList/Email">
                  <xsl:text> </xsl:text><xsl:value-of select="."/>
                  </xsl:for-each>
                  <xsl:text>
                  </xsl:text>
                  </xsl:template>
                  </xsl:stylesheet>


                  Then transform the file (using @Kusalananda's XML as phones.xml):



                  $ xsltproc phones.xsl phones.xml
                  John Wayne 78787878 john.wayne@gmail.com
                  James Stewart 90909090 james.stewart@gmail.com


                  Using XSLT has the advantage that you can loop through repeating elements, do conditional output and so forth. If you're comfortable with a bit of study and programming, it's very powerful.






                  share|improve this answer












                  If you're OK with having a second file for processing this, you can make an XSLT file and use it to transform your XML file using xsltproc.



                  phones.xsl:



                  <?xml version="1.0"?>
                  <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
                  <xsl:output method="text"/>
                  <xsl:strip-space elements="*"/>
                  <xsl:template match="ContactPerson">
                  <xsl:value-of select="GivenName"/><xsl:text> </xsl:text><xsl:value-of select="FamilyName"/>
                  <xsl:for-each select="PhoneFixList/PhoneFix">
                  <xsl:text> </xsl:text><xsl:value-of select="."/>
                  </xsl:for-each>
                  <xsl:for-each select="EmailList/Email">
                  <xsl:text> </xsl:text><xsl:value-of select="."/>
                  </xsl:for-each>
                  <xsl:text>
                  </xsl:text>
                  </xsl:template>
                  </xsl:stylesheet>


                  Then transform the file (using @Kusalananda's XML as phones.xml):



                  $ xsltproc phones.xsl phones.xml
                  John Wayne 78787878 john.wayne@gmail.com
                  James Stewart 90909090 james.stewart@gmail.com


                  Using XSLT has the advantage that you can loop through repeating elements, do conditional output and so forth. If you're comfortable with a bit of study and programming, it's very powerful.







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Feb 9 at 20:57









                  ErikF

                  2,7111413




                  2,7111413






















                       

                      draft saved


                      draft discarded


























                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f423013%2fparse-xml-file-in-a-specific-way%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Popular posts from this blog

                      Peggy Mitchell

                      Palaiologos

                      The Forum (Inglewood, California)