我目前正在将XML从一个CMS迁移到另一个,并且需要将某些文本转换为元素。由于系统工作原理,一些编辑只能输入转义文本。挑战是取代一些这些转义元素并将其转换为有效的XML元素。使用XSLT将文本转换为XML中的元素
源文件:
<p>Press the <button-name>Select key </button-name>to show more information.</p>
<p>Press the <button-name>Back key</button-name> to save the
values.</p>
<p>When the storage is completed, the <product-name/> machine
displays:</p>
<p><attention>
<display-text translate="no">STORAGE COMPLETED
Press BACK to exit</display-text>
</attention></p>
我想做
Replace <button-name> with <gui>
Replace <button-name> with <kt.in name="custom-name"/>
Keeping other escaped elements.
XML我想
<p>Press the <gui>Select key</gui>to
show more information.</p>
<p>Press the <gui>Back key</gui>
to save the calibrations values.</p>
<p>When the storage is completed, the <kt.in name="custom-name"/> machine
displays:</p>
<p><attention> <display-text translate="no">STORAGE COMPLETED
Press BACK to exit</display-text>
</attention></p>
我尝试使用基于字符串的搜索和替换,但因为我想正确的XML元素作为输出,这不会做到这一点。
你使用哪种XSLT处理器?在我看来,正确的方法是使用扩展来解析'p'元素的内容,然后将模板应用到解析的节点,并根据需要进行转换并将其序列化(用于'attention')。然而,这种方法是否可行取决于所使用的XSLT处理器以及是否有扩展可用或可用于解析和序列化。 –
我通过AntillesXML使用Xalan。解析所有随后序列化回来是一个好主意。我试试看! – user2477722