问题
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