Pick XMl from list of XMLs based on the matching conditions



  • Re: extract XMl with regex

    Here is the real example for <ID> scenario

    For the given below XML

    ns:name
    ns:locationasfsafs</ns:location>
    ns:locationeventyyyy</ns:locationevent>
    ns:locations
    ns:location1
    ns:locationphase
    ns:Action
    ns:namePrior</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameCurrent</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameFuture</ns:name>
    </ns:Action>
    ns:StatusPending</ns:Status>
    </ns:locationphase>
    </ns:location1>
    </ns:locations>
    ns:Action
    ns:nameabc</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    <ID>125</ID>
    ns:Coverage
    ns:Action
    ns:namedeg</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    </ns:Coverage>
    </ns:name>

    ns:name
    ns:locationasfsafs</ns:location>
    ns:locationeventyyyy</ns:locationevent>
    ns:locations
    ns:location1
    ns:locationphase
    ns:Action
    ns:namePrior</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameCurrent</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameFuture</ns:name>
    </ns:Action>
    ns:StatusPending</ns:Status>
    </ns:locationphase>
    </ns:location1>
    </ns:locations>
    ns:Action
    ns:nameabc</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    <ID>1234ID>
    ns:Coverage
    ns:Action
    ns:namedeg</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    </ns:Coverage>
    </ns:name>

    ns:name
    ns:locationasfsafs</ns:location>
    ns:locationeventyyyy</ns:locationevent>
    ns:locations
    ns:location1
    ns:locationphase
    ns:Action
    ns:namePrior</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameCurrent</ns:name>
    </ns:Action>
    ns:StatusCompleted</ns:Status>
    </ns:locationphase>
    </ns:location1>
    ns:location1
    ns:locationphase
    ns:Action
    ns:nameFuture</ns:name>
    </ns:Action>
    ns:StatusPending</ns:Status>
    </ns:locationphase>
    </ns:location1>
    </ns:locations>
    ns:Action
    ns:nameabc</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    <ID>123</ID>
    ns:Coverage
    ns:Action
    ns:namedeg</ns:name>
    </ns:Action>
    ns:Action
    ns:namedef</ns:name>
    </ns:Action>
    </ns:Coverage>
    </ns:name>

    I need only the whole XML which matches the following conditions.
    Condition 1: ns:namens:locationeventyyyy</ns:locationevent>
    Condition 2: ns:namens:Action
    ns:namedef</ns:name>
    </ns:Action>
    – May have more than one action with name with other values
    Condition 3: ns:namens:Action
    ns:ID123</ns:ID>
    </ns:Action>



  • @vijay-S ,

    If you have additional information for a given question, add a reply to that question, rather than opening a new one.



  • Also, please remember this is not an xml-focused forum, not a regex writing service. Please show some effort; if you are going to be doing lots of xml massaging, you might look to see if the xmltools plugin will help you, or you might want to find a more-powerful xml processing tool, which will help you do thexml tasks you need; guy can work regex miracles, but it is not the best syntax for xml processing.

    Please Read And Understand This

    FYI: I often add this to my response in regex threads, unless I am sure the original poster has seen it before. Here is some helpful information for finding out more about regular expressions, and for formatting posts in this forum (especially quoting data) so that we can fully understand what you’re trying to ask:

    This forum is formatted using Markdown. Fortunately, it has a formatting toolbar above the edit window, and a preview window to the right; make use of those. The </> button formats text as “code”, so that the text you format with that button will come through literally ; use that formatting for example text that you want to make sure comes through literally, no matter what characters you use in the text (otherwise, the forum might interpret your example text as Markdown, with unexpected-for-you results, giving us a bad indication of what your data really is).

    Images can be pasted directly into your post, or you can hit the image button. (For more about how to manually use Markdown in this forum, please see @Scott-Sumner’s post in the “how to markdown code on this forum” topic, and my updates near the end.) Please use the preview window on the right to confirm that your text looks right before hitting SUBMIT. If you want to clearly communicate your text data to us, you need to properly format it.

    If you have further search-and-replace (“matching”, “marking”, “bookmarking”, regular expression, “regex”) needs, study the official Notepad++ searching using regular-expressions docs, as well as this forum’s FAQ and the documentation it points to. Before asking a new regex question, understand that for future requests, many of us will expect you to show what data you have (exactly), what data you want (exactly), what regex you already tried (to show that you’re showing effort), why you thought that regex would work (to prove it wasn’t just something randomly typed), and what data you’re getting with an explanation of why that result is wrong. When you show that effort, you’ll see us bend over backward to get things working for you. If you need help formatting, see the paragraph above.

    Please note that for all regex and related queries, it is best if you are explicit about what needs to match, and what shouldn’t match, and have multiple examples of both in your example dataset. Often, what shouldn’t match helps define the regular expression as much or more than what should match.

    Here is the way I usually break down trying to figure out a regex (whether it’s for myself or for helping someone in the forum):

    1. Compare what portions of each line I want to match is identical to every other one (“constants”), and what parts do I want to allow to be different in each line (“variables”) but still be part of the match.
    1. Look at both the variables and constants, and see what portions of each I’ll want to keep or move around, vs which parts get thrown away completely. Each sub-component that I want to keep will be put in a regex group. Anything that gets completely thrown away doesn’t need to be in a group, though sometimes I put it in a numbered (___) or unnumbered (?:___) group anyway, if I have a good reason for it. Anything that needs to be split apart, I break into multiple groups, instead of having it as one group.

    2. For each group, I do a mental “how would I describe to my son how to correctly match these characters?” – which should hopefully give me a simple, foolproof algorithm of characters that must match or must not match; then I ask, “how would I translate those instructions into regex sequences?” If I don’t know the answer to the second, I read documentation, or ask a specific question.

    3. try it, debug, iterate.



  • This is getting old…fast.


Log in to reply