How to convert tags in all tags in xml to lowercase without changing case of atribute values?

前端 未结 4 630
面向向阳花
面向向阳花 2021-01-25 08:10

I\'ve inherited some xml files which has all tags in uppercase. I would like to convert them to lowercase using either a regular expression or via XSLT. It would be handy to be

4条回答
  •  面向向阳花
    2021-01-25 08:25

    You might need 2 regexes in my opinion - one to convert the tag name, and another to convert the variable number of attribute-value pairs.

    Here is how I could do it -

    blah:tmp shreyas$ cat old.xml | perl -pe "s|( ]+)(.*?>)|\1\L\2\E\3|g" | perl -pe "s|(\w+)( ?= ?\".*?\")|\L\1\E\2|g" > processed.xml
    blah:tmp shreyas$ diff new.xml processed.xml 
    4c4
    <     

    It would be remiss of me to neglect to thank the bottle.

    --- >

    It would be remiss of me to neglect to thank the bottle.

    9,10c9,10 <

    It seems a violent betrayal, me divulging how...

    <

    The years had not been kind Felix Lake. His constant...

    --- >

    It seems a violent betrayal, me divulging how...

    >

    The years had not been kind Felix Lake. His constant...

    15c15 <

    As luck would not have it, he did.

    --- >

    As luck would not have it, he did.

    old.xml is your Before xml and new.xml is your After xml. processed.xml is the one generated by the command.

    As you can see, the P tags in your after xml are still capital. I am not sure if they were typos or exceptions. I trreated them as typos since you mentioned changing all tags to small case.

    With a small modification, you could run these commands on all of you inherited set of XMLs, and get them converted quickly.

提交回复
热议问题