Get the xml nodes where the search text is found

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











up vote
0
down vote

favorite












I having a well formatted and Indented xml similar to the below one. I need to get the mono tags which has a text " http://yahoo.com "



<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>


I was trying with the below command.



cat -n index.xml | sed -n "/root/,/root>/p" | grep "http://yahoo.com"



6 <tag1>http://yahoo.com</tag1>
12 <tag1>http://yahoo.com</tag1>


I need output like this.But don't know how to get the mono nodes which has my search text.



<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>


Kindly help







share|improve this question




















  • I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
    – Serge
    Jan 22 at 7:28














up vote
0
down vote

favorite












I having a well formatted and Indented xml similar to the below one. I need to get the mono tags which has a text " http://yahoo.com "



<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>


I was trying with the below command.



cat -n index.xml | sed -n "/root/,/root>/p" | grep "http://yahoo.com"



6 <tag1>http://yahoo.com</tag1>
12 <tag1>http://yahoo.com</tag1>


I need output like this.But don't know how to get the mono nodes which has my search text.



<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>


Kindly help







share|improve this question




















  • I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
    – Serge
    Jan 22 at 7:28












up vote
0
down vote

favorite









up vote
0
down vote

favorite











I having a well formatted and Indented xml similar to the below one. I need to get the mono tags which has a text " http://yahoo.com "



<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>


I was trying with the below command.



cat -n index.xml | sed -n "/root/,/root>/p" | grep "http://yahoo.com"



6 <tag1>http://yahoo.com</tag1>
12 <tag1>http://yahoo.com</tag1>


I need output like this.But don't know how to get the mono nodes which has my search text.



<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>


Kindly help







share|improve this question












I having a well formatted and Indented xml similar to the below one. I need to get the mono tags which has a text " http://yahoo.com "



<root>
<mono id="1">
<tag1>http://google.com</tag1>
</mono>
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="3">
<tag1>http://mysite.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>
</root>


I was trying with the below command.



cat -n index.xml | sed -n "/root/,/root>/p" | grep "http://yahoo.com"



6 <tag1>http://yahoo.com</tag1>
12 <tag1>http://yahoo.com</tag1>


I need output like this.But don't know how to get the mono nodes which has my search text.



<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono>
<mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>


Kindly help









share|improve this question











share|improve this question




share|improve this question










asked Jan 22 at 6:35









Samuel Finny

154




154











  • I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
    – Serge
    Jan 22 at 7:28
















  • I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
    – Serge
    Jan 22 at 7:28















I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
– Serge
Jan 22 at 7:28




I would recommend you to use xsltproc from libxml2 toolkit to achieve your goal
– Serge
Jan 22 at 7:28










1 Answer
1






active

oldest

votes

















up vote
0
down vote













Using XMLStarlet:



$ xml sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
<mono id="2">
<tag1>http://yahoo.com</tag1>
</mono><mono id="4">
<tag1>http://yahoo.com</tag1>
</mono>


The XPath //mono[tag1 = "http://yahoo.com"] means "any mongo node that has a sub-node called tag1 whose value is http://yahoo.com". The -c means "give me a copy of ..." and the final -nl inserts a newline at the end of the output.






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%2f418763%2fget-the-xml-nodes-where-the-search-text-is-found%23new-answer', 'question_page');

    );

    Post as a guest






























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes








    up vote
    0
    down vote













    Using XMLStarlet:



    $ xml sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
    <mono id="2">
    <tag1>http://yahoo.com</tag1>
    </mono><mono id="4">
    <tag1>http://yahoo.com</tag1>
    </mono>


    The XPath //mono[tag1 = "http://yahoo.com"] means "any mongo node that has a sub-node called tag1 whose value is http://yahoo.com". The -c means "give me a copy of ..." and the final -nl inserts a newline at the end of the output.






    share|improve this answer
























      up vote
      0
      down vote













      Using XMLStarlet:



      $ xml sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
      <mono id="2">
      <tag1>http://yahoo.com</tag1>
      </mono><mono id="4">
      <tag1>http://yahoo.com</tag1>
      </mono>


      The XPath //mono[tag1 = "http://yahoo.com"] means "any mongo node that has a sub-node called tag1 whose value is http://yahoo.com". The -c means "give me a copy of ..." and the final -nl inserts a newline at the end of the output.






      share|improve this answer






















        up vote
        0
        down vote










        up vote
        0
        down vote









        Using XMLStarlet:



        $ xml sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
        <mono id="2">
        <tag1>http://yahoo.com</tag1>
        </mono><mono id="4">
        <tag1>http://yahoo.com</tag1>
        </mono>


        The XPath //mono[tag1 = "http://yahoo.com"] means "any mongo node that has a sub-node called tag1 whose value is http://yahoo.com". The -c means "give me a copy of ..." and the final -nl inserts a newline at the end of the output.






        share|improve this answer












        Using XMLStarlet:



        $ xml sel -t -c '//mono[tag1 = "http://yahoo.com"]' -nl file.xml
        <mono id="2">
        <tag1>http://yahoo.com</tag1>
        </mono><mono id="4">
        <tag1>http://yahoo.com</tag1>
        </mono>


        The XPath //mono[tag1 = "http://yahoo.com"] means "any mongo node that has a sub-node called tag1 whose value is http://yahoo.com". The -c means "give me a copy of ..." and the final -nl inserts a newline at the end of the output.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 22 at 8:03









        Kusalananda

        103k13202319




        103k13202319






















             

            draft saved


            draft discarded


























             


            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f418763%2fget-the-xml-nodes-where-the-search-text-is-found%23new-answer', 'question_page');

            );

            Post as a guest













































































            Popular posts from this blog

            How to check contact read email or not when send email to Individual?

            Bahrain

            Postfix configuration issue with fips on centos 7; mailgun relay