I need to find specific names (ie, a few names matching several regexp) and for each hide their corresponding values, wherever they appeared in the xml
By using a XML parsing library (Twig? libXML ? other?)
The regexp part is not for parsing, but for then selecting which nodes I need to edit (ie, I need to parse using a XML aware library, but then only to replace values in nodes where the name matches a specific complex regexp).
Deep apologies for not providing code attempts :( Even though I did try to adapt some of the answers (ex: https://stackoverflow.com/a/11482566/1841533, and quite a few others as well) I am too new to Perl to have come up with something that 1) would browse at any depth in the file, 2) looking for names that match a regexp. Posting my meager attempts would only "narrow" the direction of the discussion (ie, I really want to avoid the XYProblem : if I showed my existing attempts, and they were corrected, the requirements below would NOT be matched as my attempts lacked either "at any depth" or "name matching a regexp" completely ...)
**If you need (I can totally understand that...) sample codes, please don't read further. ** (or just a bit, to see why I don't provide any)
If however you can just read the 3 XML examples below, and the following 4 dots indicating what I need to do to them, (or better, everything after the 'What I need:' line), and provide me with a "template" script (ie, a few perl, if possible using twig or libXML), i'll forever be in your debt ^^.
[I do take lot of time to provide help to many people on various se sites... and I often wish they posted sample codes. So I understand why many people will downvote this, or just won't answer, or feel frustrated. But I can't manage to produce one sample code here without "warping" what I need too much, creating a XYproblem, hence I preferred posting what I need instead of what I tried...]
What I need
I have many xml files with different structures.
in the following: "someNames" could be several different strings, amongst which I'll need to find only those matching a (complex) regexp.
And once I find one (or several match) "someValue" will be the associated value , which i'll want to replace by a generic string.
The xmls are quite simple, but they still have several different structures:
For example sometimes the XML could contain
...
<sometag name=someName value=someValue>
...
(someName or someValue could be within quotes or not)
or
...
<someName>someValue</someName>
...
or even another form:
...
<someothertag someName=someValue>
...
- someValue could be withing quotes, or not, when it is after a "=", depending on the xml
- someName also could be within quotes or not, when it's written as
name=someName
someName changes in each file, but I want to find some matching a specific complex regexp (for example:
/\(abc\)|\([^xyz]*def\)|..../
, ie the regexp could be quite complex )for those "someName" that match the regexp, and only if they match, I want to change the corresponding "someValue" by a generic string, for example "hidden". (someValue itself can change in each file. But whatever it is (ie, can match ".*"), i want to replace it with the new value "hidden")
The deepness of the tags can also vary from file to file (so I need a generic parsing)
I'm sorry but I cannot find how to do that, as every exemple i found here are for a specific tag or specific structure, and from them I couldn't grasp the way to use twig
or libXML
to do a more generic approach... (I am very very new to Perl!)
I have trouble finding how to place the regexp, and even how to parse several XML and look for the name on any level within each xml
Any hint on how to do this is welcomed!
Update: I'm trying hard to come up with a reasonnable first try... But I think by the time I come up with one, i can delete that question. Right now i'm trying to Grok https://stackoverflow.com/a/11482566/1841533 : but it is NOT what I need. I need to modify that example to 1) allow to open any file (instead of provinding the XML directly as in that answer) 2) I need to use "findnodes" to find any tag whose name (tagName, and not its correspondign value) matches a regexp (and not some fixed "string") 3) and then once I find those tagnames, i need to edit the corresponding value to change it to "hidden".