Skip XML content while doing regex search and replace

╄→尐↘猪︶ㄣ 提交于 2019-12-11 18:08:20

问题


I have a xml string as follows

<root>
    <A id="1">This is an example</A>
    <B id="2">
            <C id="3">this is tag c</C>
    </B>
    <D id="4">this is tag d</D>
    <E id="5">
            <F id="6">this is tag f</F>
            <G id="7">this is tag g</G>
               .
               .
               .
    </E>
</root>

here i just want to search the word tag and replace it by Xml Tag using regex(Regular Expression), and i dont want to search that word inside the element <E>...</E> i.e., i want to skip element <E>...</E> while search and replace.

Any help is appreciated. Thanks in advance.


回答1:


The easiest way to do this varies with your environment, and with what you mean, exactly, by saying you want to skip element E. In XSLT or XQuery, a simple way to find text nodes that match a regular expression anywhere outside of E elements would be

//text()[not(ancestor::E)][matches(.,'tag')]

In other environments, it may or may not be convenient to do something analogous.

Note that if "skipping element E" means you want content of the form

<A>This is a tricky example
   of the word t<E>hi, mom!</E>ag.</A>

to return a hit on the search for "tag", then you will need a more complex solution.



来源:https://stackoverflow.com/questions/12474742/skip-xml-content-while-doing-regex-search-and-replace

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!